0% found this document useful (0 votes)
60 views

Lecture 3.1.4 AVL Search Trees, B, B Trees, Heap, Heap Sort.

Repeat steps 2 and 3, propagating splits up the tree as needed until reaching a node that does not become too big after the split. For example, to insert 52 into the B-tree constructed earlier: 1. Insert 52 into the leaf containing {28, 45}. This makes the leaf too big. 2. Split the leaf into {28, 52} and {45}. Promote 45 to the parent. 3. The parent now contains {14, 45, 48}. Split it into {14, 45} and {48}. Promote 45 to its parent. 4. The root now contains {1, 5, 45}. No split is needed since it has room for one more key

Uploaded by

borab25865
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Lecture 3.1.4 AVL Search Trees, B, B Trees, Heap, Heap Sort.

Repeat steps 2 and 3, propagating splits up the tree as needed until reaching a node that does not become too big after the split. For example, to insert 52 into the B-tree constructed earlier: 1. Insert 52 into the leaf containing {28, 45}. This makes the leaf too big. 2. Split the leaf into {28, 52} and {45}. Promote 45 to the parent. 3. The parent now contains {14, 45, 48}. Split it into {14, 45} and {48}. Promote 45 to its parent. 4. The root now contains {1, 5, 45}. No split is needed since it has room for one more key

Uploaded by

borab25865
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

UNIVERSITY INSTITUTE OF

ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
AND ENGG.

Bachelor of Engineering (Computer Science & Engineering)


DATA STRUCTURES 21CSH-211

TREES DISCOVER . LEARN . EMPOWER


1
AVL (Adelson-Velskii and Landis) Trees
• An AVL Tree is a binary search tree
such that for every internal node v of T,
the heights of the children of v can differ
by at most 1.

An example of an AVL tree where the heights are


shown next to the nodes:
2
AVL (Adelson-Velskii and Landis) Trees
• AVL tree is a binary search tree with balance condition
• To ensure depth of the tree is O(log(N))
• And consequently, search/insert/remove complexity bound O(log(N))
• Balance condition
• For every node in the tree, height of left and right subtree can differ by at most 1

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)

• Replace node k2 by node k1


• Set node k2 to be right child of node k1
• Set subtree Y to be left child of node k2
• Case 4 is similar

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)

• Left-right double rotation to fix case 2


• First rotate between k1 and k2
• Then rotate between k2 and k3
• Case 3 is similar
16
Example
• Continuing the previous example by inserting
• 16 down to 10, and then 8 and 9

• 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

• Other cases as exercises

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]

• The elements in the subarray A[(n/2+1) .. n]


are leaves

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]

• Min-heaps (smallest element at root), have the min-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

• Create a max-heap from an unordered array


• BUILD-MAX-HEAP

• Sort an array in place


• HEAPSORT

• 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]

Heap property restored


35
Maintaining the Heap Property
• Assumptions:
Alg: MAX-HEAPIFY(A, i, n)
• Left and Right subtrees of i
are max-heaps 1. l ← LEFT(i)
• A[i] may be smaller than its 2. r ← RIGHT(i)
children 3. if l ≤ n and A[l] > A[i]
4. then largest ←l
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest  i
9. then exchange A[i] ↔ A[largest]
10. MAX-HEAPIFY(A, largest, n)

36
MAX-HEAPIFY Running Time
• Intuitively:
- h
-
- 2h
- O(h)

• Running time of MAX-HEAPIFY is O(lgn)

• Can be written in terms of the height of the heap, as being O(h)


• Since the height of the heap is lgn

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

i=5 i=4 i=3


1 1 1

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

hi = h – i height of the heap rooted at level i


ni = 2i number of nodes at level i
41
Heapsort
• Goal:
• Sort an array using heap representations

• 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, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2)

MAX-HEAPIFY(A, 1, 1)

43
Alg: HEAPSORT(A)
1. BUILD-MAX-HEAP(A) O(n)

2. for i ← length[A] downto 2


n-1 times
3. do exchange A[1] ↔ A[i]
O(lgn)
4. MAX-HEAPIFY(A, 1, i - 1)

• Running time: O(nlgn) --- Can be shown to be


Θ(nlgn)
44
Applications of Trees
• Binary Search Tree is used to implement multilevel indexing in
database. It is also used in Huffman Coding Algorithm & to implement
searching Algorithm.
• Heap is used in Heapsort & in Dynamic memory allocation in lisp.
• Heaps is used in implementing efficient priority-queues, which in turn
are used for scheduling processes in many operating systems, Quality-
of-Service in routers, and A* (path-finding algorithm used in AI
applications, including robotics and video games).
• We use B-Tree in indexing large records in database to improve search.

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

You might also like