1 / 71

Trees, Trees, and More Trees

Trees, Trees, and More Trees. Trees, Trees, and More Trees.

limei
Download Presentation

Trees, Trees, and More 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. Trees, Trees, and More Trees

  2. Trees, Trees, and More Trees By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully, each of you leaves with something new and exciting learned about trees. Try not to lose sight of the forest through the trees. Sorry, this was all very bad. I hope it didn’t leaf a bad taste in your mouth.

  3. TREES A binary tree is a set of elements that is either empty or contains a single element (the root of the tree) and whose remaining elements are partitioned into two disjoint subsets, each of which itself is a binary tree.

  4. root H Right subtree rooted at P Left subtree P D L W G A R E

  5. H root level 0 Left subtree Right subtree P D level 1 L W G A level 2 R E level 3

  6. Full Binary Tree of Level n

  7. A is the root of the tree. A is the parent of B and C. A is an ancestor of all nodes. A B C D E F G N O L M H I J K

  8. B and C are siblings. J is a descendent of B. A B C D E F G N O L M H I J K

  9. Height of binary tree : • Number of nodes on the longest path from the root to a leaf. • The height of the empty tree is 0. • The height of a single node tree is 1. • Note: (not an AP term) the definition of “height” is not from some “cs bible” – some will define other ways

  10. Height of binary tree ? A B C D E F G N O L M H I J K

  11. Height of binary tree ? A B C D E F G N O L M H I J K Height = 4

  12. Full Binary : A recursive definition A binary tree is full if the tree is empty or if the tree's left and right subtrees have the same height and both are full

  13. Binary Search Tree • A binary search tree : • every node in the left subtree is less than or equal to its parent node. • Every node in the right subtree is greater than its parent node • When the tree is traversed in order, the values of the nodes will be in order.

  14. To insert to a binary search tree: if value less than go left else go right

  15. Insert : 15 8 25 6 14 24 20 22 30 13 26 15

  16. Insert : 14 8 25 6 14 24 20 22 30 13 26 15 8

  17. Insert : 14 8 25 6 14 24 20 22 30 13 26 15 8 25

  18. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6

  19. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 14

  20. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14

  21. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14 20

  22. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14 20 22

  23. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14 30 20 22

  24. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 24 6 14 30 20 13 22

  25. Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 24 6 14 30 26 13 20 22

  26. Tree Traversals See also: animations on web site

  27. Inorder traversal of a binary tree left…root…right A B C D E F G N O L M H I J K

  28. Inorder traversal of a binary tree left…root…right A B C D E F G N O L M H I J K H D I B J E K A L F M C N G O

  29. Preorder traversal of a binary tree: root…left…right A B C D E F G N O L M H I J K

  30. Preorder traversal of a binary tree: root…left…right A B C D E F G N O L M H I J K A B D H I E J KC F L M G N O

  31. Postorder traversal of a binary tree: left…right…ROOT A B C D E F G N O L M H I J K

  32. Postorder traversal of a binary tree: left…right…ROOT A B C D E F G N O L M H I J K H I D J K E B L M F N O G C A

  33. Inorder traversal ? L B W X E F G T E P A Q

  34. Inorder traversal : L B W X E F G T E P A Q XA B E Q L P F W T G E

  35. Preorder traversal ? L B W X E F G T E P A Q

  36. Preorder traversal: L B W X E F G T E P A Q L B X A E Q W F PG T E

  37. Postorder traversal ? L B W X E F G T E P A Q

  38. Postorder traversal : L B W X E F G T E P A Q A X Q E B P F T E G W L

  39. breadth-first-order tree traversal • ROW (or level) order traversal A B C F G D E H I

  40. breadth-first-order tree traversal • ROW (or level) order traversal A B C D E F G H I A B C F G D E H I

  41. Inorder Traversal ? 15 8 25 24 6 14 30 26 13 20 22

  42. Inorder Traversal : 15 8 25 24 6 14 30 26 13 20 22 6 8 13 14 15 20 22 24 25 26 30

  43. Preorder Traversal ? 15 8 25 24 6 14 30 26 13 20 22

  44. Preorder Traversal : 15 8 25 24 6 14 30 26 13 20 22 15 8 6 14 13 25 24 20 22 30 26

  45. Postorder Traversal ? 15 8 25 24 6 14 30 26 13 20 22

  46. Postorder Traversal : 15 8 25 24 6 14 30 26 13 20 22 6 13 14 8 22 20 24 26 30 25 15

  47. Our Tree: 15 8 25 24 6 14 30 26 13 20 22 What is the height of the tree?

  48. Height : 15 8 25 24 6 14 30 26 13 20 22 Height = 5

  49. Deleting a node from a binary tree L D P H C A F J

  50. To delete a leaf... L D P H C A F J

More Related