Professional Documents
Culture Documents
ALGORITHMS
MODULE 4
Trees
Directo
r
Dean
(administration
)
HOD
(cse)
Nonteaching
Teaching
F
1
F
1
HOD (ce)
F
1
NF
1
Dean
Dean
(academics)
(research)
HOD (it)
HOD
(me)
HOD (ec)
Teaching
NF
1
F
1
F
1
F
1
Nonteaching
NF
1
NF
1
TREE
Degree=1
In-degree
=1
Outdegree=0
Degree=3
In-degree =1
Out-degree=2
Terminologies
Degree of a tree:
Maximum degree of any node
1 Degree
=2
Degree
=3
Degree
=1
Degree
=1
Degree
=3
7 Degree
=1
Terminologies
Degree of a tree:
Maximum degree of any node
Degree of tree
=3
1 Degree
=2
Degree
=3
Degree
=1
Degree
=1
Degree
=3
7 Degree
=1
Terminologies
ROOT
First node which does not have any parent
Node with In-degree =0
ROOT
1
Terminologies
LEAF NODE or TERMINAL NODE
Last node of the tree which does not have any descendants
Node with out-degree= o
Terminologies
LEAF NODE or TERMINAL NODE
Last node of the tree which does not have any descendants
Node with out-degree= o
Leaf
Terminologies
PATH
Sequence of edges between any two nodes
Terminologies
LEVEL of a node
Root is always assigned a level zero
Level of any other node = Level of its parent +1
Terminologies
LEVEL of a node
Root is always assigned a level zero
Level of any other node = Level of its parent +1
Level 0
Level 1
Level 2
Level 3
Terminologies
DEPTH of a node
No. of nodes in the longest path from the root to any terminal
node
Depth of a node = level of a node +1
Depth of a tree = largest level of a tree +1
Level 0
Level 1
Level 2
Level 3
Terminologies
Siblings
Children of same parent node
pare
nt
Terminologies
Siblings
Children of same parent node
sibling
s
5
Terminologies
Siblings
Children of same parent node
1
pare
nt
4
Terminologies
Siblings
Children of same parent node
4
sibling
s
TREE: Definition
Is this a tree ??
Is this a tree ??
Is this a tree ?? - NO
6
6
Terminologies
Binary TREE
Terminologies
Binary TREE
rightchild
Terminologies
Binary TREE
Terminologies
Binary TREE
Terminologies
Binary TREE
rightsubtre
e
7
6
Incomplete
5
complete
D=3
Nodes = 23-1
=7
H=3
nodes= 23-1 = 7
H=3
nodes= 3
REPRESENTATIIONS OF BINARY
TREE
1.
2.
A[0]
A[1]
A[2]
A[3]
A[4]
A[5]
A[6]
A[0]
A[1]
A[2]
A[3]
A[4]
A[5]
A[6]
A[0]
A[1]
A[2]
A[3]
A[4]
A[5]
A[6]
5
1
0
1
1
6
1
2
1
3
1
4
A[0]
A[1]
A[2]
A[3]
A[4]
A[5]
A[6]
A[7]
A[8]
A[9]
A[10]
A[11]
11
A[12]
A[13]
A[14]
Other than for full binary tree, majority of the array entries may
be empty
info
right
BINARY TREE
root
1
root
2 x
x 4 x
x 6 x
x 7 x
New =getnode()
Read new->data
New->lc=new->rc=null
Root=new
2. Else
1. Read key and item
2. Ptr=search(root,key)
3. If(ptr=null)
1.
1. If(ptr->lc=NULL)
1. Ptr->lc=new
2. Else
1. Print insertion not possible
Search a key
search(node *ptr1,int key)
1.
ptr=ptr1, p=NULL
2.
If(ptr->data!=key)
1.
If(ptr->lchild=NULL and ptr->rchild=NULL)
1.
Return NULL
2.
End if
3.
If(ptr->lchild!=NULL)
1.
p-=search(ptr->lchild,key)
2.
If(p!=NULL)
1.
Return p
3.
End if
4.
End if
5.
If(ptr->rchild!=NULL)
1.
P=search(ptr->rchild,key)
2.
Return p
6.
End if
3.
Else
1.
Return ptr
4.
End if
5.
stop
Traversal
Visit each node in the tree exactly once.
Preorder traversal
Visit the root.
Traverse the left subtree of the root in preorder.
Traverse the right subtree of the root in preorder.
Inorder traversal
Traverse the left subtree of the root in inorder.
Visit root.
Traverse the right subtree of the root in inorder.
Postorder traversal
Traverse the left subtree of the root in postorder.
Traverse the right subtree of the root in postorder.
Visit the root.
Inorder traversal
Recursive implementation
inorder(root)
1. if (root!=NULL)
1. Inorder(root->lchild)
2. Visit root
3. Inorder(root->rchild)
2. End if
3. stop
Inorder traversal
Non-Recursive implementation
1. Ptr=root
2. While(true)
1. If(ptr!=NULL)
1. Push(ptr)
2. Ptr=ptr->lchild
3. Else exit
4. End if
3. End while
4. stop
Preorder traversal
Recursive implementation
preorder(root)
1. if (root!=NULL)
1. Visit root
// print ptr->data
2. preorder(root->lchild)
3. preorder(root->rchild)
2. End if
3. stop
Preorder traversal
Non-Recursive implementation
1. Ptr=root
2. While(true)
1. If(ptr!=NULL)
1. Visit(ptr)
// print ptr->data
2. Push(ptr->rchild)
3. Ptr=ptr->lchild
3. Else exit
4. End if
3. End while
4. stop
Postorder traversal
Recursive implementation
postorder(root)
1. if (root!=NULL)
1. postorder(root->lchild)
2. postorder(root->rchild)
3. Visit root
// print ptr->data
2. End if
3. stop
Postorder traversal
Non-Recursive implementation
1. Ptr=root
2. While(true)
1. If(ptr!=NULL)
1. Push(ptr->rchild)
2. Push(ptr)
3. Ptr=ptr->lchild
T=ptr
Ptr=pop()
Push(T)
3. Else
1.
2.
Print ptr->data
Ptr=NULL
4. End if
3. Else
1.
exit
4. End if
3. End while
4. stop
Expression tree
Binary search tree
Heap tree
Threaded binary tree
Huffman tree
Height balanced tree (AVL tree)
Red black tree
Splay tree
Decision tree