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.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Chapter 4: Trees Part II - AVL Tree
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
AVL Trees Data Structures & File Management Computer Science Dept Va Tech January 2004 © McQuain WD 1 Balanced Binary Trees Binary search trees.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Trees, Binary Trees, and Binary Search Trees COMP171.
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Course notes CS2606: Data Structures and Object-Oriented Development Ryan Richardson John Paul Vergara Department of Computer Science Virginia Tech Spring.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Trees, Binary Trees, and Binary Search Trees COMP171.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Data Structures Trees Phil Tayco Slide version 1.0 Apr. 23, 2015.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Binary Trees Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Binary Trees A binary tree is either.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Binary Search Trees A binary search tree is a binary tree
CSC212 Data Structure - Section AB
COSC160: Data Structures Binary Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Revised based on textbook author’s notes.
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 10 Binary Search Trees
Chapter 21: Binary Trees.
CMSC 341 Binary Search Trees.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
CSC 143 Binary Search Trees.
CS 2604 Intro Data Structures and File Management
Mark Redekopp David Kempe
Trees.
Presentation transcript:

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

BSTs Data Structures & OO Development I 2 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST 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 BG EH DF 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.

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.

BSTs Data Structures & OO Development I 4 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens D BG EH DF 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

BSTs Data Structures & OO Development I 5 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST Deletion -Removing an internal node which has two subtrees is more complex… D BG FH EF 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.

BSTs Data Structures & OO Development I 6 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST 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 BG FH EF C

BSTs Data Structures & OO Development I 7 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Deleting 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 BG FH F C

BSTs Data Structures & OO Development I 8 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST Template Interface Here’s a partial BST template: template class BST { private: BinNodeT * Root; bool InsertHelper(const T& D, BinNodeT *& sRoot); bool DeleteHelper(const T& D, BinNodeT * sRoot); bool DeleteRightMinimum(BinNodeT * sRoot); T* const FindHelper(const T& D, BinNodeT * sRoot); const T* const FindHelper(const T& D, BinNodeT * 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.

BSTs Data Structures & OO Development I 9 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST Constructor and Destructor The default BST constructor just initializes an empty tree by setting Root to NULL : template BST ::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.

BSTs Data Structures & OO Development I 10 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST Search Implementation The BST Find() function takes advantage of the BST data organization: template T* const BST ::Find(const T& toFind) { if (Root == NULL) return NULL; return (FindHelper(toFind, Root)); } template T* const BST ::FindHelper(const T& toFind, BinNodeT * sRoot) { if (sRoot == NULL) return NULL; if (sRoot->Element == toFind) { return &(sRoot->Element); } if (toFind 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.

BSTs Data Structures & OO Development I 11 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST Insert Implementation The public Insert() function is just a stub to call the recursive helper: template bool BST ::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.

BSTs Data Structures & OO Development I 12 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST Insert Helper The InsertHelper() function: template bool BST ::InsertHelper(const T& D, BinNodeT *& sRoot) { if (sRoot == NULL) { // found the location BinNodeT * Temp = new(nothrow) BinNodeT (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.

BSTs Data Structures & OO Development I 13 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens BST Delete Implementation The public Delete() function is very similar to the insertion function: template bool BST ::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…

BSTs Data Structures & OO Development I 14 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Parent 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.

BSTs Data Structures & OO Development I 15 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Some 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.

BSTs Data Structures & OO Development I 16 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Instrumentation The extended BST template: template class BST { protected:... ostream* Log; bool loggingOn;... public:... bool setLogStream(ostream* const L); bool logOn(); bool logOff(); }; template bool BST ::logOn() { if ( Log != NULL ) loggingOn = true; return loggingOn; } template T* BST ::InsertHelper(...) {... if ( Elem Element) ) { if ( loggingOn ) *Log 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…

BSTs Data Structures & OO Development I 17 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Adding 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?

BSTs Data Structures & OO Development I 18 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Iterator-based Find The public Insert() function is just a stub to call the recursive helper: template typename BST ::iterator BST ::Find(const T& toFind) { return FindHelper(toFind, Root); } The public stub simply calls the helper function: template typename BST ::iterator BST ::FindHelper(const T& toFind, BinNodeT * sRoot) { if (sRoot == NULL) return iterator(NULL); if (sRoot->Element == toFind) { return iterator(sRoot); } // recursive descent logic goes next... }

BSTs Data Structures & OO Development I 19 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Iterator 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 AI EJ DG B FH begin() end()

BSTs Data Structures & OO Development I 20 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Client-side Use Here is a somewhat trivial use of the iterator-supplied traversal: BST ::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.

BSTs Data Structures & OO Development I 21 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Balance in a BST D BG EH A C However, a BST with N nodes does not always provide O(log N) search times. B AG CH 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:

BSTs Data Structures & OO Development I 22 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Search 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.

BSTs Data Structures & OO Development I 23 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Cost 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.