300 likes | 429 Views
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
E N D
Introduction to Trees Joe Meehean
Conceptual Picture A B I C D E X
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
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
More terminology • Not a tree • D has 2 parents A B C D
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
More terminology • Height of a tree: length of its longest path from root to leaf • Ex: Height of 2 A C B E D
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
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
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
Binary Trees A S X C B T Y E D Z
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); };
Representing Binary Trees data: A A right: left: C B data: B data: C right: right: left: left: D data: D right: left:
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)
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
Representing General Trees A count: 3 data:A kids: items: D B C data: B data: C data: D kids: kids: kids:
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
Representing General Trees A data:A next_: first_: D B C data: B data: C data: D next_: next_: next_: first_: first_: first_:
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
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
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_); } }
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
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
In, post, preorder traversal Difference is in when root is visited Root first => preorder Root last => postorder Inbetween => in-order
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)
DISCUSSION BREAK!!! I laughed (ha!) and jumped ate cakes he she all five Do the pre, post, in, and level order traversals
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