Tree and its terminologies The root Ancestors a the links is directed, always pointing down 1 Descendants height of b = 4 b c 1 1 height of c = 1 2 leaf depth of d =2 Parent of e,f,g,h,i 2 d Children of d Siblings e f g h i 3 leaf leaf leaf Siblings j k Siblings m n k 4 leaf leaf leaf leaf leaf 6/3/2019 IT 279
More Terminologies In-degree: the number of links pointing to a node (always 1 or 0); the root is the only node with in-degree 0. 2. Out-degree: the number of link pointing out from a node; leaves are node with out-degree 0 3 . Degree of a tree (arity): the maximum out-degree of nodes in the tree a b c d e f g h i j k 6/3/2019 IT 279
Binary Tree (2 degree tree) 6/3/2019 Linked Lists Binary Tree (2 degree tree) data Data field Right and Left Children 2 Left child Right child 24 34 13 4 34 6/3/2019 IT 279
C++ : template<typename T> class BinaryTree { public: … private: struct Node { Node(T item, Node *left, Node *right): data(item), left(left), right(right) {}; T data; Node<T> *left; Node<T> *right; }; 6/3/2019 IT 279
A perfect-tree or complete tree is a perfect example for using array d1 d1 d2 d3 d2 d3 d7 d4 d4 d5 d6 d5 d6 d7 d8 d8 d9 d10 d11 d12 d13 d14 d15 d9 d10 complete tree full-tree (no single child) perfect-tree 24-1 6/3/2019 IT 279
a b g d h i j k c l m v w x y z f g h q i 6/3/2019 IT 279
Tree Implementation: linked lists arrays 6/3/2019 Tree Implementation: linked lists arrays Links data data field enough fields for pointers pointing the children Usually, we use the degree of the tree as the number of the pointer fields. Fixed, if the degree is small, e.g., binary tree (degree is 2). 6/3/2019 IT 279
a b g d h i j k c l m v w x y z f g h q i 6/3/2019 IT 279