O(lg n) Search Tree

65
O(lg n) Search Tree Tree T is a search tree made up of n elements: x 0 x 1 x 2 x 3 … x n-1 No function (except transverse) takes more than O(lg n) in the worst case. Functions: createEmptyTree() returns a newly created empty binary tree delete(T, p) removes the node pointed to by p from the tree T insert(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 order isEmptyTree(T) returns true if T is empty and false if it is not

description

O(lg n) Search Tree. Tree T is a search tree made up of n elements: x 0 x 1 x 2 x 3 … x n-1 No function (except transverse) takes more than O(lg n) in the worst case.   Functions: createEmptyTree() returns a newly created empty binary tree - PowerPoint PPT Presentation

Transcript of O(lg n) Search Tree

Page 1: O(lg n) Search Tree

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

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

AVL Tree

54

30

25 7910

5 60

86

72

84

210

+1

+1

0

-1

0

Page 4: O(lg n) Search Tree

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

Insertion Case 1

0

Page 6: O(lg n) Search Tree

Insertion Case 1

-1

Page 7: O(lg n) Search Tree

Insertion Case 2

+1

Page 8: O(lg n) Search Tree

Insertion Case 2

0

Page 9: O(lg n) Search Tree

Insertion Case 3

+1

Page 10: O(lg n) Search Tree

Insertion Case 3

+2

Page 11: O(lg n) Search Tree

Deletion Case 1

-1

Page 12: O(lg n) Search Tree

Deletion Case 1

0

Page 13: O(lg n) Search Tree

Deletion Case 2

0

Page 14: O(lg n) Search Tree

Deletion Case 2

+1

Page 15: O(lg n) Search Tree

Deletion Case 3

+1

Page 16: O(lg n) Search Tree

Deletion Case 3

+2

Page 17: O(lg n) Search Tree

Single Rotation

+2

+1

Page 18: O(lg n) Search Tree

Double Rotation

+2

-1

Page 19: O(lg n) Search Tree

Single Rotation

+2

+1

Page 20: O(lg n) Search Tree

Single Rotation

+2

+1

A

B

Page 21: O(lg n) Search Tree

Single RotationA B

Page 22: O(lg n) Search Tree

Single RotationA B

+2 +1

Page 23: O(lg n) Search Tree

Single Rotation

0 0

A B

Page 24: O(lg n) Search Tree

Single Rotation

+2

+1

Page 25: O(lg n) Search Tree

Single Rotation

0

+1

Page 26: O(lg n) Search Tree

Single Rotation

0 +1

Page 27: O(lg n) Search Tree

Single Rotation

0

+1

Page 28: O(lg n) Search Tree

Single Rotation

0

+1

Page 29: O(lg n) Search Tree

Single Rotation

0

0

Page 30: O(lg n) Search Tree

Double Rotation

+2

-1

Page 31: O(lg n) Search Tree

Double Rotation

+1

0

0

Page 32: O(lg n) Search Tree

Double Rotation1

+2

-1

-1

A

B

C

Page 33: O(lg n) Search Tree

Double Rotation1

+2 -1-1

A B C

Page 34: O(lg n) Search Tree

Double Rotation1

+2

-1

-1

A

B

C

Page 35: O(lg n) Search Tree

Double Rotation1

+2

-1

-1

A

B

C

Page 36: O(lg n) Search Tree

Double Rotation1

+1

+1

-1

A

B

C

Page 37: O(lg n) Search Tree

Double Rotation1

+1

+1-1

A

B C

Page 38: O(lg n) Search Tree

Double Rotation1

+1

+1-1

A

B C

Page 39: O(lg n) Search Tree

Double Rotation1

+1

+1

-1

A

B

C

Page 40: O(lg n) Search Tree

Double Rotation1

+2

+1

+1

A

B

C

Page 41: O(lg n) Search Tree

Double Rotation1

0

+1

+1

A

B

C

Page 42: O(lg n) Search Tree

Double Rotation1

0

+1

+1

A B

C

Page 43: O(lg n) Search Tree

Double Rotation1

0

+1

+1

A

B

C

Page 44: O(lg n) Search Tree

Double Rotation1

0

+1

0

A

B

C

Page 45: O(lg n) Search Tree

Double Rotation1

0+1

0

A

B

C

Page 46: O(lg n) Search Tree

Double Rotation2

+2

-1

+1

A

B

C

Page 47: O(lg n) Search Tree

Double Rotation2

+2

-1

+1

A

B

C

Page 48: O(lg n) Search Tree

Double Rotation2

+2

0

+1

A

B

C

Page 49: O(lg n) Search Tree

Double Rotation2

+2

0+1

A

B C

Page 50: O(lg n) Search Tree

Double Rotation2

+2

0

+2

A

B

C

Page 51: O(lg n) Search Tree

Double Rotation2

-1

0

+2

A

B

C

Page 52: O(lg n) Search Tree

Double Rotation2

-1

0

+2

A B

C

Page 53: O(lg n) Search Tree

Double Rotation2

-1

0

0

A

B

C

Page 54: O(lg n) Search Tree

Double Rotation2

-10

0

A

B

C

Page 55: O(lg n) Search Tree

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

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

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

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

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

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

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

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

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

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

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.