ADA_UNIT_5_1_Trees
ADA_UNIT_5_1_Trees
Definition:
ii)the remaining nodes are partitioned into n disjoint sets T 1, T2, T3, .........Tn where T1,
T2, T3, .........Tn are called sub trees.
Example
Terminologies:
Node: Node of a tree stores the actual data and links to the other node.
Root node: A first node written at the top is root node. The root node does not have the
parent.
Child: The node obtained from the parent node is called child node.
Predecessor: Every node N in a tree except root has a unique parent called as predecessor of
N. Here B is the predecessor of D, E, H, I and J.
Successor: Every node N in a tree except the leaf nodes has a unique child called as
successor of N. Here D, E, H, I and J are the successor of B.
Siblings: Two or more nodes having the same parent are called siblings. Here D and E are
siblings since they have same parent B.
Ancestors: The node n1 is said to be an ancestor of other node n2 if n1 is either the father of
n2 or the father of some ancestor of n2. Here A is the ancestor of B, D, E, H, I.
Descendant: The node n1 is called the descendant of node n2, if the node n2 is reachable
from n1. Here H is the descendant of D, B, A.
Degree: The number of sub trees of a node is called its degree. Here the node B has two sub
trees. So, degree of node B is 2.
Level: The distance of a node from the root is called level of the node. (or) Level is the rank
of hierarchy. The level of root node is 0.Here the level of D is 2.
Height (Depth): The height of the tree is defined as the maximum level + 1 of any leaf in the
tree. Here height of D is 3
Forest: A forest is a set of disjoint trees. The representation of forest is similar to tree.
Example in the above figure if we remove the root of tree a forest is created with 2 trees
headed by node B and C as root.
Binary Tree:
i)T is empty or
iii) the remaining nodes of T form only upto two disjoint binary trees T1 and T2 which are
called the left sub-tree and right sub-tree.
Strictly Binary Tree:
Strictly binary tree is a binary tree that has non-empty left and right sub trees. The out degree
of every node is either zero or 2.
A complete binary tree is a binary tree, which is completely filled, with the possible
exception of the bottom level, which is filled from left to right.
ii)For any node in the tree with a right descendant at level n, the node must have a left child
and every left descendant of node is either a leaf at level n or has two children.
Binary Tree Traversal:
Traversal is a process of visiting each nod exactly once in a systematic order. Binary tree has
three types of traversals.
1. Preorder
2. Inorder
3. Postorder
1. Preorder Traversal
The preorder traversal of a binary tree can be recursively defined as follows:
• Process the node data (D)
• Traverse the left subtree in preorder (L)
• Traverse the right subtree in preorder (R)
Algorithm:
Explanation:
Using the above algorithm we write the preorder traversal of the below binary tree.
Step 1: It process the root node. It print 8
Step 2: Then it process the left link of 8, here it is 5.
Step 3: Then it process the left link of 5, here it is 9.
Step 4: Then it process the left link and right link of 9, here it is NULL.
Step 5: So it process the right link of 5, here it is 7.
Step 6: Recursively it call pre_order() until all the node has been visited.
2. Inorder Traversal
The inorder traversal of a binary tree can be recursively defined as follows:
• Traverse the left subtree in preorder (L)
• Process the node data (D)
• Traverse the right subtree in preorder (R)
Algorithm:
Explanation:
Using the above algorithm we write the inorder traversal of the below binary tree.
Step 1: It visit the root node. It check left node is NULL or not. Here it has value 5
Step 2: It visit the node 5. It check left node is NULL or not. Here it has value 9
Step 3: It visit the node 9. It check left node is NULL or not. Here the left child is NULL.
Step 4: So it process the data.
Step 5:Recursively it call in_order() until all the node has been visited.
3. Postorder Traversal
The postorder traversal of a binary tree can be recursively defined as follows:
• Traverse the left subtree in preorder (L)
• Traverse the right subtree in preorder (R)
• Process the node data (D)
Algorithm:
Explanation:
Using the above algorithm we write the postorder traversal of the below binary tree.
Step 1: It visit the root node. It check left node is NULL or not. Here it has value 5
Step 2: It visit the node 5. It check left node is NULL or not. Here it has value 9
Step 3: It visit the node 9. It check left node is NULL or not. Here the left child is NULL.
Step 4: So it process the data.
Step 5:Recursively it call post_order() until all the node has been visited.