1 / 29

Introduction to Trees

Introduction to Trees. Joe Meehean. Conceptual Picture. A. B. I. C. D. E. X. Conceptual Picture. A. B. I. C. D. E. X. Terminology e ach circle is a node pointers are edges t opmost node is the root b ottom nodes are leaves no outgoing edges Every non-empty tree has

trisha
Download Presentation

Introduction to 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. Introduction to Trees Joe Meehean

  2. Conceptual Picture A B I C D E X

  3. Conceptual Picture A B I C D E X • Terminology • each circle is a node • pointers are edges • topmost node is the root • bottom nodes are leaves • no outgoing edges • Every non-empty tree has • one root • one or more leaves

  4. More terminology A • Node A is the parent of node B • Node B is the child of node A • The root has no parent • All other nodes have exactly 1 parent B

  5. More terminology • Not a tree • D has 2 parents A B C D

  6. More terminology Tree 1 Tree 2 • Path is a sequence of connected nodes • Length of a path is the number of edges in the path • A to D is 2 • C to F is 1 • X is 0 A C B F Tree 3 D X

  7. More terminology • Height of a tree: length of its longest path from root to leaf • Ex: Height of 2 A C B E D

  8. More terminology • Depth of a node: length of path from root • Example • A: 0 • B: 1 • E: 2 • D: 2 • C: 1 A C B E D

  9. More terminology • Subtrees of a node are the nodes rooted at a nodes children • As subtrees • rooted at B • rooted at C A C B E D

  10. Binary Trees • Special Tree • No node has more than 2 children • can have 0,1, or 2 • Each child is either “right” or “left” A C B E D

  11. Binary Trees A S X C B T Y E D Z

  12. Representing Binary Trees template <typename D> class BinaryTreenode<D>{ private: D data_; BinaryTreenode<D>* left_; BinaryTreenode<D>* right_; public: BinaryTreenode(D d = D(), BinaryTreenode<D>* left = NULL, BinaryTreenode<D>* right = NULL); };

  13. Representing Binary Trees data: A A right: left: C B data: B data: C right: right: left: left: D data: D right: left:

  14. Binary Trees and Recursion • Example • Recursive definition of height for binary trees • an empty tree has height 0 • an non-empty tree has height 1 + max(height of left subtree, height of right subtree)

  15. Representing General Trees template <class D> class Treenode<D>{ private: D data; list<Treenode<D>*> kids; • No fixed number of children per node • can’t have single member variable per child • use a list of children • items in list will be Treenode *s

  16. Representing General Trees A count: 3 data:A kids: items: D B C data: B data: C data: D kids: kids: kids:

  17. Representing General Trees template <class D> class Treenode<D>{ private: D data; Treenode<D> *first_child_; Treenode<D> *next_sibling_; • Alternative • each node has a pointer to its first child • each node has a pointer to its next sibling • more basic linked list style

  18. Representing General Trees A data:A next_: first_: D B C data: B data: C data: D next_: next_: next_: first_: first_: first_:

  19. Questions?

  20. Tree Traversals • Iterate through all nodes • each node visited once, to… • print all values • see if a node has some property • modify the node, etc… • 4 common orders for visiting nodes • preorder • postorder • in order (binary trees only) • level order

  21. Preorder Traversal A • Depth-first traversal • Visit the root first • Recursive definition • visit the root • do a preorder traversal of each subtree, left to right • Ex: A B E D C F 1 C B 2 5 F E D 3 4 6

  22. Preorder Traversal Code void preorder(Treenode<D>* node){ if( node != null ){ //--visit node— // preorder the children preorder(node->first_); // preorder the siblings preorder(node->next_); } }

  23. Postorder traversal A • Depth-first traversal • Visit the root last • Recursive definition • do a postorder traversal of each subtree, left to right • visit the root • Ex: E D B F C A 6 C B 3 5 F E D 1 2 4

  24. In-order traversal A • Depth-first traversal • For binary trees only • Visit root in between subtrees • Recursive definition • in-order traversal of left subtree • visit the root • in-order traversal of right subtree • Ex: E B D A F C 4 C B 2 6 F E D 1 3 5

  25. In, post, preorder traversal Difference is in when root is visited Root first => preorder Root last => postorder Inbetween => in-order

  26. Level order traversal q.push(root); while( !q.empty() ){ //dequeue node n //visit n //enqueue all of n’schildren(L to R) } • Visit all nodes at level 1 (depth 1) • then level 2, level 3, etc… • always left to right (or some order) • Use a queue • instead of recursion (implicitly uses a stack)

  27. Questions?

  28. DISCUSSION BREAK!!! I laughed (ha!) and jumped ate cakes he she all five Do the pre, post, in, and level order traversals

  29. DISCUSSION BREAK!!! I laughed (ha!) and jumped ate cakes he she all five Pre: I laughed and he jumped she (ha!) ate all 5 cakes In: and he laughed she jumped I all ate 5 (ha!) cakes Post: he and she jumped laughed all 5 ate cakes (ha!) I Level: I laughed (ha!) and jumped ate cakes he she all 5

More Related