O(lg n) Search Tree
-
Upload
brewster-neumann -
Category
Documents
-
view
50 -
download
0
description
Transcript of O(lg n) Search Tree
![Page 1: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/1.jpg)
O(lg n) Search Tree
Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1
No function (except transverse) takes more than O(lg n) in the worst case.
Functions:createEmptyTree() returns a newly created empty binary treedelete(T, p) removes the node pointed to by p from the tree Tinsert(T, x) returns T with x added in the proper location search(T, key) returns a pointer to the node in T that has a key that
matches key returns null if x is not found traverse(T) prints the contents of T in orderisEmptyTree(T) returns true if T is empty and false if it is not
![Page 2: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/2.jpg)
Homework 5
• Describe how to implement a search tree that has a worst time search, insert, and delete time of no more than O(lg n). This tree should have no number of element limit.
• Do the six Search Tree functions.• Discuss how you would implement this if there
was a maximum to the number of elements.
![Page 3: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/3.jpg)
AVL Tree
54
30
25 7910
5 60
86
72
84
210
+1
+1
0
-1
0
![Page 4: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/4.jpg)
AVL Tree
• The five functions are the same.
• Except that the tree needs to be rebalanced after insertion or deletion.
• Keep track of the path used to insert/delete.
• Balance starting at the parent of the leaf inserted or deleted.
• Work up to the root.
![Page 5: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/5.jpg)
Insertion Case 1
0
![Page 6: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/6.jpg)
Insertion Case 1
-1
![Page 7: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/7.jpg)
Insertion Case 2
+1
![Page 8: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/8.jpg)
Insertion Case 2
0
![Page 9: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/9.jpg)
Insertion Case 3
+1
![Page 10: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/10.jpg)
Insertion Case 3
+2
![Page 11: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/11.jpg)
Deletion Case 1
-1
![Page 12: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/12.jpg)
Deletion Case 1
0
![Page 13: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/13.jpg)
Deletion Case 2
0
![Page 14: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/14.jpg)
Deletion Case 2
+1
![Page 15: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/15.jpg)
Deletion Case 3
+1
![Page 16: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/16.jpg)
Deletion Case 3
+2
![Page 17: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/17.jpg)
Single Rotation
+2
+1
![Page 18: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/18.jpg)
Double Rotation
+2
-1
![Page 19: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/19.jpg)
Single Rotation
+2
+1
![Page 20: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/20.jpg)
Single Rotation
+2
+1
A
B
![Page 21: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/21.jpg)
Single RotationA B
![Page 22: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/22.jpg)
Single RotationA B
+2 +1
![Page 23: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/23.jpg)
Single Rotation
0 0
A B
![Page 24: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/24.jpg)
Single Rotation
+2
+1
![Page 25: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/25.jpg)
Single Rotation
0
+1
![Page 26: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/26.jpg)
Single Rotation
0 +1
![Page 27: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/27.jpg)
Single Rotation
0
+1
![Page 28: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/28.jpg)
Single Rotation
0
+1
![Page 29: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/29.jpg)
Single Rotation
0
0
![Page 30: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/30.jpg)
Double Rotation
+2
-1
![Page 31: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/31.jpg)
Double Rotation
+1
0
0
![Page 32: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/32.jpg)
Double Rotation1
+2
-1
-1
A
B
C
![Page 33: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/33.jpg)
Double Rotation1
+2 -1-1
A B C
![Page 34: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/34.jpg)
Double Rotation1
+2
-1
-1
A
B
C
![Page 35: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/35.jpg)
Double Rotation1
+2
-1
-1
A
B
C
![Page 36: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/36.jpg)
Double Rotation1
+1
+1
-1
A
B
C
![Page 37: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/37.jpg)
Double Rotation1
+1
+1-1
A
B C
![Page 38: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/38.jpg)
Double Rotation1
+1
+1-1
A
B C
![Page 39: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/39.jpg)
Double Rotation1
+1
+1
-1
A
B
C
![Page 40: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/40.jpg)
Double Rotation1
+2
+1
+1
A
B
C
![Page 41: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/41.jpg)
Double Rotation1
0
+1
+1
A
B
C
![Page 42: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/42.jpg)
Double Rotation1
0
+1
+1
A B
C
![Page 43: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/43.jpg)
Double Rotation1
0
+1
+1
A
B
C
![Page 44: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/44.jpg)
Double Rotation1
0
+1
0
A
B
C
![Page 45: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/45.jpg)
Double Rotation1
0+1
0
A
B
C
![Page 46: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/46.jpg)
Double Rotation2
+2
-1
+1
A
B
C
![Page 47: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/47.jpg)
Double Rotation2
+2
-1
+1
A
B
C
![Page 48: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/48.jpg)
Double Rotation2
+2
0
+1
A
B
C
![Page 49: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/49.jpg)
Double Rotation2
+2
0+1
A
B C
![Page 50: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/50.jpg)
Double Rotation2
+2
0
+2
A
B
C
![Page 51: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/51.jpg)
Double Rotation2
-1
0
+2
A
B
C
![Page 52: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/52.jpg)
Double Rotation2
-1
0
+2
A B
C
![Page 53: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/53.jpg)
Double Rotation2
-1
0
0
A
B
C
![Page 54: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/54.jpg)
Double Rotation2
-10
0
A
B
C
![Page 55: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/55.jpg)
Binary Search Tree -- Array
10 112 3 4 5 6 7 8 9 10
2 * i + 1 is the left child2 * i + 2 is the right child
![Page 56: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/56.jpg)
Binary Search Tree -- Array
10 112 3 4 5 6 7 8 9 10
2 * i + 1 is the left child2 * i + 2 is the right child
![Page 57: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/57.jpg)
Binary Search Tree -- Array
10 112 3 4 5 6 7 8 9 10
2 * i + 1 is the left child2 * i + 2 is the right child
![Page 58: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/58.jpg)
Binary Search Tree -- Array
• Advantages– fast– can access the parent from a child
• Disadvantages– fixed size– standard AVL rotates greater than O(lg n)
![Page 59: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/59.jpg)
Priority Queue
A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1
Functions:
createEmptySet() returns a newly created empty priority queue
findMin(S) returns the minimum node with respect to ordering
insert(S, x) returns S with x added
deleteMin(S) returns S with the minimum node removed
isEmptySet(S) returns true if S is empty and false if it is not
![Page 60: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/60.jpg)
Homework 6
• Describe how to implement a priority queue that has a worst case findMin in O(1) time and insert and deleteMin in no more than O(lg n) time. You can assume that n is always less than 128. In other words, there is a max of 127 elements that can be stored in the queue.
• Do the five Priority Queue functions.
![Page 61: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/61.jpg)
Priority Queue -- Lists
• Ordered Array – Find in constant time. – Insert and delete in O(n).
• Unordered Array– Insert in constant time. – Find and delete in O(n).
![Page 62: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/62.jpg)
Priority Queue -- Lists
• Ordered Linked List– Find and delete in constant time. – Insert in O(n).
• Unordered Linked List– Insert in constant time. – Find and delete in O(n).
![Page 63: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/63.jpg)
Priority Queue Trees
• Binary Search Tree– Find can be more than O(lg n) if out of balance.– Insert and delete can be more than O(lg n).
• AVL Tree– Find is O(lg n) time.– Insert and delete are O(lg n).
![Page 64: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/64.jpg)
Priority Queue Trees
• AVL Tree with pointer to smallest– Find is O(1) time.– Insert and delete are O(lg n).– Works, but is way too complicated for the task
• We need a simpler solution
![Page 65: O(lg n) Search Tree](https://reader036.fdocuments.in/reader036/viewer/2022081421/56813518550346895d9c6ebd/html5/thumbnails/65.jpg)
Homework 6
• Describe how to implement a priority queue that has a worst case findMin in O(1) time and insert and deleteMin in no more than O(lg n) time. You can assume that n is always less than 128. In other words, there is a max of 127 elements that can be stored in the queue.
• Do the five Priority Queue functions.