BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain &...

23
Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BSTs Data Structures & OO Development I 1 Binary Search Trees A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and: The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will) and the general binary tree does not have good performance. Suppose that we wish to store data elements that contain a number of fields, and that one of those fields is distinguished as the key upon which searches will be performed. 1. All keys in the left subtree (if there is one) are less than the key in the root node. 2. All keys in the right subtree (if there is one) are greater than (or equal to)* the key in the root node. 3. The left and right subtrees of the root are binary search trees. * In many uses, duplicate values are not allowed.

description

BSTs Data Structures & OO Development I 3 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Searching in a BST D BG EH A C Because of the key ordering imposed by a BST, searching resembles the binary search algorithm on a sorted array, which is O(log N) for an array of N elements. A BST offers the advantage of purely dynamic storage, no wasted array cells and no shifting of the array tail on insertion and deletion. Trace searching for the key value E.

Transcript of BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain &...

Page 1: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

1Binary Search Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will) and the general binary tree does not have good performance.

Suppose that we wish to store data elements that contain a number of fields, and that one of those fields is distinguished as the key upon which searches will be performed.

1. All keys in the left subtree (if there is one) are less than the key in the root node.

2. All keys in the right subtree (if there is one) are greater than (or equal to)* the key in the root node.

3. The left and right subtrees of the root are binary search trees.

* In many uses, duplicate values are not allowed.

Page 2: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

2BST Insertion

Here, the key values are characters (and only key values are shown).

Inserting the following key values in the given order yields the given BST:

D G H E B D F C D

B G

E H

D F

C

What is the resulting tree if the (same) key values are inserted in the order:

B C D D E F G H or E B C D D F G H

In a BST, insertion is always at the leaf level. Traverse the BST, comparing the new value to existing ones, until you find the right spot, then add a new leaf node holding that value.

Page 3: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

3Searching in a BST

D

B G

E HA C

Because of the key ordering imposed by a BST, searching resembles the binary search algorithm on a sorted array, which is O(log N) for an array of N elements.

A BST offers the advantage of purely dynamic storage, no wasted array cells and no shifting of the array tail on insertion and deletion.

Trace searching for the key value E.

Page 4: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

4

D

B G

E H

D F

C

BST Deletion

Deletion is perhaps the most complex operation on a BST, because the algorithm must result in a BST. The question is: what value should replace the one deleted? As with the general tree, we have cases:

- Removing a leaf node is trivial, just set the relevant child pointer in the parent node to NULL.

- Removing an internal node which has only one subtree is also trivial, just set the relevant child pointer in the parent node to target the root of the subtree.

NULL

Page 5: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

5BST Deletion

- Removing an internal node which has two subtrees is more complex…

D

B G

F H

E F

C

Simply removing the node would disconnect the tree. But what value should replace the one in the targeted node?

To preserve the BST property, we must take the smallest value from the right subtree, which would be the closest succcessor of the value being deleted.

Fortunately, the smallest value will always lie in the left-most node of the subtree.

Page 6: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

6BST Deletion

So, we first find the left-most node of the right subtree, and then swap data values between it and the targeted node.

Note that at this point we don’t necessarily have a BST.

Now we must delete the copied value from the right subtree.

That looks straightforward here since the node in question is a leaf. However…

- the node will NOT be a leaf in all cases

- the occurrence of duplicate values is a complicating factor

- so we might want to have a DeleteRightMinimum() function to clean up at this point

E

B G

F H

E F

C

Page 7: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

7Deleting the Minimum Value

Suppose we want to delete the value ‘E’ from the BST:

After swapping the ‘F’ with the ‘E’, we must delete

We must be careful to not confuse this with the other node containing an ‘F’.

Also, consider deleting the value ‘G’. In this case, the right subtree is just a leaf node, whose parent is the node originally targeted for deletion.

Moral: be careful to consider ALL cases when designing.

E

B G

F H

F

C

Page 8: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

8BST Template InterfaceHere’s a partial BST template:

template <typename T> class BST {private: BinNodeT<T>* Root;

bool InsertHelper(const T& D, BinNodeT<T>*& sRoot); bool DeleteHelper(const T& D, BinNodeT<T>* sRoot); bool DeleteRightMinimum(BinNodeT<T>* sRoot); T* const FindHelper(const T& D, BinNodeT<T>* sRoot); const T* const FindHelper(const T& D, BinNodeT<T>* sRoot) const; // additional member fn's not shown

public: BST(); // create empty BST BST(const T& D); // root holds D // deep copy support not shown bool Insert(const T& D); // insert element bool Delete(const T& D); // delete element T* const Find(const T& D); // return access to D const T* const Find(const T& D) const; // return access to D void Clear(); void Display(std::ostream& Out) const; ~BST();};// . . . continues with member function implementations . . .

Arguably, one could derive this from a general binary tree type, but there is little merit in doing so.

Page 9: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

9BST Constructor and Destructor

The default BST constructor just initializes an empty tree by setting Root to NULL:

template <typename T> BST<T>::BST() { Root = NULL;}

The BST destructor is simply a trivial modification of the one shown earlier for the general binary tree.

The second BST constructor is entirely similar, merely calling the corresponding base constructor.

Page 10: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

10BST Search Implementation

The BST Find() function takes advantage of the BST data organization:

template <typename T> T* const BST<T>::Find(const T& toFind) {

if (Root == NULL) return NULL;

return (FindHelper(toFind, Root));}

template <typename T> T* const BST<T>::FindHelper(const T& toFind, BinNodeT<T>* sRoot) {

if (sRoot == NULL) return NULL;

if (sRoot->Element == toFind) { return &(sRoot->Element); }

if (toFind < sRoot->Element) return FindHelper(toFind, sRoot->Left); else return FindHelper(toFind, sRoot->Right);}

Search direction is determined by relationship of target data to data in current node.

Uses operator== for the type T, which is customized by the client for the particular application at hand.

Page 11: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

11BST Insert Implementation

The public Insert() function is just a stub to call the recursive helper:

template <typename T>bool BST<T>::Insert(const T& D) {

return InsertHelper(D, Root);}

The stub simply calls the helper function..

The helper function must find the appropriate place in the tree to place the new node.

The design logic is straightforward:

- locate the parent "node" of the new leaf, and

- hang a new leaf off of it, on the correct side

Warning:

the BST definition in these notes allows for duplicate data values to occur, the logic of insertion may need to be changed for your specific application.

Page 12: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

12BST Insert Helper

The InsertHelper() function:

template <typename T>bool BST<T>::InsertHelper(const T& D, BinNodeT<T>*& sRoot) {

if (sRoot == NULL) { // found the location BinNodeT<T>* Temp = new(nothrow) BinNodeT<T>(D); if (Temp == NULL) return false; sRoot = Temp; return true; }

// recursive descent logic goes next . . .}

When the parent of the new value is found, one more recursive call takes place, passing in a NULL pointer to the helper function.

Note that the insert helper function must be able to modify the node pointer parameter, and that the search logic is precisely the same as for the find function.

Page 13: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

13BST Delete Implementation

The public Delete() function is very similar to the insertion function:

template <typename T>bool BST<T>::Delete(const T& D) {

if ( Root == NULL ) return false;

return DeleteHelper(D, Root);}

The DeleteHelper() function design is also relatively straightforward:

- locate the parent of the node containing the target value

- determine the deletion case (as described earlier) and handle it:

- parent has only one subtree

- parent has two subtrees

The details of implementing the delete helper function are left to the reader…

Page 14: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

14Parent Pointers

Some binary tree implementations employ parent pointers in the nodes.

- increases memory cost of the tree (probably insignificantly)

- increases complexity of insert/delete/copy logic (insignificantly)

- provides some unnecessary alternatives when implementing insert/delete

- may actually simplify the addition of iterators to the tree (later topic)

It is also useful to have some instrumentation during testing. For example:

- log the values encountered and the directions taken during a search

This is also easy to add, but it poses a problem since we generally do not want to see such output when the BST is used.

I resolve this by adding some data members and mutators to the template that enable the client to optionally associate an output stream with the object, and to turn logging of its operation on and off as needed.

Page 15: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

15Some Refinements

The given BST template may also provide additional features:

- a function to provide the size of the tree

- a function to provide the height of the tree

- a function to display the tree in a useful manner

It is also useful to have some instrumentation during testing. For example:

- log the values encountered and the directions taken during a search

This is also easy to add, but it poses a problem since we generally do not want to see such output when the BST is used.

I resolve this by adding some data members and mutators to the template that enable the client to optionally associate an output stream with the object, and to turn logging of its operation on and off as needed.

Page 16: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

16InstrumentationThe extended BST template:

template <typename T> class BST {protected: . . . ostream* Log; bool loggingOn; . . .

public: . . . bool setLogStream(ostream* const L); bool logOn(); bool logOff();};

template <typename T> bool BST<T>::logOn() { if ( Log != NULL ) loggingOn = true; return loggingOn;}

template <typename T> T* BST<T>::InsertHelper(. . .) { . . . if ( Elem < (sRoot->Element) ) { if ( loggingOn ) *Log << "Insert: going left from " << (sRoot->Element) << endl; return InsertHelper(Elem, sRoot->Left); } . . .}

The client may entirely ignore the ability to log an execution trace, or enable it and turn logging on and off at will.

Of course, it's important to avoid dereferencing a null pointer…

Page 17: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

17Adding an Iterator

A BST iterator can be added in a very similar way to that shown earlier for the linked list iterator.

As before:

- add a declaration of a class iterator within the BST declaration

- an iterator will store a pointer to a tree node; dereferencing it will return the address of the data element within that node.

- implement the expected increment/decrement operators for the iterator

- add the expected iterator-supplying functions to the BST

- there's no case for an iterator-based insertion function in a BST, but there may be a case for an iterator-based deletion function

- add a search function that returns an iterator

There are two considerations:

- what traversal pattern should the iterator provide?

- is it necessary to modify the BST implementation aside from adding support functions?

Page 18: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

18Iterator-based Find

The public Insert() function is just a stub to call the recursive helper:

template <typename T> typename BST<T>::iterator BST<T>::Find(const T& toFind) {

return FindHelper(toFind, Root);}

The public stub simply calls the helper function:

template <typename T> typename BST<T>::iterator BST<T>::FindHelper(const T& toFind, BinNodeT<T>* sRoot) {

if (sRoot == NULL) return iterator(NULL);

if (sRoot->Element == toFind) { return iterator(sRoot); }

// recursive descent logic goes next . . .}

Page 19: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

19Iterator Increment Logic

The only issue that is handled differently from the the linked list iterator is the pattern by which an iterator steps forward or backwards within the BST.

Consider stepping forward as in an inorder traversal:

The pattern is reasonably straightforward, but how can we move up from a node to its parent within the BST?

C

A I

E J

D G

B

F H

begin()end()

Page 20: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

20Client-side Use

Here is a somewhat trivial use of the iterator-supplied traversal:

BST<int>::iterator It; for (It = T.begin(); It != T.end(); It++) { if ( *It <= Floor ) { *It = Floor; } }

Note also that the client can implement an inorder search of the BST using the same approach. In fact, the BST itself could do that instead of recursion.

One point here is that the code above shows how the client can traverse the structure without knowing anything about the actual physical layout… compare to a traversal of an STL vector, or even of a simple array.

Page 21: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

21Balance in a BST

D

B G

E HA C

However, a BST with N nodes does not always provide O(log N) search times.

B

A G

C H

D

E

A well-balanced BST. This will have log(N) search times in the worst case.

A poorly-balanced BST. This will not have log(N) search times in the worst case.

A B C D E G H

What if we inserted the values in the order:

Page 22: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

22Search Cost in a BST

From an earlier theorem on binary trees, we know that a binary tree that contains L nodes must contain at least 1 + log L levels.

If the tree is full, we can improve the result to imply that a full binary tree that contains N nodes must contain at least log N levels.

So, for any BST, the there is always an element whose search cost is at least log N.

Unfortunately, it can be much worse. If the BST is a "stalk" then the search cost for the last element would be N.

It all comes down to one simple issue: how close is the tree to having minimum height?

Unfortunately, if we perform lots of random insertions and deletions in a BST, there is no reason to expect that the result will have nearly-minimum height.

Page 23: BSTs Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Search Trees A binary search tree or BST.

Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens

BSTs

Data Structures & OO Development I

23Cost of Insertion/Deletion in a BST

Clearly, once the parent is found, the remaining cost of inserting a new node in a BST is constant, simply allocating a new node object and setting a pointer in the parent node.

So, insertion cost is essentially the same as search cost.

For deletion, the argument is slightly more complex. Suppose the parent of the targeted node has been found.

If the parent has only one subtree, then the remaining cost is resetting a pointer in the parent and deallocating the node; that's constant.

But, if the parent has two subtrees, then an additional search must be carried out to find the minimum value in the right subtree, and then an element copy must be performed, and then that node must be removed from the right subtree (which is again a constant cost).

In either case, we have no more than the cost of a worst-case search to the leaf level, plus some constant manipulations.

So, deletion cost is also essentially the same as search cost.