Вы находитесь на странице: 1из 29

Running times?

printPreOrder( )
printInOrder( )
printPostOrder( )
size( )
height( )

O(n)
O(n)
O(n)
O(n)
O(n)
O(n)

makeEmpty( )

Lecture 16
Binary Search Trees

CS2134

Binary Search Tree Order Property


In a binary search tree, for every node X, all keys
in Xs left subtree have smaller values than the key
in X, and all keys in Xs right subtree have larger
values than the key in X.

CS2134

Binary Search Trees


Binary Trees which store elements in tree order
Key of node is element it stores
Tree order: for each node x in the tree
keys in left subtree < key(x)
keys in right subtree > key(x)

CS2134

7
2

search tree

not a search tree

CS2134

How should we build the binary search tree?


7 2

7
2

9
5

1
3

CS2134

How do we determine if 5 is in the tree?


Is 5 equal to 7, less than 7?
Is 5 equal to 2, less than 2?

7
2

Is 5 equal to 5?
5

1
3

CS2134

How do we determine if 6 is in the tree?


Is 6 equal to 7, less than 7?
Is 6 equal to 2, less than 2?

7
2

Is 6 equal to 5, less than 5?


5

1
3

CS2134

How do we insert 6 into the tree?


Is 6 greater than 7, less than 7?
Is 6 greater than 2, less than 2?

7
2

Is 6 greater than 5, less than 5?


5

1
3

CS2134

How do we delete an object in the tree


Delete 1
Is 1 equal to 7, less than 7?
7
Is 1 equal to 2, less than 2?
2

Is 1 equal to 1?
5

3
2

9
5

If the node is a leaf, delete the node


and set parents child pointer to NULL
CS2134

10

How do we delete an object in the tree that is not a leaf?


Delete 5

Is 5 equal to 7, less than 7?


Is 5 equal to 2, less than 2?

7
2

Is 5 equal to 5?

3
1

9
3

If the node has only one child - adjust


parents child link to bypass the node and
then delete the node
CS2134

11

How do we delete an object in the tree that has two children?


Delete 2

Is 2 equal to 7, less than 7?


Is 2 equal to 2?
7

7
2

9
5

4
4

Replace the node with the smallest


item in right subtree
CS2134

12

Delete 7
Is 7 equal to 7?
7
9

8
8

2
3
4

1
3

Replace the node with the smallest


item in right subtree
CS2134

13

The Node Class


template <class Comparable>
class BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
int size;

element

size
left

right

NULL NULL

BinaryNode( const Comparable & theElement, BinaryNode *lt,


BinaryNode *rt, int sz = 1 )
: element( theElement ), left( lt ), right( rt ), size( sz ) { }
};

friend class BinarySearchTree<Comparable>;

int main()
{

new BinaryNode<int>( 9, NULL, NULL, 1 );


...

CS2134

14

The BinarySearchTree Class

template <class Comparable>


class BinarySearchTree
{
public:
typedef BinaryNode<Comparable> Node;

BinarySearchTree( ) : root( NULL ) { } // Construct the tree.


BinarySearchTree( const BinarySearchTree & rhs ) : root( NULL ) { *this = rhs; }// Copy constructor.
~BinarySearchTree( ){ makeEmpty( ); } // Destructor for the tree.
const Comparable * findKth( int k ) const { return elementAt( findKth( k, BinarySearchTree<Comparable>::root ) );}
const Comparable * findMin( ) const { return elementAt( findMin( root ) ); }
const Comparable * findMax( ) const { return elementAt( findMax( root ) ); }
const Comparable * find( const Comparable & x ) const { return elementAt( find( x, root ) ); }
bool isEmpty( ) const { return root == NULL;}
void makeEmpty( ) { makeEmpty( root ); }
void insert( const Comparable & x ) { insert( x, root ); }
void remove( const Comparable & x ) { remove( x, root ); }
void removeMin( ) { removeMin( root ); }
const BinarySearchTree & operator=( const BinarySearchTree & rhs );
private:
Node * root;
int treeSize( Node *t ) const { return t == NULL ? 0 : t->size; }
Comparable const * elementAt( Node *t ) const;
void insert( const Comparable & x, Node * & t ) const;
void remove( const Comparable & x, Node * & t ) const;
void removeMin( Node * & t ) const;
Node * findMin( Node *t ) const;
Node * findMax( Node *t ) const;
Node * find( const Comparable & x, Node *t ) const;
void makeEmpty( Node * & t ) const;
Node * clone( Node *t ) const;
CS2134
Node *findKth( int k, Node *t ) const;
int treeSize( Node *t ) const { return t == NULL ? 0 : t->size; }
};

15

Search
How do we determine if 5 is in the tree?
Is 5 equal to 7, less than 7?
7
Is 5 equal to 2, less than 2?
2

Is 5 equal to 5?
5

1
3

Very similar to binary search in a vector

CS2134

Searching in BSTs
Search for key i in BST
Start at root node,
repeat the following steps until i is found or node is NULL
Compare i to key at current node
if i < key, move on to left child
If i > key, move on to right child
if equal, done
Can do recursively
Faster to do iteratively
Fastest methods are careful to limit # of comparisons

CS2134

Finding if an object is in the binary search tree


template <class Comparable>
class BinarySearchTree
{
public:
...// public methods
const Comparable * find( const Comparable & x ) const
{ return elementAt( find( x, root ) ); }
private:
Node * root;
... // private methods
Node * find( const Comparable & x, Node *t ) const;
};
Internal method to find an item in a subtree.
x is item to search for.
t is the node that roots the tree.
Return node containing the matched item.

template <class Comparable>


BinaryNode<Comparable> * BinarySearchTree<Comparable>::
find( const Comparable & x, Node *t ) const
{
while( t != NULL )
if( x < t->element )
t = t->left;
else if( t->element < x )
t = t->right;
else
return t;
// Match
return NULL;

7
2

9
21

// Not found

root
t

3
CS2134

6 10

finding 5

Given a pointer to a node, return a pointer to an object


template <class Comparable>
class BinarySearchTree
{
public:
...// public methods
private:
Node * root;
...

// private methods

Comparable const * elementAt( Node *t ) const;


};

template <class Comparable>


Comparable const * BinarySearchTree<Comparable>::elementAt( Node *t ) const
{
if( t == NULL )
Internal method to return NULL if the item doesn't exist,
return NULL;
otherwise a pointer to the element field in node t.
else
return &(t->element);
}

CS2134

Special Search
Finding the smallest object in the tree
Is left child equal to NULL?
7
Is left child equal to NULL?
2

Is left child equal to NULL?


5

1
3

CS2134

20

Finding the minimum object in the binary search tree


template <class Comparable>
class BinarySearchTree
{
public:
...// public methods
const Comparable * findMin( ) const
{ return elementAt( findMin( root ) ); }
private:
Node * root;
};

... // private methods


Node * findMin( Node *t ) const;
Internal method to find the smallest item
in a subtree t.
Return node containing the smallest item.

template <class Comparable>


BinaryNode<Comparable> * BinarySearchTree<Comparable>::findMin( Node *t ) const
{
if( t != NULL )
while( t->left != NULL )
t = t->left;

root

return t;
}

9
6

1
CS2134

21
7 10

Special Search
Finding the largest object in the tree
Is right child equal to NULL?
7

9
5

1
3

Is right child equal to NULL?


Is right child equal to NULL?
11

8
10

CS2134

22

Finding the maximum object in the binary search tree


template <class Comparable>
class BinarySearchTree
{
public:
...// public methods
Comparable const * findMax( ) const
{ return elementAt( findMax( root ) ); }
private:
Node * root;
... // private methods
Node * findMax( Node *t ) const;
};

Internal method to find the largest item


in a subtree t.
Return node containing the largest item.

template <class Comparable>


BinaryNode<Comparable> * BinarySearchTree<Comparable>::findMax( Node *t ) const
{
if( t != NULL )
while( t->right != NULL )
t = t->right;
}

return t;

CS2134

21

1
2

10

Insertion
Insert an object into the tree
If 6 is not in the tree, insert it into the tree
Is 6 equal to 7, less than 7?
7
Is 6 equal to 2, less than 2?
2

Is 6 equal to 5, less than 5?


5

1
3

Assume dont allow duplicate elements in tree


Insertion is like search, but when reach NULL,
insert new node there containing element
Shape of tree depends on order of insertions
CS2134

24

Inserting a new object into the binary search tree


template <class Comparable>
class BinarySearchTree
{
public:
...// public methods
void insert( const Comparable & x )
{ insert( x, root ); }
private:
Node * root;
... // private methods
};

void insert( const Comparable & x, Node * & t ) const;

template <class Comparable>


void BinarySearchTree<Comparable>::
insert( const Comparable & x, Node * & t ) const
{
if( t == NULL )
t = new Node( x, NULL, NULL, 0 );
else if( x < t->element )
insert( x, t->left );
else if( t->element < x )
insert( x, t->right );
else
throw DuplicateItemException( );

Internal method to insert into a subtree.


x is the item to insert.
t is the node that roots the tree.
Set the new root.
Throw DuplicateItemException if x is

inserting 6
t

4t

root
t

6,t

t->size++;
}

6,t
6,t

CS2134

function
call stack

1
2

21
10

BinarySearchTree<int> t;
int NUMS = 10;
const int GAP =
3;
int i, j;

element 3

root

for( i = GAP; i != 0; i = ( i + GAP ) % NUMS )


t.insert( i );

NULL

size

left

right

element 6

element

size

size

left

right

left

right

NULL

element

element

size

size

left

right

left

right

NULL

NULL

size

left

right

size

left

right
NULL

NULL

element

element

element

size

left

right
NULL

NULL NULL

element

size

left

right

NULL NULL

CS2134

26

Deletion
Special Case: Deletion of Smallest object in the Tree
Is left child NULL?
7

Is left child NULL?


9

Is left child NULL?


7

9
5

If the node is a leaf, delete the node and set


parents child pointer to NULL. Otherwise,
attach the right child to the parents node.
CS2134

27

Deletion
Special Case: Deletion of Smallest object in the Tree
Is left child NULL?
7

Is left child NULL?


9

Is left child NULL?


7

1
2

5
4

If the node is a leaf, delete the node and set


parents child pointer to NULL. Otherwise,
attach the right child to the parents node.
CS2134

28

Removing the minimum object in the binary search tree


template <class Comparable>
class BinarySearchTree
{
public:
...// public methods
void removeMin( ){ removeMin( root ); }
private:
Node * root;
... // private methods
void removeMin( Node * & t ) const;
};

Internal method to remove minimum item from a subtree.


t is the node that roots the tree.
Set the new root.
Throw UnderflowException if t is empty.

template <class Comparable>


void BinarySearchTree<Comparable>::removeMin( Node * & t ) const
{
if( t == NULL )
throw UnderflowException( );
else if( t->left != NULL )
removeMin( t->left );
else
{
Node *tmp = t;
t = t->right;
delete tmp;
return;
}

8
4
1

t->size--;

root
t

9
6

21

CS2134

10

Вам также может понравиться