You are on page 1of 40

Algorithms 

& Data structure II 

Dr. Harish Kumar
Dr. Harish Kumar
Associate Professor
BINARY TREES
 TREES, BINARY TREES AND BINARY SEARCH TREES
 IMPLEMENTING BINARY TREES
IMPLEMENTING BINARY TREES
 SEARCHING BINARY SEARCH TREES
 TREE TRAVERSAL
 BREADTH‐FIRST TRAVERSAL
 DEPTH‐FIRST TRAVERSAL
 INSERTION
 DELETION
 AVL TREES
AVL TREES
 HEAPS
TREES
TREES
A tree can be defined recursively as the following:
1. Am empty structure is an empty tree
2. If t1, t2, …, tk are disjoint trees, then the structure whose root has its children the 
roots of t1, t2, …, tk is also a tree
3. Only structures generated by rules 1 and 2 are trees.
TREES
Path : Each node has to be reachable from the root through a unique sequence of 
arcs.

Length of the path: The number of arcs in a path.

Level : The Level of a node is the length of the path from the root to the node plus 1, 
which is the number of nodes in the path.

Height : It is the maximum level of a node in the tree.

Height=
maximum
level=4

path has
length = 3

Level=3+1=4

Fig : Examples of Tree


TREES

There is a unique path between any two nodes in a tree. A tree has no cycles.

Consider a linked list of n elements. To locate an element, the search has to start 
from the beginning of the list and the list must be scanned until the element is 
found or the end of list is reached. (Best case: O(1), Worst case: O(n))
Orderly Tree 
orderly tree : A tree where all elements are stored according to some predetermined 
criterion of ordering, 
If all the elements are stored in an orderly tree the number of tests can be reduced.

Transforming a linked List (a) into a tree (b)


BINARY TREES
BINARY TREES
Complete Binary Tree : It is a binary tree with all nodes at all levels except the last had two 
children. All non‐terminal nodes have both their children, and all leaves are at the same 
level.
There would be 20 node at level 1 (root), 21 nodes at level 2, and generally, 2i nodes at 
level i+1. 

A decision tree : It is a binary tree in which all nodes have either zero or two non‐empty 
children.

Example: sort a1, a2, a3 yes a1<a2 no


a1<a3
1< 3 a2<a3
2< 3 no
yes yes
no
a2<a3
a3<a1<a2 a1<a3 a3<a2<a1
yes no yes no
a1<a2<a3 a1<a3<a2 a2<a1<a3 a2<a3<a1
BINARY SEARCH TREES (BST)

For example,
example to locate 31:
To locate 28: found

Not found
Construct a Binary Search Tree (BST), Given a Sequence
100, 90, 110, 80, 95, 120, 50, 130, 65, 150, 165, 25, 45, 60, 145 ‐‐ Sequence
First element in the sequence = root of the BST

100
90 110
80 95 120
50 130
25 65
150

45 60 165
145
IMPLEMENTING BINARY TREES.
Binary trees can be implemented as arrays and as linked structures.
GENERIC BINARY SEARCH TREE 
In the new Implementation, a node is a
instance of a class composed of an  information field and two reference fields. 
This node is used and operated on by methods. In another class that pertains to 
the tree as a whole.
SEARCHING BINARY TREES
For every node, compare the key to be located with the value stored  in   
the node currently referred.
If the key is less than the value, go to the left sub tree and try again.
If the key is less than the value, go to the left sub tree and try again.
If it is greater than that value, try the right sub tree.
If it is same the search can be discontinued.
Search can be aborted, if there is no way to go, means that the key is 
, y g , y
not in the tree

A Function for searching Binary search tree
SEARCHING BINARY TREES
The complexity of searching is measured by the number of comparisons performed 
during the searching process. This number depends on the number of nodes encountered 
on the unique path leading from the root to the node being searched. 
on the unique path leading from the root to the node being searched.
The complexity is the length of the path leading to this node plus 1. It depends on the 
shape of the tree and the position of the node in the tree.

Search # of internal
number comparisons path length
13 1 0
10 2 1
2 3 2
12 3 2
25 2 1
20 3 2
31 3 2
29 4 3
O(# of comparisons)=O(internal path length + 1)
SEARCHING BINARY TREES
The tree contains nodes 1 through n. If  i is the root, then its left subtreee has i‐1 
nodes, and its right sub tree has n‐i nodes. If pathi‐1 and pathn‐i are average paths 
in these sub trees then the average path of this tree is
in these sub trees, then the average path of this tree is

The root of the tree can be any number i, 1<=i<=n.Therefore, the average path 
of an average tree is obtained by averaging all values of path
f b db ll l f hn(i) over all values 
() ll l
of i.
SEARCHING BINARY TREES
The tree contains nodes 1 through n. If  i is the root, then its left subtreee has i‐1 
nodes, and its right sub tree has n‐i nodes. If pathi‐1 and pathn‐i are average paths 
in these sub trees then the average path of this tree is
in these sub trees, then the average path of this tree is

The root of the tree can be any number i, 1<=i<=n.Therefore, the average path 
of an average tree is obtained by averaging all values of path
f b db ll l f hn(i) over all values 
() ll l
of i.
TREE TRAVERSAL
Tree traversal is the process of visiting each node in the tree exactly one time. 

BREADTH‐FIRST TRAVERSAL.
Breadth‐first traversal is visiting each node starting from the root and moving down 
level by level, visiting nodes on each level from left to right.

Breadth-first traversal sequence:


13, 10, 25, 2, 12, 20, 31, 29
BREADTH‐FIRST TRAVERSAL
Implementation of breadth‐first traversal is quite straightforward when a queue is 
used. After a node is visited, its children, if any, are placed at the end of the queue, 
and the node at the beginning of the queue is visited.
and the node at the beginning of the queue is visited.
Note : enqueue means inserting an element into the queue
dequeue means deleting an element from the queue
DEPTH‐FIRST TRAVERSAL.
Depth‐first traversal proceeds as far as possible to the left (or right), then backs up 
until the first crossroad, goes one step to the right (or left), and again as far as 
possible to the left (or right). Repeat this process until all nodes are visited.

There are three tasks of interest in this type of traversal:
V: visiting a node
L: traversal the left subtree
R: traversal the right subtree

An orderly traversal takes place if these tasks are performed in the same order 
for each node. So there are six possible traversals:
VLR, VRL, LVR, RVL, LRV, RLV
DEPTH‐FIRST TRAVERSAL

VLR: 13,10,2,12,25,20,31,29
VRL: 13 25,31,29,20,10,12,2
LVR: 2,10,12,13,20,25,29,31
RVL: 31,29,25,20,13,12,10,2
LRV: 2,12,10,20,29,31,25,13
RLV: 29,31,20,25,12,2,10,13

Assume the move is always from left to right, then there are three standard 
traversals:
VLR: preorder (binary) tree traversal
VLR: preorder (binary) tree traversal
LVR: inorder tree traversal
LRV: postorder tree traversal
DEPTH‐FIRST TRAVERSAL
DEPTH‐FIRST TRAVERSAL
INSERTION
 If the value of the new element is equal to the value of the current node, 
perform no action.
f ti
 If the value of the new element is less than the value of the current node, 
proceed  to the left subtree (left child) of the node. If null, insert the node 
as a left child
as a left child  the parent
the parent.
 If the value of the new element is greater than the value of the current node, 
proceed to the right subtree (right child) of the node. If null, insert the node 
as a left child of the parent
as a  left child of the parent. 
DEPTH‐FIRST TRAVERSAL ‐ INSERTION
DEPTH‐FIRST TRAVERSAL ‐ INSERTION

SEARCHING BINARY TREES
Ex 1 : Insertion
DEPTH‐FIRST TRAVERSAL ‐ INSERTION

Ex 2 : Insertion
DEPTH‐FIRST TRAVERSAL ‐ DELETION
Three situations need to be considered while deleting a node from BST: 
• A leaf node : The appropriate reference of its parent is set to null and the 
space occupied by the deleted node is later claimed by garbage collector
space occupied by the deleted node is later claimed by garbage collector
DEPTH‐FIRST TRAVERSAL ‐ DELETION
• A node with one child : The parent’s reference to the node is reset to refer to the 
node’s child. The node’s children are lifted up by one level

• A node with two children: In this case, no one‐step operation can be performed
since the parent’s right or left pointer cannot point to both children at the
same time. so this can be done by Deletion by merging or Deletion by Copying
DELETION BY MERGING
• The root of the left subtree is made as the root of the tree
• The root of the right subtree is made as the root of the tree
DELETION BY MERGING

30
20 40
10
5 11
12
DELETION BY COPYING
• Copy the minimum key in the right subtree of x to the node x, then delete the         
node with this minimum key

Example: DELETE 7

7 8
2 15 2 15
1 4 8 40 1 4 9 40
3 6 9 3 6
5 5
DELETION BY COPYING
• Copy the maximum key in the left subtree of x to the node x, then delete the       
node with this maximum key.

Example: DELETE 7

7 6

2 15 2 15

1 4 8 40 1 4 9 40

3 6 9 3 5

5
AVL TREES (ADEL’SON‐VEL’SKILL & LANDIS)
An AVL tree (originally called an admissible tree) is a tree in which the height of left 
and right subtrees of every node differ by at most one

For an AVL tree, all


balance factors should
be +1, 0, or -1.
AVL TREES (ADEL’SON‐VEL’SKILL & LANDIS)
• The left subtree and the right subtree are themselves AVL trees 
• A node is said to be 
left‐high if the left subtree has greater height 
right‐high if the right subtree has greater height 
equal if the heights of the LST and RST are the same

EXAMPLES OF AVL TREE


AVL TREES (ADEL’SON‐VEL’SKILL & LANDIS)
BALANCING OF AVL TREE

After inserting (a) 60; (b) 50; and (c) 20 into an initially empty binary search tree, the 
tree is not balanced; d) a corresponding AVL tree rotates its nodes to restore balance
tree is not balanced; d) a corresponding AVL tree rotates its nodes to restore balance. 
AVL TREES (ADEL’SON‐VEL’SKILL & LANDIS)

(a) Adding 80 to tree in the previous slide (d) does not change the balance of the 
tree; (b) a subsequent addition of 90 makes tree unbalanced; (c) left rotation
tree; (b) a subsequent addition of 90 makes tree unbalanced; (c) left rotation 
restores balance. 
HEAPS
A particular kind of binary tree, called heaps, has following properties: 
• The value of each node is not less than the values stored in each of its children.
• Tree is perfectly balanced, and the leaves in the last level are all in the left most
position.
• Every subtree is a heap.

There are two types of heaps: 
Minheap :The value of each node is less than the value of its children i.e. root 
contains the smallest element
contains the smallest element 
Maxheap :The value of each node is greater than the value of its children i.e. root 
contains the largest element 
HEAPS

Fig a are all heaps, Fig b violate the first property, Fig c violate the second .
INSERTING AN ITEM INTO A MIN HEAP 
• Insert the new element in the next position at the bottom of the heap. 
• While new item is not at the root and new item is smaller than its parent 
• Swap the new item with its parent, moving the new item up
REMOVING AN ITEM FROM A  HEAP 
• Remove the item in the root node by replacing it with the last item in the heap 
(LIH). 
• While item LIH has children and LIH is larger than either of its children 
• Swap LIH with its smaller child, moving LIH down the heap. 

You might also like