Tree Data Structure
Tree Data Structure
Recursive definition
T is either empty
or consists of a node r (the root) and a possibly empty set of trees
whose roots are the children of r
Trees Data Structures
Tree
Nodes
Each node can have 0 or more children
A node can have at most one parent
Binary tree
Tree with 02 children per node
Terminology
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
Root node
Leaf nodes
Tree Node Level and Path
Length
A L
evel:0
B C D L
evel:1
E F L
evel:2
G H L
evel:3
6
6 Min Index Contents
The level of a node is the length of the path from the root to that
node.
The ancestors of a node are all the nodes along the path from
the root to the node and Descendants after that nodes to leaves
node.
Tree Properties
Property Value
A Number of nodes
Height
B C Root Node
Leaves
D E F Interior nodes
Number of levels
Ancestors of H
G Descendants of B
Siblings of B
H I Right sub-tree of G
Representation of Trees
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of sub-trees
O O O
O
Left Child - Right Sibling
data
A
left child right sibling
B C D
E F G H I J
K L M
Tree Implementation
- Create a tree with three nodes (one root & two children)
- Insert a new node (in tree with root R, as a new child at level L)
- Delete a node (in tree with root R, the first child at level L)
[1] A
Sequential Representation [2] B
[3] C
[4] D
(1) waste space
A [1] A
[2] B (2) insertion/deletion [5] E
problem [6] F
[3] -- [7]
B G
[4] C [8]
A H
[5] -- [9]
C I
[6] --
[7] -- B C
D [8] D
[9] --
. . D E F G
E
[16] E
H I
Linked Representation
typedef struct tnode *ptnode;
typedef struct tnode {
int data;
ptnode left, right;
};
data
left data right
left right
Arithmetic Expression Using BT
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/AB CDE
* D prefix expression
postorder traversal
AB/C *D*E+
/ C
postfix expression
level order traversal
A B +*E*D /CAB
Tree for Algebraic expression
+
* /
a b - e
c d
h = O(log n)
Advantages of trees:
Preorder:
Process the root
Traverse the left subtree in preorder
Traverse the right sub-tree in preorder
Inorder:
Traverse the left subtree in inorder
Process the root
Traverse the right sub-tree in inorder
Tree Traversal(recursive)
Postorder
Traverse the left subtree in postorder
Traverse the right sub-tree in postorder
Process the root
PreOrder Traversal
Inorder Traversal
Postorder Traversal
PreOrder Traversal
P(TreeNode)
Print (TreeNode.value)
If LeftPointer(TreeNode) != NULL
Then
P(TreeNode.LeftNode)
If RightPointer(TreeNode) != NULL
Then
P(TreeNode.RightNode)
F, B, A, D, C, E, G, I, H
Inorder Traversal
P(TreeNode)
If LeftPointer(TreeNode) != NULL
Then
P(TreeNode.LeftNode)
print(TreeNode.value)
If RightPointer(TreeNode) != NULL
Then
P(TreeNode.RightNode)
A, B, C, D, E, F, G, H, I
Postorder Traversal
P(TreeNode)
If LeftPointer(TreeNode) != NULL
Then
P(TreeNode.LeftNode)
If RightPointer(TreeNode) != NULL
Then
P(TreeNode.RightNode)
Print (TreeNode.value)
A, C, E, D, B, H, I, G, F
The in-order and pre-order traversal of a binary tree are:
d b e a f c g and a b d e c f g, respectively.
What will be the post-order traversal for the same tree?
a
/ \
b c
/\ / \
d e f g
The value next to F in pre[], must be left child of root. So, F is root and A
is left child. How to find the all nodes in left subtree?
We know A is root of all nodes in left subtree. All nodes before A in post[]
must be in left subtree. Now we know F is root, elements {E,C,K,A} are in
left subtree, and the elements {H,B,G,D} are in right subtree.
recursively follow the above approach and you will get the tree.
Postorder: E C K A H B G D F Preorder: F A E K C D H G B
F
/ \
(E C K A) (H,B,G,D) *postorder
First traverse left sub tree, here A will be root. Now decide left and right
sub-tree. Apply all the previous step, you can see in pre-order (A E K C D
H G B) A is root and E will left sub-tree and (C,K) will be right side.
F
/ \
A (H,B,G,D) *postorder
/ \
E (C,K)
F
/ \
A (H,B,G,D) *postorder
/ \
E (C,K) *postorder
Traverse right sub tree, here K will be root. Now decide left and right sub-tree.
Apply all the previous step, you can see in pre-order (K C D H G B) K is root and
C will left sub-tree. F
/ \
A (H,B,G,D) *postorder
/ \
E K *postorder
/
C
Same steps will be applied for right sub-tree. When no element is
remaining, you will find final tree.
F
/ \
A D
/\/ \
E K H G
/ /
C B
Arithmetic Expression Using BT
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/AB CDE
* D prefix expression
postorder traversal
AB/C *D*E+
/ C
postfix expression
level order traversal
A B +*E*D /CAB
Inorder Traversal (recursive version)
eulerTour(node v) {
perform action for visiting node on the left;
if v is internal then
eulerTour(v->left);
perform action for visiting node from below;
if v is internal then
eulerTour(v->right);
perform action for visiting node on the right;
}
Euler Tour Traversal (contd)
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/AB CDE
* D prefix expression
postorder traversal
AB/C *D*E+
/ C
postfix expression
level order traversal
A B +*E*D /CAB
Inorder Traversal (recursive version)
Traversals
Searches
Insertion
Deletion
55
Three BST search algorithms:
56
57
58
59
60
61
62
BST Insertion
63
64
65
30 30
30 30
66
Deletion
67
Deletion from the middle of a tree
68
Deletion from the middle of a tree
69
70
(continued)
71
27 27
27 27
72