1 / 25

Binary Trees

Learn about binary tree structure, insertion, deletion, traversal methods, and binary search tree activity.

ksteve
Download Presentation

Binary Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Trees

  2. Binary tree structure Root Node stores the first data item Node can have up to 2 descendants Leaf Nodes have no descendants Root Node Node Node Node Node Leaf Leaf Leaf Leaf Leaf

  3. Descendants are the children Parent Left Child Right Child

  4. Binary tree structure – subtrees and branches Root Branch Left Subtree Right Subtree

  5. Which way? • Less than – Goes to the Left • More than – Goes to the Right

  6. Adding items to a Binary Tree 5 3 7 4 9 1 6 2 8 5 3 7 9 1 4 6 2 8

  7. Constructing a binary tree Daniel Charles George Belinda Cheryl Fred • The following data items are to be stored in a binary tree: • Daniel, Charles, Belinda, Cheryl, George and Fred.

  8. Add a name to the binary tree • To add the name Pete and Edward Daniel Charles George Belinda Cheryl Fred Pete Edward

  9. Adding items to a Search Tree W A T F O R D Y X W A Y T X F D O R

  10. A .- B -... C -.-. D -.. E . F ..-. G --. Morse Code Binary Search Tree Activity H .... I .. J .--- K -.- L .-.. M -- N -. O --- P .--. Q --.- R .-. S ... T - U ..- V ...- W .-- X -..- Y -.-- Z --.. Morse Root ? ?

  11. Morse Code Binary Search Tree Root E T I A N M S U R W D K G O H V F L P J B X C Y Z Q

  12. Binary Tree Traversal Pre-Order In-Order Post-Order

  13. What is traversal? • Methods of looking at each node in a tree, in turn • Recursive • Pre-Order, In-Order, Post-Order • Recursively Defined – Calls itself within itself • Must have an End Condition

  14. Pre order Traversal Draw an outline around the tree starting to the left of the root, output each node as you pass to it’s left Root Node John Adam Simon Adam Dan Richard Output:John, Adam, Adam, Dan, Simon, Richard

  15. Post order Traversal Draw an outline around the tree starting to the left of the root, output each node as you pass to the right of it. Root Node John Adam Simon Adam Dan Richard Output:Adam, Dan, Adam, Richard, Simon, John

  16. In order Traversal Draw an outline around the tree starting to the left of the root, output each node as you pass beneath it Root Node John Adam Simon Adam Dan Richard Output:Adam, Adam, Dan, John, Richard, Simon

  17. Golden Rules Pre – Left Post – Right In - Under

  18. Pre-Order Traversal 8 4 2 1 3 6 5 7 12 10 9 11 14 13 15 8 12 4 10 14 2 6 1 3 5 7 9 11 13 15

  19. In-Order Traversal 1-2-3-4-5-6-7-8-9-10-11-12-13-14-15 8 12 4 10 14 2 6 1 3 5 7 9 11 13 15

  20. Post-Order Traversal 1 3 2 5 7 6 4 9 11 10 13 15 14 12 8 8 12 4 10 14 2 6 1 3 5 7 9 11 13 15

  21. Binary Tree – Insertion & Deletion Root Node stores the first data item Node can have up to 2 descendants Leaf Nodes have no descendants Root Node Node Node Node Node Leaf Leaf Leaf Leaf Leaf

  22. Structure of a Node Each node stores 4 pieces of Data This allows the tree structure to be stored with an array Position Data Item Right Pointer Address Left Pointer Address

  23. Insert 4 into tree Insert 8 into tree 5 3 7 9 1 4 6 2 8

  24. Inserting a Node Pseudo Code CurrentNode = Root Repeat If NewNode < CurrentNode travel Left Else travel Right CurrentNode = Node Reached Until Node = Null Create Node(NewNode)

  25. Deleting a Node • 2 Methods of removing a Node within a Tree • Remove Node and all its sub Nodes • Store Sub Nodes on a temp store (array) • Reinsert each Node stored on temp store OR • Set Data Item of deleted node to Null • Leave node within structure

More Related