1 / 30

Data Structures – Binary Tree

Data Structures – Binary Tree. What is a tree?. Where do you see trees?. Ummm...outside file systems. Tree Terminology. node – any element of the tree root – the topmost node of the tree parent – a node that has one or more nodes connected below it (children)

haines
Télécharger la présentation

Data Structures – Binary Tree

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. Data Structures – Binary Tree

  2. What is a tree?

  3. Where do you see trees? • Ummm...outside • file systems

  4. Tree Terminology • node – any element of the tree • root – the topmost node of the tree • parent – a node that has one or more nodes connected below it (children) • child – a node that has a connected node above it (parent) • leaf – any child node at the bottom of the tree • subtree – a parent and all the nodes below it

  5. Name that part! root / parent subtree parent / child’ child / leaf child / leaf child / leaf

  6. So what’s a binary tree? • A parent can have at most TWO children (left child, right child) • The left child’s data and all the data in the left subtree is “less than” the parent’s data • The right child’s data all the data in the right subtree is “greater than” the parent’s data • NOTE: The “less than” and “greater than” requirements of the subtrees is commonly known outside of the IB world as a Binary Search Tree

  7. Example – Valid Binary Tree 6 2 10 4 1

  8. Example – INVALID Binary Tree 3 2 10 4 1

  9. Practice 1 • Create a binary tree by inserting the following numbers in the given order: 6, 4, 2, 7, 8, 14, 9

  10. Practice 2 • Create a binary tree by inserting the following numbers in the given order : 14, 75, 2, 34, 25, 26, 27, 28

  11. Another Binary Tree h d m a k x

  12. Practice 3 • Create a binary tree by inserting the following strings: Wanda, Alpos, Vazbyte, Fecso, Downs, Mata, Montante

  13. Practice 4 • Create a binary tree by inserting the following strings: Ramos, Dia, Khurelbaatar, Nice, Ren, Shahid, Zetkulic

  14. Adding to a Binary Tree • Start at the root • Compare against the current node • Go left if less than current node OR insert if there is none (creating a new leaf) • Go right if greater than current node OR insert if there is none (creating a new leaf) • Repeat step 2

  15. Searching in a Binary Tree • Start at the root • Compare against the current node • Found the node if they are equal • Go left if less than current node OR if there is no left, then does not exist • Go right if greater than current node OR if there is no left, then does not exist • Repeat step 2

  16. Search(4) – What path do you take? 6 2 10 4 13 1

  17. Search(9) – What path do you take? 6 2 10 4 13 1

  18. Removing from a Binary Tree • Search for matching node • If node is a leaf, then unlink its parent • If node is a parent of one child, then link the node’s parent to the node’s child • If node is a parent of two children, then travel down its right subtree to find the left-most leaf (smallest value of the right subtree). Take the value and put it in the original node that was being removed. Unlink the right-most leaf that you found.

  19. Remove(4) – 0 children case 6 2 10 4 13 1

  20. Remove(10) – 1 child case 6 2 10 4 13 1

  21. Remove(6) – 2 children case 6 2 10 4 13 1

  22. When do we use binary trees? • ...whenever we need to search for quickly • Inherent binary searching capabilities • Large trees can be searched quickly • What is the best case scenario? • What is the worst case scenario? • What is the average case scenario? • Compare a Linked List to a Binary Tree

  23. Tree Traversal • Add, remove, search only go down 1 path • How do you “walk-through” all the nodes of a tree?

  24. In-order tree traversal • Left-subtree traversal if it exists and rerun • Action on the current node (e.g. print) • Right-subtree traversal if it exists and rerun Note: In-order traversal often used to visit nodes in their inherent order

  25. In-order Traversal (1, 2, 4, 6, 8, 10, 13) 6 2 10 4 8 13 1

  26. Pre-order tree traversal • Action on the current node (e.g. copy) • Left-subtree traversal if it exists and rerun • Right-subtree traversal if it exists and rerun Note: Pre-order traversal is often used to duplicate a tree

  27. Pre-order Traversal (6, 2, 1, 4, 10, 8, 13) 6 2 10 4 8 13 1

  28. Post-order tree traversal • Left-subtree traversal if it exists and rerun • Right-subtree traversal if it exists and rerun • Action on the current node (e.g. print) Note: Post-order traversal is often used to completely delete or free up all nodes by visiting children and lowest levels first. (not really necessary with garbage collection)

  29. Post-order Traversal (1, 4, 2, 8, 13, 10, 6) 6 2 10 4 8 13 1

  30. Other Resources • http://www.csanimated.com/animation.php?t=Binary_search_tree

More Related