Title: Data Structures with C Using STL
1Data Structures with CUsing STL
2A complete binary tree
- A complete binary tree is a binary tree that is
either full or full through the next-to-last
level, with the leaves on the last level as far
to the left as possible. - SHAPE OF A COMPLETE BINARY TREE
3What is a Heap?
- A heap is a binary tree that satisfies these
special SHAPE and ORDER properties - Its shape must be a complete binary tree.
- For each node in the heap, the value stored in
that node is greater than or equal to the value
in each of its children. (Maximum Heap) - or
- For each node in the heap, the value stored in
that node is less than or equal to the value in
each of its children. (Minimum Heap)
4Are these both heaps?
treePtr
treePtr
50
C
30
20
A
T
10
18
5Is this a heap?
treePtr
12
60
40
30
8
10
6Where is the largest element in a maximum heap
found?
treePtr
12
60
40
30
8
7We can number the nodes left to right by level
this way
treeptr
12 2
60 1
40 3
30 4
8 5
8And use the numbers as array indexes to store the
tree
tree.nodes
treePtr
0 1 2 3 4 5 6
70 60 12 40 30 8
70 0
12 2
60 1
40 3
30 4
8 5
9Finding children of node P
tree.nodes
LC P2 1
RC (P1)2
0 1 2 3 4 5 6
70 60 12 40 30 8
treePtr
70 0
12 2
60 1
40 3
30 4
8 5
10Finding parent of node C
tree.nodes
P (C-1)/2
0 1 2 3 4 5 6
70 60 12 40 30 8
treePtr
70 0
12 2
60 1
40 3
30 4
8 5
11// HEAP SPECIFICATION // Assumes ItemType
is either a built-in simple data type // or a
class with overloaded relational
operators. templatelt class ItemType gt class
HeapType public void FilterDown (
int root , int bottom ) void
FilterUp ( int root, int bottom )
private ItemType elements //
Dynamically allocated array int
numElements
12ReheapDown
// IMPLEMENTATION OF RECURSIVE HEAP MEMBER
FUNCTIONS // Maximum heap assumed templatelt
class ItemType gt void HeapTypeltItemTypegtFilte
rDown ( int root, int bottom ) // Pre root
is the index of the node that may violate the
heap // order property // Post Heap
order property is restored between root and
bottom int maxChild int
rightChild int leftChild
leftChild root 2 1 rightChild
(root 1) 2
13 if ( leftChild lt bottom ) //
FilterDown continued if ( leftChild
bottom ) maxChild leftChld else
if (elements leftChild lt elements
rightChild ) maxChild rightChild
else maxChild leftChild if (
elements root lt elements maxChild )
Swap ( elements root , elements maxChild
) FilterDown ( maxChild, bottom )
14// IMPLEMENTATION continued templatelt class
ItemType gt void HeapTypeltItemTypegtFilterUp (
int root, int bottom ) // Pre bottom is the
index of the node that may violate the heap //
order property. The order property is
satisfied from root to // next-to-last node. //
Post Heap order property is restored between
root and bottom int parent if
( bottom gt root ) parent ( bottom - 1
) / 2 if ( elements parent lt elements
bottom ) Swap ( elements parent ,
elements bottom ) FilterUp ( root, parent
)
15Insertion into a heap
- Add the new element at the end of the array.
- FilterUp the new node.
- Increase size of heap
For Example
16Example of inserting a value into a heap
tree.nodes
treePtr
0 1 2 3 4 5 6
70 60 12 40 30 8 49
70 0
12 2
60 1
40 3
49 6
30 4
8 5
Compare with parent and swap
17Example of inserting a value into a heap
tree.nodes
treePtr
0 1 2 3 4 5 6
70 60 49 40 30 8 12
70 0
49 2
60 1
40 3
12 6
30 4
8 5
Compare with parent
18Deletion into a heap
- Swap last element in the heap array into spot
where item is being deleted. - Decrement the size of the heap.
- Then from that element FilterDown.
For Example
19Example of deleting a value into a heap
Delete 60
tree.nodes
treePtr
0 1 2 3 4 5 6
70 60 12 40 30 8
70 0
12 2
60 1
40 3
30 4
8 5
Swap tree.nodes1 and tree.nodes5
20Example of deleting a value into a heap
Delete 60
tree.nodes
treePtr
0 1 2 3 4 5 6
70 8 12 40 30 60
70 0
12 2
8 1
40 3
30 4
60 5
Decrement size
21Example of deleting a value into a heap
Delete 60
tree.nodes
treePtr
0 1 2 3 4 5 6
70 8 12 40 30
70 0
12 2
8 1
40 3
30 4
FilterDown by selecting larger child If larger
than parent swap.
22Example of deleting a value into a heap
Delete 60
tree.nodes
treePtr
0 1 2 3 4 5 6
70 40 12 8 30
70 0
12 2
40 1
8 3
30 4
Repeat until at leaf or larger child is smaller
23Heapify an array
- To make a heap out of an existing array
- For every non-leaf node starting with the one
with the largest subscript to 0 - FilterDown that node.
24An Example
treePtr
39
45
7
29
16
68
72
25An Example
treePtr
39
45
72
29
68
7
16
26An Example
treePtr
39
68
72
29
45
7
16
27An Example
treePtr
72
68
39
29
45
7
16
28AVL trees
- An AVL tree is a balanced binary search tree,
named for the authors of the algorithms for AVL
trees Adelson-Velskii and Landis. - Balanced means the the depth of the left subtree
is no more than one larger than the depth of the
right subtree for all nodes in the tree. - Tree must be checked for balance when nodes are
inserted or deleted.
29AVL trees
- AVL trees can be implemented using inheritance
based on the binary search tree. - The AVL tree node includes the balance factor for
that tree. - The balance factor is the difference between the
depth of the left subtree and the right subtree.
30The BSTreeNode Class
- // BSTreeNode class definition
- template ltclass Tgt
- class BSTreeNode
- protected
- // pointers to left and right subtrees
- BSTreeNodeltTgt left
- BSTreeNodeltTgt right
- public
- // public member allowing client to update its
value - T data
31Continuing with public methods
- // constructors
- BSTreeNode (const T item, BSTreeNodeltTgt
lptrNULL, - BSTreeNodeltTgt rptrNULL)
- // access methods for the pointer fields
- BSTreeNodeltTgt Left(void) const
- BSTreeNodeltTgt Right(void) const
- void SetLeft(BSTreeNodeltTgt lptr)
- void SetRight(BSTreeNodeltTgt rptr)
-
-
32Definition of AVLTreeNode
- // inherits the BSTreeNode class
- template ltclass Tgt
- class AVLTreeNodepublic BSTreeNodeltTgt
- private
- int balanceFactor // difference of depths of
subtrees - public
- // constructor
- AVLTreeNode(const T item, AVLTreeNodeltTgt
lptrNULL, - AVLTreeNodeltTgt rptrNULL)
-
33Continue AVLTreeNode definition
- // access methods
- AVLTreeNodeltTgt Left(void)const
- AVLTreeNodeltTgt Right(void)const
- void SetLeft(AVLTreeNodeltTgt lptr)
- void SetRight(AVLTreeNodeltTgt rptr)
- int GetBalanceFactor(void)
34Example of AVL Tree
350
291
560
100
710
430
35Where insert makes tree unbalanced
- 4 cases
- an insertion to the left subtree of the left
child of a node. - an insertion to the right subtree of the left
child of a node. - an insertion to the left subtree of the right
child of a node - an insertion to the right subtree of the right
child of a node.
36Case 1 needs a single Right Rotation
After insert of 8
Before insert of 8
350
291
560
710
100
430
37Case 1 needs a single Right Rotation
GP
351
Before
560
P
292
LC
101
430
710
80
P becomes right child of LC, LC gets promoted and
becomes left child of GP, all nodes involved have
balanceFactor set to 0.
38Case 4 needs a single Left Rotation
Before insert of 89
350
290
56-1
320
710
100
39Case 4 needs a single Left Rotation
GP
350
Before
290
56-2
P
RC
100
71-1
320
890
P becomes left child of RC, RC gets promoted and
becomes right child of GP, all nodes involved
set balanceFactor to 0.
40Add 4 and adjust balance factors
351
560
291
430
710
320
100
80
120
41Case 1 a single right rotation
351
560
292
430
710
320
101
81
120
This time when we bring P down we attach the
right child of LC as the LC of P
40
42Case 1 a single right rotation
351
560
100
81
430
710
290
40
120
320
43Insert 14 and adjust balanceFactors
351
560
291
430
710
320
100
80
180
44Insert 14 and adjust balanceFactors
351
560
292
430
710
320
101
What happens when you perform a single right
rotation with 29 as P?
80
181
140
45Insert 14 and adjust balanceFactors
351
10-2
560
430
710
291
80
181
320
This doesn't fix the tree!
140
46Case 2 requires a double rotation
k2
k3
k1
k1
k3
k2
A
D
B
C
A
D
B
C
47Apply Double Rotation
351
560
292
k3
430
710
k1
320
101
D
80
181
A
k2
140
C
B
48Apply Double Rotation
351
560
180
k2
430
710
k1
290
k3
100
140
80
320
A
C
B
D
49Case 3 requires Double rotation
k2
k1
k3
k1
k3
A
k2
D
B
C
A
D
B
C
50Case 3 Mirror Image of Case 2
35-1
k1
56-2
181
290
100
712
430
k3
A
D
80
140
62-1
k2
680
B
C
51Case 3 Mirror Image of Case 2
350
k2
620
181
k1
290
100
561
711
k3
80
140
430
680
B
C
D
A
52Practice with AVL tree insertions
- Build an AVL tree with the following values
- 2, 1, 4, 5, 9, 3, 6, 7
53Building the tree
2
Single Rotate Left
54Continue building tree
Rebalancing needed - double rotation
55Continue building the AVL tree
Rebalancing needed - double rotation
56The Final AVL tree
4
2
6
1
3
9
5
7
57Deletions to AVL Tree
- Deletions are done in the same manner as with the
Binary Search Tree, except we must again check
balance of resulting tree. - Adjustments to tree which is out of balance are
done in the same manner as they were with
insertions.