You are on page 1of 53

DATA STRUCTURES AND

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

Non-linear data structure


Finite collection of special data items called nodes
Nodes of the tree are arranged in hierarchical structures to
represent the parent-child relationship
No restriction on the number of children for a node
1
2

DEGREE of a node: Number of nodes connected to any


node
IN-DEGREE of a node:
Number of edges that ends at a
particular node
OUT-DEGREE: Number of edges that starts from a particular
node
Degree = in-degree+ out-degree
1

DEGREE of a node: Number of nodes connected to any


node
IN-DEGREE of a node:
Number of edges that ends at a
particular node
OUT-DEGREE: Number of edges that starts from a particular
node
Degree = in-degree+ out-degree
1

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

Path : 1-3, 3-7

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

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


1. There is a specially designated node called the root
2. The remaining nodes are partitioned into n>=o disjoint
sets T1, T2, Tn where each of these sets is a tree
T1, T2, Tn are called the subtrees of the root

Is this a tree ??

Is this a tree ??

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


1. There is a specially designated node called the root
2. The remaining nodes are partitioned into n>=o disjoint
sets T1, T2, Tn where each of these sets is a tree
T1, T2, Tn are called the subtrees of the root

Is this a tree ?? - NO

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


1. There is a specially designated node called the root
2. The remaining nodes are partitioned into n>=o disjoint
sets T1, T2, Tn where each of these sets is a tree
T1, T2, Tn are called the subtrees of the root

6
6

BINARY TREE: Definition

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


1. There is a specially designated node called the root
2. The remaining nodes are partitioned into N=0,1 or 2
disjoint sets T1 and T2 where each of these sets is a
tree
T1 and T2 are called the subtrees of the root
1

Terminologies
Binary TREE

a tree which is either empty or each node can have maximum 2


children
each node can have either 0 children, 1 child or 2 children
Leftchild - Node on the left of the parent
Rightchild - Node on the right of the parent
1

Terminologies
Binary TREE

a tree which is either empty or each node can have maximum 2


children
each node can have either 0 children, 1 child or 2 children
Leftchild - Node on the left of the parent
Rightchild - Node on the right of the parent
1
leftchil
d
4

rightchild

Terminologies
Binary TREE

a tree which is either empty or each node can have maximum 2


children
each node can have either 0 children, 1 child or 2 children
Leftsubtree - Tree on the left of the parent
Rightsubtree - Tree on the right of the parent
1

Terminologies
Binary TREE

a tree which is either empty or each node can have maximum 2


children
each node can have either 0 children, 1 child or 2 children
Leftsubtree - Tree on the left of the parent
Rightsubtree - Tree on the right of the parent
1
leftsubtre
e
4

Terminologies
Binary TREE

a tree which is either empty or each node can have maximum 2


children
each node can have either 0 children, 1 child or 2 children
Leftsubtree - Tree on the left of the parent
Rightsubtree - Tree on the right of the parent
1

rightsubtre
e
7

CLASSIFICATION OF BINARY TREE


1. Strictly binary tree
A binary tree in which every nonleaf node has non empty left and
right sub tree
A strictly binary tree with N leaves always contains 2N-1 nodes

CLASSIFICATION OF BINARY TREE


2. Complete binary tree
A binary tree with all levels except the last level
contains the maximum number of possible nodes and
all nodes in the last level appear as left as possible

6
Incomplete

5
complete

CLASSIFICATION OF BINARY TREE


3. Full binary tree
A binary tree that contains maximum possible number
of nodes at all levels
A tree of depth d will have 2d-1 nodes

D=3
Nodes = 23-1
=7

PROPERTIES OF BINARY TREE


1. Maximum number of nodes on level l is 2l, where l>=0

L=0, nodes = 20=1


L=1, nodes = 21=2
L=0, nodes = 22=4

PROPERTIES OF BINARY TREE


2. Maximum number of nodes possible in a binary tree of
height h is 2h-1.

H=3
nodes= 23-1 = 7

PROPERTIES OF BINARY TREE


3. Minimum number of nodes possible in a binary tree of
height h is h.

H=3
nodes= 3

REPRESENTATIIONS OF BINARY
TREE

1.
2.

Linear or sequential representation


Linked or pointer representation

REPRESENTATIIONS OF BINARY TREE


1.

Linear or sequential representation


A block of memory for an array is allocated before storing the
actual tree in it.
Once memory is allocated, the size of the tree is restricted as
permitted by the memory.
In this representation, the nodes are stored level by level,
starting from the zero level where only the root node is present.

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

REPRESENTATIIONS OF BINARY TREE


Linear or sequential representation
Following rules can be used to decide the location of any node
1. The root node is at index 0
2. For any node with index I,
1. Parent (i) is at (i 1)/2.
2. Left child of i is at 2i+1 :
3. Right child of i is at 2i + 2:

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

REPRESENTATIIONS OF BINARY TREE


Linear or sequential representation
Left child of i is at 2i+1 :
If 2i + 1 > n 1, then the node does not have a left child.
Eg: i=3 , then 2i+1 = 7, but 7>n-1 no left child
Right child of i is at 2i + 2:
If 2i + 2 > n 1, then the node does have a right child.
Eg: i=3 , then 2i+2 = 8, but 8>n-1 no right child

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

REPRESENTATIIONS OF BINARY TREE


Linear or sequential representation

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]

REPRESENTATIONS OF BINARY TREE


Linear or sequential representation
Advantages of linear representation

Any node can be accessed by calculating the index

For each node, only data is stored no need to store any


pointers
Disadvantages

Other than for full binary tree, majority of the array entries may
be empty

No way to increase the size of the tree during execution

Inserting or deleting a node is inefficient

REPRESENTATIIONS OF BINARY TREE


2. Linked or pointer representation
Tree can also be defined as a finite collection of nodes
where each node is divided into 3 parts containing
Left child address
Information (data)
Right child address
Left

info

right

BINARY TREE

root

1
root

2 x

x 4 x

x 6 x

x 7 x

Operations on binary tree


Insertion as a leaf node
Deletion of a leaf node
traversal

Binary Tree Creation


CreateBinaryTree
1. if(ROOT=NULL)
1.
2.
3.
4.

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.

Print search unsuccessfull

4. Else if(ptr->lc=NULL and ptr->rc=NULL)


1.
2.
3.
4.
5.

Read option to insert L or R


New=getnode()
New->data=item
New->lc=new->rc=NULL
If(option=L)

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

2. Else if (stack is not empty)


1. Ptr=pop()
2. Visit(ptr)
// print ptr->data
3. Ptr=ptr->rchild

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

2. Else if (stack is not empty)


1. Ptr=pop()

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

2. Else if(stack not empty)


1. Ptr=pop()
2. If(ptr->rchild!=0 and stack[top]=ptr->rchild)
1.
2.
3.

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

TYPES OF BINARY TREE


1.
2.
3.
4.
5.
6.
7.
8.
9.

Expression tree
Binary search tree
Heap tree
Threaded binary tree
Huffman tree
Height balanced tree (AVL tree)
Red black tree
Splay tree
Decision tree

You might also like