Binary Search

advertisement

Trees

Recursive definition of a tree: terminology internal vertex level of a node depth of a node height of a node

Characteristics of a tree with n nodes

A full k-ary tree

Counting the nodes in a full k-ary tree

Tree traversals

Tree representations

Converting an Arbitrary Tree to a Binary Tree

Array Implementation for complete or nearly complete tree

Decision Trees and Expression Trees

Diagram from Goodrich and Tamassia

Decision Trees

Tree traversals inorder(T: treeptr) if (T

null) then

inorder(T

left)

visit(root(T))

inorder(T

right) endif

Complexity

Breadth-First Search

BFS (T: treeptr)

// Breadth-first search procedure for a binary tree.

// T is a pointer to the root of the tree

Q: queuetype;

put T on the queue

while Q is not empty do begin x

head(Q) // Remove head of queue

visit(x) // Get information from node

if (x

left

null) then put x

left on the queue

if (x

right

null) then put x

right on the queue end // while loop

Depth First Search

More on Traversals

An inorder + another traversal can be used to draw a tree — preorder: A D F G H K L P Q R W Z inorder: G F H D A L P Q R Z W K

Euler tour traversal

Algorithm eulerTour(T, v)

// modification of p. 283 in Goodrich and Tamassia

(1) perform action for visiting node v on the left

(2) if v is an internal node then recursively tour the left subtree of v by calling eulerTour(T, T.leftChild(v))

(3) perform the action for visiting the node v from below.

(4) if v is an internal node then recursively tour the right subtree of v by calling eulerTour(T, T.rightChild(v))

(5) perform the action for visiting node v on the right

More on Euler Tours

Finding the number of descendants of a node

Printing a fully parenthesized expression printExpression(T, v)

// p. 283 in Goodrich and Tamassia

If v is an external node then print the value stored at v else print “(“ printExpression(T, T.leftchild(v)) print the operator stored at v printExpression(T, T.rightChild(v)) print “)” complexity

Threaded trees inorder(T: treeptr) if (T

null) then

inorder(T

left)

visit(root(T))

inorder(T

right) endif

The Search Tree ADT

Binary search trees binary search tree operations: find(x, T) findmax(T) findmin(T) insert(x, T)

Example: build a BST from the following list: 5, 2, 7, 3, 8, 4, 1, 6, 9

Deletions

Case 1:

BST Deletions (continued)

Case 2:

Case 3:

Let’s look at case three with the following binary search tree:

Implementation details:

Find operation on BST

A B C D E F G H

T

1

: T

A

(T

1

) =

T

2

: T

A

(T

2

) =

T

1

: T

A

(T

1

) =

T

2

: T

A

(T

2

) =

Optimal Binary Search Trees

A greedy strategy

Build a binary search tree with keys A, B, C, and D which have probabilities p(A) = p(C) = 1/3 and p(B) = p(D) = 1/6.

But, if A is at the root we get:

k

2 k

3 k

4 k

5 cost:

Another Greedy Approach key k

1

< k

2

< k

3

< k

4

< k

5

.25 .12 p .3 .15 .18

A different strategy key left right k

1

Another approach for finding an optimal BST key k

1

< k

2

< k

3

< k

4

< k

5

.25 .12

p .3 .15 .18

A bottom up approach

Building the Optimal Tree

Let T i,j

denote the subtree that contains keys from i to j inclusive. cost(T i,i

) = c ii

= j cost(T i,j

) = min [cost(T i,k-1

) + cost (T k+1,j

)] +

p m

k m=i n cost(T

1,n

) = min [cost(T i,k-1

) + cost (T k+1,j

)] +

p m

1

 k

 n m=1 key k

1

< k

2

< k

3

< k

4

< k

5 p .3 .15 .18 .25 .12

1 2 3 4 5

.3

.15

1

2

Fill in the table as follows:

1. Fill in the c i,i

values along the diagonal.

.18 3

.25 4

2. Next, calculate all values of the form c k,k+1

.12 5 c

1,2

= min( c

2,3

= min( c

3,4

= c

4,5

Remaining calculations

Trees with three nodes c

1,3

= min[c

1,0

+ c

2,3

, c

1,1

+ c

3,3

, c

1,2

+ c

4,3

] + p

1

+ p

2

+ p

3

= min[0 + .48, .3 + .18, .6] + .3 +.15 +.18 = 1.11 c

2,4

= min[c

2,1

+ c

3,4

, c

2,2

+ c

4,4

, c

2,3

+ c

5,4

] + p

2

+ p

3

+ p

4

= min[0 + .61, .15 + .25, .48 + 0] + .15 + .18 + .25 = .98 c

3,5

=

Trees with four nodes c

1,4

= min[c

1,0

+ c

2,4

, c

1,1

+ c

3,4

, c

1,2

+ c

4,4

, c

1,3

+ c

5,4

] + p

1

+ p

2

+ p

3

+ p

4

= min[.98, .91, .85, 1.11] + .88 = 1.73 c

2,5

= c

1,5

=

Constructing the actual tree

Two approaches used to avoid having a badly balanced tree:

1.

2.

1 2 3 4 5 6 7 8 9

.14 k = 1

.22 k = 1

.04 k = 2

.54 k = 1

.22 k = 3

.14 k = 3

.84 k = 3

.52 k = 3

.43 k = 4

.15 k = 4

1.08 k = 3

.71 k = 4

.59 k = 4

.31 k = 4

.08 k = 5

1.16 k = 3

.77 k = 4

.65 k = 4

.37 k = 4

.12 k = 5

1.59 k = 4

1.13 k = 4

1.01 k = 4

.70 k = 5

.35 k = 7

2.10 k = 4

1.64 k = 4

1.51 k = 7

1.09 k = 7

.69 k = 7

.02 k = 6

.17 k = 7

.13 k = 7

.49 k = 8

.43 k = 8

.17 k = 8

.51 k = 8

.45 k = 8

.19 k = 8

.01 k = 9

Example adapted from Introduction to Algorithms in Pascal by Parsons, Wiley 1995

2.14 k = 4

1.68 k = 4

1.54 k = 7

1.12 k = 7

.72 k = 7

Download