design and analysis of algorithms binary search trees

61
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU

Upload: larue

Post on 23-Feb-2016

42 views

Category:

Documents


0 download

DESCRIPTION

Design and Analysis of Algorithms Binary search trees. Haidong Xue Summer 2012, at GSU. Operations on a binary search tree. SEARCH(S, k) MINIMUM(S) MAXIMUM(S) SUCCESSOR(S, x) PREDECESSOR(S, x) INSERT(S, x) DELETE(S, x). O(h). O(h). h is the height of the tree. O(h). O(h). O(h) . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Design  and Analysis of  Algorithms Binary search trees

Design and Analysis of AlgorithmsBinary search trees

Haidong XueSummer 2012, at GSU

Page 2: Design  and Analysis of  Algorithms Binary search trees

Operations on a binary search tree

• SEARCH(S, k)• MINIMUM(S)• MAXIMUM(S)• SUCCESSOR(S, x)• PREDECESSOR(S, x)• INSERT(S, x)• DELETE(S, x)

O(h)

O(h) h is the height of the treeO(h)

O(h)

O(h)

O(h)

O(h)

Page 3: Design  and Analysis of  Algorithms Binary search trees

What is a binary search tree?

• A binary tree• Binary-search-tree property– For each node, all the nodes in its left sub tree is

smaller than to or equal to this node; all the nodes in its right sub tree is larger than or equal to this node

What the difference between “binary search tree” and a “max-heap”?

Not a complete binary tree

With a different tree property

Page 4: Design  and Analysis of  Algorithms Binary search trees

What is a binary search tree?

11

9 12

8 10 11 12

2

6

5 7

2 5 8

Yes Yes

Page 5: Design  and Analysis of  Algorithms Binary search trees

What is a binary search tree?

2

5

5

6 8

7

1

2

6

4

3

YesYes

Page 6: Design  and Analysis of  Algorithms Binary search trees

What is a binary search tree?

11

9 12

8 12 11 12

10

11

9 25

8 10 20 78

8 13 19

No No

Page 7: Design  and Analysis of  Algorithms Binary search trees

Elements in a binary search tree• Can we use an array to represent a binary

search tree?– No– So some tree structure information has to be

stored

Page 8: Design  and Analysis of  Algorithms Binary search trees

Elements in a binary search tree• binary search tree node {– Key– Satellite data– Left node (left)– Right node (right)– Parent node (p)}

Page 9: Design  and Analysis of  Algorithms Binary search trees

11

9 12

8 10 11 12

2

p

left right

p

left right

p

left right

p

left rightp

left rightp

left right

p

left right

p

left right

11

9 12

8 10 11 12

2

NIL

NIL NIL NIL NIL NIL NIL NIL

NIL NIL

Operations are based on this structure

Page 10: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Preorder tree walk• Inorder tree walk• Postorder tree walk

Page 11: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Preorder-tree-walk (node x){– If(x==NIL) return;– Access(x);– Preorder-tree-walk(x.left);– Preorder-tree-walk(x.right)}

Page 12: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Preorder-tree-walk ( );

Page 13: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Inorder-tree-walk (node x){– If(x==NIL) return;– Inorder-tree-walk( x.left);– Access(x);– Inorder-tree-walk( x.right)}

Page 14: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Inorder-tree-walk ( );

Page 15: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Postorder-tree-walk (node x){– If(x==NIL) return;– Postorder-tree-walk( x.left);– Postorder-tree-walk( x.right)– Access(x);}

Page 16: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Postorder-tree-walk ( );

Page 17: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Preorder tree walk

• Inorder tree walk

• Postorder tree walk

Page 18: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order• Preorder tree walk

• Inorder tree walk

• Postorder tree walk

11

9 12

8 10 11 12

11 9 8 10 12 11 12

8 9 10 11 11 12 12

8 10 9 11 12 12 11

Sorted order from inorder tree walk!

Page 19: Design  and Analysis of  Algorithms Binary search trees

Print all keys in sorted order

• Time complexity of inorder tree walk– Access each node once

Page 20: Design  and Analysis of  Algorithms Binary search trees

Searching in a binary search tree

TREE-SEARCH• Input: root pointer (x), key (k)• Output: a element whose key is the same as

the input key; NIL if no element has the input key

1. if(x==NIL or x.key==k) return x;2. if(k<x.key) return TREE-SEARCH(x.left, k);3. Return TREE-SEARCH(x.right, k);

Page 21: Design  and Analysis of  Algorithms Binary search trees

11

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 11)11

Page 22: Design  and Analysis of  Algorithms Binary search trees

11

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 11)11

Page 23: Design  and Analysis of  Algorithms Binary search trees

8

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 8)11

TREE-SEARCH( , 8)9

TREE-SEARCH( , 8)8

Page 24: Design  and Analysis of  Algorithms Binary search trees

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 20)11

TREE-SEARCH( , 20)12

TREE-SEARCH( , 20)12

NIL

TREE-SEARCH( NIL , 20)

NIL

Means there is no such a node in the tree

Page 25: Design  and Analysis of  Algorithms Binary search trees

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 20)30

30

Illegal, but never happen if start from the root

Page 26: Design  and Analysis of  Algorithms Binary search trees

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 2)11

What’s the worst case?

Worst successful search

Worst unsuccessful search TREE-SEARCH( , 1)11

O(h)

O(h)

Page 27: Design  and Analysis of  Algorithms Binary search trees

Searching in a binary search treeIterative code could be more efficient than recursive code

TREE-SEARCH (x, k)1. if(x==NIL or x.key==k) return x;2. if(k<x.key) return TREE-SEARCH(x.left, k);3. Return TREE-SEARCH(x.right, k);

TREE-SEARCH (x, k)1. current=x;2. While (current!=NIL and current.key!=k){

if(x.key<k) current=x.left;else current=x.right;

}3. return current;

Page 28: Design  and Analysis of  Algorithms Binary search trees

8

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 8)11

TREE-SEARCH (x, k)1. current=x;2. while (current!=NIL and current.key!=k){

if(x.key<k) current=x.left;else current=x.right;

}3. return current;

Page 29: Design  and Analysis of  Algorithms Binary search trees

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 20)11

NIL

NIL

Page 30: Design  and Analysis of  Algorithms Binary search trees

Minimum and maximum

11

9 12

8 10 11 12

2

As a human, can you tell where is the minima and maxima?

Page 31: Design  and Analysis of  Algorithms Binary search trees

Minimum and maximum

11

9 12

8 10 11 12

2

The minima of the tree rooted at x is: the minima of x.left if x.left is not NIL; x if x.left is NIL

TREE-MINIMUM( x ) //the recursive one

1. if (x.left==NIL) return x;2. return TREE-MINIMUM(x.left);

0. if(x==NIL) return NIL;

It has some cost, so if x is guaranteed not NIL we can remove it

Page 32: Design  and Analysis of  Algorithms Binary search trees

Minimum and maximum

11

9 12

8 10 11 12

2

TREE-MINIMUM( ) //recursive11

2

Page 33: Design  and Analysis of  Algorithms Binary search trees

Minimum and maximum

11

9 12

8 10 11 12

2

The minima of the tree rooted at x is: the leftmost node

TREE-MINIMUM( x ) //the iterative one

1. current = x;2. while(current.left!=NIL)

current = current.left;3. return current;

0. if(x==NIL) return NIL;

Page 34: Design  and Analysis of  Algorithms Binary search trees

2

Minimum and maximum

11

9 12

8 10 11 12

2

TREE-MINIMUM( ) //iterative11

Page 35: Design  and Analysis of  Algorithms Binary search trees

Minimum and maximum

TREE-MINIMUM( x ) //the iterative one0. if(x==NIL) return NIL;1. current = x;2. while(current.left!=NIL)

current = current.left;3. return current;

TREE-MINIMUM( x ) //the recursive one0. if(x==NIL) return NIL;1. if (x.left==NIL) return x;2. return TREE-MINIMUM(x.left);

TREE-MAXIMUM( x ) //the recursive one0. if(x==NIL) return NIL;1. if (x.right==NIL) return x;2. return TREE-MAXIMUM( x.right);

TREE-MAXIMUM( x ) //the iterative one0. if(x==NIL) return NIL;1. current = x;2. while(current.right!=NIL)

current = current.right;3. return current;

O(h) O(h)

Time complexity?

Page 36: Design  and Analysis of  Algorithms Binary search trees

Successor and predecessor

• What is a successor of x?• What is a successor of x if there is another

node has the same key in the binary search tree?

11

9 12

8 10 11 12

8 9 10 11 11 12 12

Page 37: Design  and Analysis of  Algorithms Binary search trees

Successor and predecessor

15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 15

The minimum of the right sub tree

TREE-SUCCESSOR( ) 13

There is no right sub tree

The lowest ancestor whose left child is also an ancestor of or 13 13

Page 38: Design  and Analysis of  Algorithms Binary search trees

Successor and predecessorTREE-SUCCESSOR( x ) // When x has a right sub tree1. if(x.right!=NIl) return TREE-MINIMUM(x);

// When x does not have a right sub tree2. current = x3. currentParent = x.p4. while( currentParent!=NIL and currentParent.left!=current ){

current = currentParent;currentParrent = currenParent.p;

}5. return currentParent;

Page 39: Design  and Analysis of  Algorithms Binary search trees

17

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 15

2 3 4 6 7 9 13 15 17 18 20

TREE-MINIMUM

Page 40: Design  and Analysis of  Algorithms Binary search trees

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 9

2 3 4 6 7 9 13 15 17 18 20

current

currentParent

current == currentParent.left is true

Has no right sub tree

13

Page 41: Design  and Analysis of  Algorithms Binary search trees

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 13

2 3 4 6 7 9 13 15 17 18 20

current

currentParent

current == currentParent.left is falsecurrentParent == NIL is false

No right subtree

current == currentParent.left is falsecurrentParent == NIL is false

current == currentParent.left is true

15

Page 42: Design  and Analysis of  Algorithms Binary search trees

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 20

2 3 4 6 7 9 13 15 17 18 20

current

currentParentcurrent == currentParent.left is falsecurrentParent == NIL is false

No right subtree

current == currentParent.left is falsecurrentParent == NIL is false

currentParent == NIL is true

NIL

Page 43: Design  and Analysis of  Algorithms Binary search trees

TREE-SUCCESSOR( x ) // When x has a right sub tree1. if(x.right!=NIl) return TREE-MINIMUM(x);

// When x does not have a right sub tree2. current = x3. currentParent = x.p4. while( !(currentParent==NIL or currentParent.left==current) ){

current = currentParent;currentParrent = currenParent.p;

}5. return currentParent;

TREE-PREDECESSOR( x ) // When x has a left sub tree1. if(x.left!=NIl) return TREE-MAXIMUM(x);

// When x does not have a left sub tree2. current = x3. currentParent = x.p4. while( !(currentParent==NIL or currentParent.right==current) ){

current = currentParent;currentParrent = currenParent.p;

}5. return currentParent;

Time complexity?

O(h)

Page 44: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion

11

9 19

8 10 18 22

2

As a human, how to insert a element to a binary search tree?

Page 45: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletionTREE-INSERT( T, z ) if(T.root==NIL){ T.root = z; z.p = NIL;}else { INSERT(t.root, z);}

INSERT(x, z)if(z.key<x.key)

if(z.left==NIL){ z.p=x; x.left=z;}else INSERT(x.left, z);

else if(z.right==NIL){ z.p=x; x.right=z;}else INSERT(x.right, z);

Page 46: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion

11

9 19

8 10 18 22

2

TREE-INSERT( T, ) //recursive 3

Not NIL

Not NIL

Not NIL

NIL

3

Page 47: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletionTREE-INSERT( T, z ) // iterative1. posParent = NIL;2. pos = T.root; // try to find a position, start from T.root3. while(pos!=NIL){4. posParent = pos;5. if(z.key < pos.key)6. pos = pos.left;7. else8. pos = pos.right;9. }10. z.p = posParent;11. if(posParent==NIL); // T is empty12. T.root = z;13. else if(z.key<posParent.key)14. posParent.left = z;15. else16. posParent.right = z;

Find a position

Modify z

Modify posParent

Page 48: Design  and Analysis of  Algorithms Binary search trees

NIL

Insertion and deletion11

9 19

8 10 18 22

2

TREE-INSERT( T, ) //iterative3

pos

posParent

…..3. while(pos!=NIL){4. posParent = pos;5. if(z.key < pos.key)6. pos = pos.left;7. else8. pos = pos.right;9. }…

3

NIL

Page 49: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion

11

9 19

8 10 18 22

2

As a human, how to delete a element from a binary search tree?

The element has less than 2 children

The element has two children and its successor is the right child

The element has two children and its successor is not the right child

Page 50: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion

11

9 19

8 10 18 22

2

The element has less than one child

TREE-DELETE( )22 // no child

TREE-DELETE( )8 // no child

TRANSPLANT( T, u, v )

Replace a tree with another tree

Page 51: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion

TRANSPLANT( T, u, v ) //in T, replace u tree with v tree //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

Page 52: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion11

9 19

8 10 18 22

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , ) 8 2

p

left right

p

left right

Page 53: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion11

9 19

8 10 18 22

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , .right) 222

p

left right

NIL

Page 54: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion11

9 19

8 10 18 22

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , ) 18 9

p

left right

p

left right

Page 55: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion11

19

18 22

9

8 10

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , ) 18 9

Page 56: Design  and Analysis of  Algorithms Binary search trees

TREE-DELETE( T, z) // When z has less than two children1. if(z.left==NIL) TRANSPLANT(T, z, z.right)2. else if (z.right==NIL) TRANSPLANT(T, z, z.left)// When z has two children3. else{ //Get the successor of z4. y= TREE-MINIMUM(z.right); // it is TREE-SUCCESSOR(z) // if the successor is not z’s right child5. if(z.right != y){ // upgrade the succesor’s right6. TRANSPLANT(T, y, y.right); // assign z’right to the successor 7. y.right = z.right;8. y.right.p = y;9. } // replace z with the successor8. TRANSPLANT(T, z, y); // assign z’left to the successor9. y.left = z.left;10. y.left.p = y.left;11. }

Page 57: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletion

TREE-DELETE( T, ) 15

6 18

3 7 17 20

2 4 13

9

7

Only one child

Page 58: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletionTREE-DELETE( T, )

15

6 18

3 17 20

2 4

7

13

9

6

Find the successor

Replace with the successor tree

6

Assign .left to the successor6

7

Page 59: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletionTREE-DELETE( T, )

15

6 19

3 7 17 20

2 4 13

9

15

Find the successor

Replace the successor with its right tree

Assign .left to the successor15

17

18

Replace with the successor

15

Assign .right to the successor15

Page 60: Design  and Analysis of  Algorithms Binary search trees

Insertion and deletionTime complexity

TREE-INSERT(T, x)

TREE-DELETE(T, x)

Similar to TREE-SEARCH, O(h)

Because of TREE-SUCCESSOR, O(h)

Page 61: Design  and Analysis of  Algorithms Binary search trees

How to build a binary search tree?

• By insertion• When it is done it randomly, the expected

height is O(lgn)• What is the worst case?– There is only 1 leaf

• How to avoid the worst case?– Randomly insert– Variations of binary search tree like RBT