06 Tree Part02
06 Tree Part02
GTU # 3130702
Unit-3
Non-Linear Data
Structure (Tree Part-2)
Tree Traversal
The most common operations performed on tree structure is that of traversal.
This is a procedure by which each node in the tree is processed exactly once in a systematic
manner.
There are three ways of traversing a binary tree. A
1. Preorder Traversal
2. Inorder Traversal B D
3. Postorder Traversal
C E G
F
Preorder Traversal
Preorder traversal of a binary tree is defined as follow
1. Process the root node A ✓
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder B ✓ D ✓
A B C D E F G
Inorder Traversal
Inorder traversal of a binary tree is defined as follow
A ✓
1. Traverse the left subtree in Inorder
2. Process the root node
B ✓ D ✓
3. Traverse the right subtree in Inorder
C ✓ E ✓ G ✓
F ✓
C B A E F D G
Postorder Traversal
Postorder traversal of a binary tree is defined as follow
A ✓
1. Traverse the left subtree in Postorder
2. Traverse the right subtree in Postorder
B ✓ D ✓
3. Process the root node
C ✓ E ✓ G ✓
F ✓
2 3 25 3 1
75
6 22
4 22 40 60 80
5 45
5
15 30 90
23 65
34 78
Linked Representation of Binary Tree
T
LPTR DATA RPTR
A
B D
B D
C E G
C E G
F
F
Algorithm of Binary Tree Traversal
Preorder Traversal - Procedure: RPREORDER(T)
Inorder Traversal - Procedure: RINORDER(T)
Postorder Traversal - Procedure: RPOSTORDER(T)
Procedure: RPREORDER(T)
This procedure traverses the tree in preorder, in a recursive manner.
T is root node address of given binary tree LPTR DATA RPTR
Node structure of binary tree is described as below Typical node of Binary Tree
Postorder : G D B H I E F C A
A A
Inorder :DGBAHEICF
B C B C
A
D,G H,E,I F D E F
D,G,B H,E,I,C,F
G H I
Construct Binary Tree from Traversal
Preorder : G B Q A C K F P D E R H Inorder : Q B K C F A G P E D H R
G G G
QBKCFA PED HR B P B P
D Q A D
Q A
G
C E R
B KCF E HR
P
K F H
Q KCFA ED HR
Linked Representation of Binary Tree
T
LPTR DATA RPTR
A
B D
B D
C E G
C E G
F
F
Threaded Binary Tree
The wasted NULL links in the binary tree
storage representation can be replaced by T
threads
A binary tree is threaded according to A
particular traversal order. e.g.: Threads for
the inorder traversals of tree are pointers to
its higher nodes, for this traversal order B D
In-Order - C B A E F D G
C E G
Pre-Order - A B C D E F G
Post-Order - C B F E G D A
F
Threaded Binary Tree
In-Threaded Binary Tree
T
If left link of node P is null, then this link is
replaced by the address of its predecessor
If right link of node P is null, then this link is A
replaced by the address of its successor
In-Order - C B A E F D G C E G
Pre-Order - A B C D E F G
Post-Order - C B F E G D A F
Threaded Binary Tree
Method 1:- Represent thread a Negative address
Method 2:- To have a separate Boolean flag for each of left and right pointers, node structure for
this is given below
Head node is simply another node which serves as the predecessor HEAD
and successor of first and last tree nodes.
Tree is attached to the left branch of the head node.
Threaded Binary Tree
HEAD
A
B D A
C E G
B D
C E G
Inorder Traversal
C B A E F D G
F
B E
A
C D F H
B E
G
Inorder Traversal
C D F H
CBDAFGEH
G
Advantages of Threaded Binary Tree
Inorder traversal is faster than unthreaded version as stack is not required.
Effectively determines the predecessor and successor for inorder traversal, for unthreaded tree
this task is more difficult.
A stack is required to provide upward pointing information in binary tree which threading
provides without stack.
It is possible to generate successor or predecessor of any node without having over head of
stack with the help of threading.
Disadvantages of Threaded Binary Tree
Threaded trees are unable to share common sub trees.
If Negative addressing is not permitted in programming language, two additional fields are
required.
Insertion into and deletion from threaded binary tree are more time consuming because both
thread and structural link must be maintained.
Binary Search Tree (BST)
A binary search tree is a binary tree in which each node possessed a key that satisfy the
following conditions
1. All key (if any) in the left sub tree of the root precedes the key in the root
2. The key in the root precedes all key (if any) in the right sub tree
3. The left and right sub trees of the root are again search trees
Construct Binary Search Tree (BST)
Construct binary search tree for the following data
50 , 25 , 75 , 22 , 40 , 60 , 80 , 90 , 15 , 30
50
25 75
22 40 60 80
15 30 90
Delete node a
a Delete node a b
3 8
2 5 7 9
1 4 10
In-Order Traversal : 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
After Delete a Node ( 6 ) : 1 , 2 , 3 , 4 , 5 , 7 , 8 , 9 , 10
Construct Binary Search Tree (BST)
Construct binary search tree for the following data
6 , 3 , 8 , 2 , 5 , 7 , 9 , 10 , 1 , 4 , 6.5 , 7.5
3 8
2 5 7 9
1 4 6.5 7.5 10
3 8 7
2 5 7 9 3 8
2 5 7.5 9
1 4 7.5 10
1 4 10
Delete node a
a C
b C
Thank
You