Lecture 3.1.4 AVL Search Trees, B, B Trees, Heap, Heap Sort.
Lecture 3.1.4 AVL Search Trees, B, B Trees, Heap, Heap Sort.
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
AND ENGG.
3
Which is an AVL Tree?
4
Height of an AVL tree
• Theorem: The height of an AVL tree storing n keys is O(log n).
• Proof:
• Let us bound n(h), the minimum number of internal nodes of an AVL tree of height h.
• We easily see that n(0) = 1 and n(1) = 2
• For h > 2, an AVL tree of height h contains the root node, one AVL subtree of height h-1 and another
of height h-2 (at worst).
• That is, n(h) >= 1 + n(h-1) + n(h-2)
• Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So
n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction),
n(h) > 2in(h-2i)
• Solving the base case we get: n(h) > 2 h/2-1
• Taking logarithms: h < 2log n(h) +2
• Since n>=n(h), h < 2log(n)+2 and the height of an AVL tree is O(log n)
5
AVL Tree Insert and Remove
• Do binary search tree insert and remove
• The balance condition can be violated sometimes
• Do something to fix it : rotations
• After rotations, the balance of the whole tree is maintained
6
Balance Condition Violation
• If condition violated after a node insertion
• Which nodes do we need to rotate?
• Only nodes on path from insertion point to root may have their balance altered
• Rebalance the tree through rotation at the deepest node with balance violated
• The entire tree will be rebalanced
• Violation cases at node k (deepest node)
1. An insertion into left subtree of left child of k
2. An insertion into right subtree of left child of k
3. An insertion into left subtree of right child of k
4. An insertion into right subtree of right child of k
• Cases 1 and 4 equivalent
• Single rotation to rebalance
• Cases 2 and 3 equivalent
• Double rotation to rebalance
7
AVL Trees Complexity
• Overhead
• Extra space for maintaining height information at each node
• Insertion and deletion become more complicated, but still O(log N)
• Advantage
• Worst case O(log(N)) for insert, delete, and search
8
Single Rotation (Case 1)
9
9
Example
• After inserting 6
• Balance condition at node 8 is violated
10
Single Rotation (Case 1)
11
Example
• Inserting 3, 2, 1, and then 4 to 7 sequentially into empty AVL tree
3
2
2
1 3
1
12
Example (Cont’d)
2
• Inserting 4
1 3
2 2
• Inserting 5
1 3 4
1
4 5
3
5
13
Example (Cont’d)
4
2
• Inserting 6 2 5
1 4
1 3 6
3 5
4
4
• Inserting 7
2 6
2 5
1 3 5 7
1 3 6
7
14
Single Rotation Will Not Work for the Other Case
• For case 2
• After single rotation, k1 still not balanced
• Double rotations needed for case 2 and case 3
15
15
Double Rotation (Case 2)
• Inserting 16 and 15
4
4
2 6
2 6
1 3 5 15
1 3 5 7
7 16
16
15
17
17
Example (Cont’d)
• Inserting 14
4
4
2 7
2 6
1 3 6 15
1 3 5 15
14 16
7 16 5
14
18
18
Double Rotation (Case 2)
19
Summary
Violation cases at node k (deepest node)
1. An insertion into left subtree of left child of k
2. An insertion into right subtree of left child of k
3. An insertion into left subtree of right child of k
4. An insertion into right subtree of right child of k
Case 1
Case 2
Case 4?
Case 3
20
B-Tree
A B-Tree of order m is an m-way tree(i.e. a tree where each node may have up to m children) in which:
The number of keys in each non-leaf node is one less than the number of its children.
All leaves are on the same level.
All non-leaf(internal) nodes and external nodes (leaf) except the root have at least Гm/2˥ children or in other words Гm/2˥ -1 keys.
Suppose m=5.
•Number of keys in internal node is 1 less than its number of children.
•All internal node except the root have at least Г5/2˥ = 3 children (or at least 2 keys)
•A leaf node contains not more than 4 keys.
•Leaf node must contain at least Г5/2˥ - 1 = 2 keys.
21
Constructing a B-Tree
Suppose we start with an empty B-tree and keys arrive in the following order:
1, 12, 8, 2, 25, 5, 14, 28, 17, 7, 52, 16, 48, 68, 3, 26, 29, 53, 55, 45
Construct a B-tree of order 5. That means each node can have upto 5 childrens.
22
Constructing a B-Tree(contd..)
23
Constructing a B-Tree(contd..)
24
Constructing a B-Tree(contd..)
25
Inserting into a B-Tree
1. Attempt to insert the new key into a leaf.
2. If this would result in that leaf becoming too big, split the leaf into 2, promoting the middle key to the leaf’s parent.
3. If this would result in the parent becoming too big, split the parent into two, promoting the middle key.
4. This strategy must have to be repeated all the way to the top.
5. If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher.
26
The Heap Data Structure
• Def: A heap is a nearly complete binary tree with the following two
properties:
• Structural property: all levels are full, except possibly the last one, which
is filled from left to right
• Order (heap) property: for any node x
Parent(x) ≥ x
From the heap property, it follows that:
“The root is the maximum
8 element of the heap!”
7 4
5 2 A heap is a binary tree that is filled in order
Heap
29
Array Representation of Heaps
• A heap can be stored as an array A.
• Root of tree is A[1]
• Left child of A[i] = A[2i]
• Right child of A[i] = A[2i + 1]
• Parent of A[i] = A[ i/2 ]
• Heapsize[A] ≤ length[A]
30
Heap Types
• Max-heaps (largest element at root), have the max-heap property:
• for all nodes i, excluding the root:
A[PARENT(i)] ≥ A[i]
31
Adding/Deleting Nodes
• New nodes are always inserted at the bottom level (left to right)
• Nodes are removed from the bottom level (right to left)
32
Operations on Heaps
• Maintain/Restore the max-heap property
• MAX-HEAPIFY
• Priority queues
33
Maintaining the Heap Property
• Suppose a node is smaller than a child
• Left and Right subtrees of i are max-heaps
• To eliminate the violation:
• Exchange with larger child
• Move down the tree
• Continue until node is not smaller than children
34
Example
MAX-HEAPIFY(A, 2, 10)
A[2] A[4]
A[2] violates the heap property A[4] violates the heap property
A[4] A[9]
36
MAX-HEAPIFY Running Time
• Intuitively:
- h
-
- 2h
- O(h)
37
Building a Heap
• Convert an array A[1 … n] into a max-heap (n = length[A])
• The elements in the subarray A[(n/2+1) .. n] are leaves
• Apply MAX-HEAPIFY on elements between 1 and n/2
Alg: BUILD-MAX-HEAP(A) 1
4
1. n = length[A]
2 3
1 3
2. for i ← n/2 downto 1 4 5 6 7
2 16 9 10
3. do MAX-HEAPIFY(A, i, n) 8 9 10
14 8 7
A: 4 1 3 2 16 9 10 14 8 7
38
Example: A 4 1 3 2 16 9 10 14 8 7
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
8
2 9 10
16 9 10 8
2 9 10
16 9 10 8
14 9 10
16 9 10
14 8 7 14 8 7 2 8 7
i=2 i=1
1 1 1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
8
14 9 10
16 9 3 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
39
Running Time of BUILD MAX HEAP
Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
O(n)
2. for i ← n/2 down to 1 O(lgn)
3. do MAX-HEAPIFY(A, i, n)
Running time: O(nlgn)
• This is not an asymptotically tight upper bound
40
Running Time of BUILD MAX HEAP
• HEAPIFY takes O(h) the cost of HEAPIFY on a node i is proportional to the height of the
node i in the tree
h h
T ( n) ni hi 2i h i O (n)
Height i 0 i 0 Level No. of nodes
h0 = 3 (lgn) i=0 20
h1 = 2 i=1 21
h2 = 1 i=2 22
h3 = 0 i = 3 (lgn) 23
• Idea:
• Build a max-heap from the array
• Swap the root (the maximum element) with the last element in the array
• “Discard” this last node by decreasing the heap size
• Call MAX-HEAPIFY on the new root
• Repeat this process until only one node remains
42
Example: A=[7, 4, 3, 1, 2]
MAX-HEAPIFY(A, 1, 1)
43
Alg: HEAPSORT(A)
1. BUILD-MAX-HEAP(A) O(n)
45
Advanced Topics on Trees
• Segment Tree
• Binary Indexed Tree
• Suffix Array and Tree
• Self-Balancing BSTs
• K Dimensional Tree
• n-ary Tree
46
References
1. Li`pschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
2. Data structure and algorithm by Narasimha Karumanchi.
3. www.tutorialspoint.com
4. www.geeksforgeeks.com
47
Books Recommended
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and
Algorithms in C++”, Wiley Student Edition.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data Structures and Algorithms”,
Addison Wesley
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall
of India
THANK YOU
49