Professional Documents
Culture Documents
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
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
1 Ptr.data= Item
3 If option= Y then
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
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
1
2
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
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
2
3
4
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
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
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.
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
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