Lecture 8 Tree Data Structure
Lecture 8 Tree Data Structure
Algorithms
The above figure shows a tree where node A is the root node; nodes B, C, and D are
children of the root node and form sub-trees of the tree rooted at node A.
•Ancestor node An ancestor of a node is any predecessor node on the path from root
to that node.
•The root node does not have any ancestors. In the tree given in the above diagram,
nodes A, C, and G are the ancestors of node K.
•Descendant node A descendant node is any successor node on any path from the node
to a leaf node.
•Leaf nodes do not have any descendants. In the tree given in above, nodes C, G, J,
and K are the descendants of node A.
•A parent is an ancestor to its children and its children are descendants
Indegree of a node -The number of edges merging into a node. For example,
indegree of the node B is one i.e., one edge merges.
Outdegree of a node -The number of edges coming out from a node. For
example, outdegree of the node A is two i.e., two edges come out of this root
node.
DATA STRUCTURES AND ALGORITHMS 12
Basic Terminology cont…
Depth of a node - the length of the path from the root to that
node
•root: depth 0
Height of a node - the length of the longest path from that node to
a leaf
•any leaf: height 0
Height of a tree: The length of the longest path from the root to a
leaf
DATA STRUCTURES AND ALGORITHMS 13
Trees - Example
2. Traverse the
3. Traverse the
Left Subtree
Right Subtree
A, B, D, H, I, E, C, F, J, K, G
H,D,I,B,E,A,J,F,K,C,G
H, I, D, E, B, J, K, F, G, C, A
DATA STRUCTURES AND ALGORITHMS 67
Post-Order Traversal
Pre-order list –
2,5,11,6,7,4,9,5,2