O(lg n) Search Tree

Post on 02-Jan-2016

47 views 0 download

Tags:

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

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

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.

AVL Tree

54

30

25 7910

5 60

86

72

84

210

+1

+1

0

-1

0

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.

Insertion Case 1

0

Insertion Case 1

-1

Insertion Case 2

+1

Insertion Case 2

0

Insertion Case 3

+1

Insertion Case 3

+2

Deletion Case 1

-1

Deletion Case 1

0

Deletion Case 2

0

Deletion Case 2

+1

Deletion Case 3

+1

Deletion Case 3

+2

Single Rotation

+2

+1

Double Rotation

+2

-1

Single Rotation

+2

+1

Single Rotation

+2

+1

A

B

Single RotationA B

Single RotationA B

+2 +1

Single Rotation

0 0

A B

Single Rotation

+2

+1

Single Rotation

0

+1

Single Rotation

0 +1

Single Rotation

0

+1

Single Rotation

0

+1

Single Rotation

0

0

Double Rotation

+2

-1

Double Rotation

+1

0

0

Double Rotation1

+2

-1

-1

A

B

C

Double Rotation1

+2 -1-1

A B C

Double Rotation1

+2

-1

-1

A

B

C

Double Rotation1

+2

-1

-1

A

B

C

Double Rotation1

+1

+1

-1

A

B

C

Double Rotation1

+1

+1-1

A

B C

Double Rotation1

+1

+1-1

A

B C

Double Rotation1

+1

+1

-1

A

B

C

Double Rotation1

+2

+1

+1

A

B

C

Double Rotation1

0

+1

+1

A

B

C

Double Rotation1

0

+1

+1

A B

C

Double Rotation1

0

+1

+1

A

B

C

Double Rotation1

0

+1

0

A

B

C

Double Rotation1

0+1

0

A

B

C

Double Rotation2

+2

-1

+1

A

B

C

Double Rotation2

+2

-1

+1

A

B

C

Double Rotation2

+2

0

+1

A

B

C

Double Rotation2

+2

0+1

A

B C

Double Rotation2

+2

0

+2

A

B

C

Double Rotation2

-1

0

+2

A

B

C

Double Rotation2

-1

0

+2

A B

C

Double Rotation2

-1

0

0

A

B

C

Double Rotation2

-10

0

A

B

C

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

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

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

Binary Search Tree -- Array

• Advantages– fast– can access the parent from a child

• Disadvantages– fixed size– standard AVL rotates greater than O(lg n)

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

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.

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).

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).

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).

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

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.