You are on page 1of 34

Introduction To Tree

By:
Dr. Ghulam Rasool

Tree
A Tree is a non-linear data structure. A
common example of tree is the
linage of a person that is referred to
as the family tree.
Tree is used where the hierarchy
relationship among data are to be
maintained.
Insertion, deletion, searching etc. are
more efficient in trees than linear DS.

Properties of Tree
Each tree has one node called as the root of the tree.
A unique path exists from the root node to all other
nodes in the tree.
Each node , other than the root has a unique
predecessor. It is called parent.
Each node may have no, one or several successor
nodes. Successor node is also called child
Node: It is the main component of any tree structure.
Node of a tree stores the actual data and links to
other nodes
Root Node: The unique Node in the tree that has no
parent node is called root node. It is beginning of tree
Parent Node: The node that generate links for other
nodes is called parent node

Basic about Tree


Child Node: the node that is directly connected to parent is
called child node
Sub-tree: The child node of a root node that has its own
child nodes is called sub-tree.
Terminal Node: The node having no successor or child node
is called terminal node. It is also called leaf node
Depth of a Node: Each Node has a unique path connecting it
to root. The depth of a node is the total number of nodes
between the node and the root including root node itself.
Height of a Tree: max no of nodes that is possible in a path
starting from root node to a leaf node is called the height
of a tree.
Degree: Max no of children that is possible for a node is
known as the degree of a node.

Binary Trees
A binary tree T is a finite set of
nodes, such that
Each node has at most two child
nodes called left and right.
Figure page 208

Full Binary Tree


A binary tree is a full binary tree, if it
contains maximum possible number
of nodes in all levels
Complete Binary Tree
A binary tree is said to be complete
binary tree, if all its level, except
possibly the last level have the
maximum possible number of nodes
and all the nodes at the last level
appear as far left as possible

Properties of a Binary Tree


In any binary tree, max number of nodes
on level l is 2^l
Maximum number of nodes possible
in a binary tree of height h is 2(h-1)
Minimum number of nodes possible
in a binary tree of height h is h
For any non-empty binary tree, if n is
the no of nodes and e is the no of
edges, then n= e+1
Height of a complete binary tree with
n number of nodes is [log2(n+1)]h

Algorithm build_Tree(Ptr, Item)


1 If Ptr!= NULL

1 Ptr.data= Item

2 Node Ptr has left-subtree (Y/N)?

3 If option= Y then

1 lcptr= new Node

2 Ptr.lc= lcptr

3 Build_Tree(lcptr, NewL)

4 Else

1 lcptr=NULL

2 Ptr.lc= NULL

3 Build_Tree(lcptr, NULL)

5
Node Ptr has rightsubtree (Y/N) ?

6 If option = Y

1 rcptr= new Node

2 ptr.rc= rcptr

3 Build_Tree(rcptr, NewR)

Continue

7 Else
1 rc.Ptr= NULL
2 Ptr.Rc=NULL
3 Build_Tree(Ptr, NULL)
Exit

Operations on Binary Tree


Insertion
Deletion
Traversal
Merging

Insertion into Binary Tree InsertTree(Key,Item)

1
2

ptr= Search_Link(Root, Key)


If(ptr= Null) then
1 Print Search is unsuccessful
2 Exit
3 If(ptr.LC=NULL) or (ptr.RC=NULL)
1 Read option to insert as left/right child
2 If(option=L) then
1 If(ptr.LC= NULL) then
1 p= new node
2 p.data= Item
3 p.LC=p.RC=NULL
4 ptr.LC=P

Insertion into Binary Tree


2 else
1 Print Insertion is not possible as left child
2 Exit
3 else
1 If(ptr.RC= NULL) then
1 p= new node
2 p.data= Item
3 p.LC=p.RC=NULL
4 ptr.RC=P
2 else
1 Print Insertion is not possible as left child
2 Exit
4 else
Print Key node already has child
5 Exit

Search_Link(PTR0, Key)
1 ptr= PTR0
2 If(ptr.Data!= Key)
1 If(ptr.LC!=NULL)
1 Search_Link(ptr.LC)
2 Else
1 Retrun(0)
3 1 If(ptr.RC!=NULL)
1 Search_Link(ptr.RC)
2 Else
1 Retrun(0)
3 Else
1 Return(ptr)
4 Exit

Tree Traversal Methods


Following are tree traversal methods
Pre-order
In-order
Post-order

Algorithm Pre-Order(Root)
1
Ptr= Root
2 If(Ptr != NULL)
1 Print Ptr.i
2 Pre-order(Ptr.Lc)
3 Pre-order(Ptr.Rc)
3 Exit
Algorithm IN-Order(Root)
1
Ptr= Root
4 If(Ptr != NULL)
1 In-order(Ptr.Lc)
2 Print Ptr.i
3 In-order(Ptr.Rc)
3 Exit

Algorithm Post-Order(Root)
1
2

Ptr= Root
If(Ptr != NULL)
1 Post-order(Ptr.Lc)
2 Post-order(Ptr.Rc)
3 Print Ptr.i
3 Exit

Binary Search Tree


A binary tree T is called binary search tree if
each node N of T satisfies the following
property:
The value at N is greater than every value in
left sub-tree of N and is less than every
value in the right sub-tree
Following operations are performed:
Searching for data
Inserting any data into tree
Deleting any data from tree
Traversing the tree

Binary Search Tree Creation


1

2
3
4

Root= new node


Root.data=item
Root.Lchild=Null
Root.Rchild=Null
Repeat step 3-5 until user want to create nodes
P= Root and get value of new node from user
While (P!=Null)
If P.data=item
print data already exists
Exit
ElseIf(P.data<item)
Parent=P
P=P.RChild

BST Creation
else Parent= P
P= P.LChild
5 Q= new node
Q. data= item
Q.LChild= Q.Rchild=Null
If (Parent= Null)
Parent=Q
ElseIf( Parent.data>item)
Parent.LChild=Q
Else
Parent.RChild=Q
6
Exit

Algorithm BST_Insert(Item)
1 Ptr= Root and falg= False
2 While(Ptr!= NIL) and (flag =False)
1
Case Item< ptr.Data
1 ptr1=ptr

2 ptr= ptr.Child

2
Case Item > ptr.Data

1 ptr1=ptr

2 ptr= ptr.Child

3
Case Item = ptr.Data

1 flag= True

2 write item already exist

3 exit

4 End Case 5
3. End While

Insertion in BST
4 If(Ptr = NIL)
1 P= New Node
2 P.data= item
3. P.Lchild=P.Rchild= NIL
4 If(ptr1.Data < Item)
Ptr1.Rchild= p
else
Ptr1.Lchild= P
5 Exit

Expression Tree
A Binary tree that is used to store arithmetic expressions is
called expression tree.

a+b*c*d+e

Algorithm Expression Tree()


1 Scan PSN expression from left to right until end of string
2
If (symbol = operand)
P= new node
P.data= symbol
Push(P)
Else
Ptr1= Pop()
Ptr2= Pop()
P= new node
P.data= symbol
P.Lchild= Ptr2
P.Rchild=Ptr1
Push(P)
3
Exit

Expression Tree
Write an algorithm/program that
should evaluate expression tree
Write an algorithm/program that
should search value from BST

Heap Tree
A complete binary tree is called heap
tree if it satisfies:
For each node N in H, the value at N
is greater than or equal to the value
of each of children of N
Such a tree is called Max heap tree
In Min heap any node N has the
value less than or equal to the value
of any successor of N.

Max and Min Heap

Building Heap
Start with the last internal node
Swap the current internal node with
its larger child, if necessary
Then follow the swapped node down
Continue until all internal nodes are
done

Heap Tree

Insertion in a heap

Insert value 6

Insertion

AVL Tree
A self balancing binary search tree
The heights of the two child subtrees
of any node differ by at most one
Re-balancing is required if required

Balanced and unbalanced


BST
1

4
2

3
1

4
4

12/26/03

6
3

Is this balanced?

2
1

6
7
AVL Trees - Lecture 8

7
32

Heap Tree
Write a program that should insert
and delete nodes from Heap Tree
Write a program that should sort the
data using heap tree

Quiz
Insert the following numbers into a
binary search tree in the order that
they are given and draw the resulting
tree.
87; 36; 22; 15; 56; 85; 48; 90; 72; 6
Delete 48 and draw the resulting
tree.
Delete 15 and draw the resulting tree

You might also like