0% found this document useful (0 votes)
31 views7 pages

ADA_UNIT_5_1_Trees

A tree is a non-linear data structure consisting of nodes, with a root node and disjoint subtrees. Key terminologies include node, child, predecessor, successor, siblings, ancestors, descendants, degree, level, height, and path. The document also describes binary trees, their types, and traversal methods including preorder, inorder, and postorder.

Uploaded by

ahmedfaraz1102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

ADA_UNIT_5_1_Trees

A tree is a non-linear data structure consisting of nodes, with a root node and disjoint subtrees. Key terminologies include node, child, predecessor, successor, siblings, ancestors, descendants, degree, level, height, and path. The document also describes binary trees, their types, and traversal methods including preorder, inorder, and postorder.

Uploaded by

ahmedfaraz1102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Trees

Tree is a non-linear data structure.

Definition:

A tree is a finite set of one or more nodes such that

i)there is a special node called the root node.

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

Path − Path refers to sequence of nodes along the edges of a tree.

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:

A binary tree T is a finite set of nodes such that

i)T is empty or

ii)T contains a special node called root node and

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.

Full or Complete binary tree:

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.

Almost full Binary tree:

A binary tree of depth n is an almost complete binary tree if

i)any node at level less than n-1 has two children.

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)

The following algorithm illustrates preorder traversal.

Algorithm:

Step1: Process the root node.


If root ≠ NULL then
Print ptr->info
Else
Print “Tree is empty”
Step 2: Traverse the left sub tree recursively in preorder
If ptr->left ≠ NULL
Call pre_order(ptr->left)
Step 3: Traverse the right sub tree recursively in preorder
If ptr->right ≠ NULL
Call pre_order(ptr->right)
Step 4: return

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.

The preorder traversal form of above binary tree is


8→ 5 → 9 → 7 → 1 → 12 → 2 → 4 → 11 → 3

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:

Step1: Check tree is empty


If root == NULL then
Print “Tree is empty”
Step 2: Traverse the left sub tree recursively in inorder
If ptr->left ≠ NULL
Call in_order(ptr->left)
Step 3: Process the root node.
Print ptr->info
Step 4: Traverse the right sub tree recursively in inorder
If ptr->right ≠ NULL
Call in_order(ptr->right)
Step 5: return

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.

The inorder traversal form of above binary tree is


9→ 5 → 1 → 7 → 2 → 12 → 8 → 4 → 3 → 11

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:

Step1: Check tree is empty


If root == NULL then
Print “Tree is empty”
Step 2: Traverse the left sub tree recursively in postorder
If ptr->left ≠ NULL
Call post_order(ptr->left)
Step 3: Traverse the right sub tree recursively in postorder
If ptr->right ≠ NULL
Call post_order(ptr->right)
Step 4: Process the root node.
Print ptr->info
Step 5: return

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.

The postorder traversal form of above binary tree is


9→ 1 → 2 → 12 → 7 → 5 → 3 → 11 → 4 → 8

You might also like