Stacks and Queues
Stacks and Queues
Level 1
Leaf
Leaf Level 2
Leaf
Binary Tree
Binary Tree
Binary Tree is a rooted tree in which
root can have maximum two children
such that each of them is again a binary
tree. That means, there can be 0,1, or 2
children of any node.
Strict Binary Tree
Strict Binary Tree
Strict Binary Tree is a Binary tree in
which root can have exactly two
children or no children at all. That
means, there can be 0 or 2 children of
any node.
Complete Binary Tree
Complete Binary Tree
Complete Binary Tree is a Strict Binary
tree in which every leaf node is at same
level. That means, there are equal
number of children in right and left
subtree for every node.
Extended Binary Tree
⚫ A binary tree with special nodes replacing every
null subtree. Every regular node has two children,
and every special node has no children.
Extended Binary Tree
⚫ An extended binary tree is a transformation of
any binary tree into a complete binary tree.
⚫ Right part -
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Constructing Binary Tree
Binary Search Tree
The value at any node,
• Greater than every value in left subtree
• Smaller than every value in right
subtree
– Example
•• Y > X
Y<Z Y
X Z
Binary Search Tree
• Values in left sub tree less than parent
• Values in right sub tree greater than parent
• Fast searches in a Binary Search tree, maximum of
log n comparisons
31
21 40
10 22 35 42
Binary Search Trees
• Examples 5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary Not a
search binary
trees search tree
Example Binary Searches
• search (root, 25 )
10 5
10 < 25, right 5 < 25, right
5 30 30 > 25, left 2 45 45 > 25, left
25 = 25, found 30 > 25, left
2 25 45 30
10 < 25, right
10 25 = 25, found
25
Algorithm for Binary Search Tree
⚫ A) compare ITEM with the root node N of the tree
⚫ i) if ITEM<N, proceed to the left child of N.
⚫ ii) if ITEM>N, proceed to the right child of N.
⚫ B) repeat step (A) until one of the following occurs
⚫ i) we meet a node N such that ITEM=N, i.e. search
is successful.
⚫ ii) we meet an empty sub tree, i.e. the search
is unsuccessful.
Binary Tree Implementation
typedef struct node
{
int data;
struct node *lc,*rc;
};
Iterative Search of Binary Search Tree
search()
{
while (n != NULL)
{
if (n->data == item) // Found it
return n;
if (n->data > item) // In left
n = n->lc; subtree
else // In right
n = n->rc; subtree
}
return null;
}
Recursive Search of Binary Search Tree
Search(node *n, info)
{
if (n == NULL) // Not found
return( n );
else if (n->data == item) // Found it
return( n );
else if (n->data > item) // In left subtree
return search( n->left, item );
else // In right
subtree
return search( n->right, item );
}
Insertion in a Binary Search Tree
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left
subtree for Y
4. If X > Y, insert new leaf X as new right
subtree for Y
Insertion in a Binary Search Tree
• Insert ( 20 )
10
30 > 20, left
5 10 <30
20, right
25 > 20, left
2 25 45 Insert 20 on
left
20
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
60
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
60
50
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
33 60
50
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
33 60
50
55
Insertion in a Binary Tree
40, 60, 50, 33, 55, 11
40
33 60
11 50
55
Deletion in Binary Tree
• Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else //we must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from
subtree
Note :-
– Deletions may unbalance tree
Example Deletion (Leaf)
• Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
Example Deletion (Internal Node)
• Delete ( 10 )
10 5 5
5 30 5 30 2 30
2 25 2 25 2 25 45
45
45
Deleting leaf
Replacing 10
with largest Replacing 5
value in left with
subtree largest
value in left
Example Deletion (Internal Node)
• Delete ( 10 )
10 25 25
5 30 5 30 5 30
2 25 2 25 45 2 45
45
Deleting leaf Resulting tree
Replacing 10
with smallest
value in
right subtree
Binary Search Properties
• Time of search
– Proportional to height of tree
– Balanced binary tree
• O( log(n) ) time
– Degenerate tree
• O( n ) time
• Like searching linked list / unsorted array
AVL Tree
⚫ AVL trees are height-balanced binary search trees
⚫ Balance factor of a node=
height(left sub tree) - height(right sub tree)
⚫ An AVL tree has balance factor calculated at every
node
⚫ For every node, heights of left and right sub tree can
differ by no more than 1
⚫ Store current heights in each node
AVL Tree
⚫ A binary tree in which the difference of height of the
right and left subtree of any node is less than or
equal to 1 is known as AVL Tree.
⚫ Height of left subtree – height of right
subtree can be either -1,0,1
AVL Tree
0 0 0
0
AVL Tree
1
-1 0
3 2
0 1 0 0
Balanced as LST-RST=1
Insertion in AVL Tree
2
3
Case 3: the node was heavy and the new node has been inserted in
the heavy sub tree thus creating an unbalanced sub tree
Rebalancing
• When the tree structure changes (e.g., insertion or
deletion), we need to transform the tree to restore
the AVL tree property.
• This is done using single rotations or double
rotations.
Rotation
s • single rotations
3
Example
Insert 3,2,1,4,5,6,7
2
Example
Insert 3,2,1,4,5,6,7
1
Example
Insert 3,2,1,4,5,6,7
Single rotation
1
Example
Insert 3,2,1,4,5,6,7
1
3
Example
Insert 3,2,1,4,5,6,7
1
3
4
Example
Insert 3,2,1,4,5,6,7
1
3
5
Example
Insert 3,2,1,4,5,6,7
2
Single rotation
1
3
5
Example
Insert 3,2,1,4,5,6,7
1
4
3 5
Example
Insert 3,2,1,4,5,6,7
1
4
3 5
6
Example
Insert 3,2,1,4,5,6,7
Single rotation
2
1
4
3 5
6
Example
Insert 3,2,1,4,5,6,7
2
5
1 3 6
Example
Insert 3,2,1,4,5,6,7
2
5
1 3 6
7
Example
Insert 3,2,1,4,5,6,7
Single rotation
2
5
1 3 6
7
Example
Insert 3,2,1,4,5,6,7
2
6
1 3 5 7
AVL Insertion: Outside Case
Consider a
valid
j
AVL subtree
k h
h
h
Z
X Y
AVL Insertion: Outside Case
j Inserting into X
destroys the AVL
property at node
k h
j
h+1 h Z
Y
X
AVL Insertion: Outside Case
j Do a “right
rotation”
k h
h+1 h Z
Y
X
Single right rotation
j Do a “right
rotation”
k h
h+1 h Z
Y
X
Outside Case Completed
h+1
j
h h
X Y Z
AVL property has been
restored!
AVL Insertion: Inside Case
Consider a
valid AVL
j
subtree
k h
h h Z
X Y
AVL Insertion: Inside Case
Inserting into
Y destroys the j Does “right rotation”
restore balance?
AVL property
at node j
k h
h h+1 Z
X
Y
AVL Insertion: Inside Case
k “Right rotation”
does not restore
h j balance… now k is
out of balance
X h+1
h
Z
Y
AVL Insertion: Inside Case
Consider the
structure of subtree j
Y…
k h
h h+1 Z
X
Y
AVL Insertion: Inside Case
Y = node i and
subtrees V and
j
W
k h
h
i h+1 Z
X h or h-
1
V W
AVL Insertion: Inside Case
j We will do a left-
right “double
rotation” . . .
k
i Z
X
V W
Double rotation : first
rotation
i
k Z
W
X V
Double rotation : second rotation
j Now do a right
rotation
i
k Z
W
X V
Double rotation : second rotation
k j
h h
h or h-
1
X V W Z
B-
B-tree is a fairly well-balanced tree.
Tree
All leaves are on the bottom level.
All internal nodes (except perhaps the root node) have at least
cell(m / 2) (nonempty) children.
The root node can have as few as 2 children if it is an internal
node, and can obviously have no children if the root node is a
leaf (that is, the whole tree consists only of the root node).
Each leaf node (other than the root node if it is a leaf) must
contain at least ceil(m / 2) - 1 keys.
Let's work our way through an example similar to that
given by Kruse. Insert the following letters into what is
originally an empty B-tree of order 5:
C N G A H E K Q M F W L T Z D P R X Y S
H E K Q M F W L T Z D P R X Y S
Inserting M requires a split. Note that M happens to be the
median key and so is moved up into the parent node.
M F W L T Z D P R X Y S
When Z is added, the rightmost leaf must be split. The median item T is moved
up into the parent node. Note that by moving up the median key, the tree is
kept fairly balanced, with 2 keys in each of the resulting nodes.
Z D P R X Y S
Finally, when S is added, the node with N, P, Q, and R splits,
sending the median Q up to the parent. However, the parent node is
full, so it splits, sending the median M up to form a new root node.
Note how the 3 pointers from the old parent node stay in the
revised node that contains D and G.
HEAP
A max tree is a tree in which the key value in each node is no
smaller than the key values in its children. A max heap is a
complete binary tree that is also a max tree.
Operations on heaps:
- creation of an empty heap
- insertion of a new element into the heap;
- deletion of the largest element from the heap
Max Heap
[1] 9
[1] 14 [1] 30
Min Heap
[1]
2 [1] 10 [1] 11
20 20 21
15 2 15 5 15 20
14 10 2 2
14 10 14 10
initial location of new node insert 5 into heap insert 21 into heap
Insertion into a Max Heap
remove
20 10 15
15 2 15 2 14 2
14 10 14 10
Deletion from a Max
Heap
element delete_max_heap(int *n)
{
int parent, child;
element item, temp;
if (HEAP_EMPTY(*n)) {
fprintf(stderr, “The heap is empty\n”);
exit(1);
}
/* save value of the element with the
highest key */
item = heap[1];
/* use last element in heap to adjust heap */
temp = heap[(*n)--];
parent = 1;
child = 2;
while (child <= *n) {
/* find the larger child of the current
parent */
if ((child < *n)&&
(heap[child].key<heap[child+1].key))
child++;
if (temp.key >= heap[child].key) break;
/* move to the next lower level */
heap[parent] = heap[child];
child *= 2;
}
heap[parent] = temp;
return item;
}