CMSC 341 Binary Search Trees 5/8/2019.

Slides:



Advertisements
Similar presentations
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Trees, Binary Trees, and Binary Search Trees. 2 Trees * Linear access time of linked lists is prohibitive n Does there exist any simple data structure.
BST Data Structure A BST node contains: A BST contains
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
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.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
CSCS-200 Data Structure and Algorithms Lecture
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
UNIT III TREES.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Binary Tree Applications
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search 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
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Trees, Binary Trees, and Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 341 Binary Search Trees 11/19/2018.
Find in a linked list? first last 7  4  3  8 NULL
CMSC 341 Introduction to Trees.
CMSC 341 Introduction to Trees.
Lecture No.16 Data Structures Dr. Sohail Aslam.
CMSC 341 Binary Search Trees.
CMSC 341 Binary Search Trees 1/3/2019.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Binary Trees, Binary Search Trees
CMSC 341 Binary Search Trees 2/23/2019.
CMSC 341 Binary Search Trees.
Mark Redekopp David Kempe
Trees.
CMSC 341 Binary Search Trees 5/28/2019.
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
CMSC 341 Binary Search Trees 2/21/2006.
Binary Trees, Binary Search Trees
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

CMSC 341 Binary Search Trees 5/8/2019

Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the value stored in the left child node is less than the value at v and the value stored in the right child is greater. The elements in the BST must be comparable. Duplicates are not allowed. 5/8/2019

BST Implementation The SearchTree ADT A search tree is a binary search tree in which are stored homogeneous elements with no duplicates. It is dynamic. The elements are ordered in the following ways inorder -- as dictated by operator< preorder, postorder, levelorder -- as dictated by the structure of the tree Each BST maintains a simple object, known as ITEM_NOT_FOUND, that is guaranteed to not be an element of the tree. ITEM_NOT_FOUND is provided to the constructor. (author’s code) 5/8/2019

BinarySearchTree class template <class Comparable> class BinarySearchTree { public: BinarySearchTree(const Comparable &notFnd); BinarySearchTree (const BinarySearchTree &rhs); ~BinarySearchTree(); const Comparable &findMin() const; const Comparable &findMax() const; const Comparable &find(const Comparable &x) const; bool isEmpty() const; void printTree() const; void makeEmpty(); void insert (const Comparable &x); void remove (const Comparable &x); const BinarySearchTree &operator=(const BinarySearchTree &rhs); 5/8/2019

BinarySearchTree class (cont) private: BinaryNode<Comparable> *root; const Comparable ITEM_NOT_FOUND; const Comparable& elementAt(BinaryNode<Comparable> *t) const; void insert (const Comparable &x, BinaryNode<Comparable> * &t) const; void remove (const Comparable &x, BinaryNode<Comparable> * &t) const; BinaryNode<Comparable> *findMin(BinaryNode<Comparable> *t const; BinaryNode<Comparable> *findMax(BinaryNode<Comparable> *t)const; BinaryNode<Comparable> *find(const Comparable &x, BinaryNode<Comparable> *t) const; void makeEmpty(BinaryNode<Comparable> *&t) const; void printTree(BinaryNode<Comparable *t) const; BinaryNode<Comparable> *clone(BinaryNode<Comparable> *t)const; }; 5/8/2019

BinarySearchTree Implementation template <class Comparable> const Comparable &BinarySearchTree<Comparable> :: find(const Comparable &x) const { return elementAt(find (x, root)); } elementAt(BinaryNode<Comparable> *t) const { return t == NULL ? ITEM_NOT_FOUND : t->element; BinaryNode<Comparable> *BinarySearchTree<Comparable> :: find(const Comparable &x, BinaryNode<Comparable> *t) const { if (t == NULL) return NULL; else if (x < t->element) return find(x, t->left); else if (t->element < x) return find(x, t->right); else return t; // Match Public find operation calls private find operation. The private find operation recursively searches the tree. If element is found, a pointer to its node is returned. Otherwise, NULL is returned. If NULL is returned to the public find method, it then returns Item_Not_Found. Otherwise, it returns the element of the node. What about strategy of returning Item_Not_Found? Alternative strategies mentioned in text: find throws an exception find has a bool reference paramenter to indicate success/failure Discuss pros/cons Another approach (like that for lists) Find returns an iterator that points to the found element. If the element is not found, the iterator is PastEnd.

Performance of find Search in randomly built BST is O(lg n) on average but generally, a BST is not randomly built Asymptotic performance is O(h) in all cases Elements inserted in the order: 50,40,30,20,10 avg search time: 0+1+2+3+4+5 = 10 Elements inserted in order: 30,20,10,40,50 avg search time: 0+1+2+1+2=6 How many permutations of 5 things? 5! = 120 must look at average IPL of the120 trees 5/8/2019

Predecessor in BST Predecessor of a node v in a BST is the node that holds the data value that immediately precedes the data at v in order. Finding predecessor v has a left subtree then predecessor must be the largest value in the left subtree (the rightmost node in the left subtree) v does not have a left subtree predecessor is the first node on path back to root thatdoes not have v in its left subtree 5/8/2019

Successor in BST Successor of a node v in a BST is the node that holds the data value that immediately follows the data at v in order. Finding Successor v has right subtree successor is smallest value in right subtree (the leftmost node in the right subtree) v does not have right subtree successor is first node on path back to root that does not have v in its right subtree 5/8/2019

The remove Operation } template <class Comparable> void BinarySearchTree<Comparable>:: remove(const Comparable &x, BinaryNode<Comparable> *&t) const { if (t == NULL) return; // item not found, do nothing if (x < t->element) remove(x, t->left); else if (t->element < x) remove(x, t->right); else if ((t->left != NULL) && (t->right != NULL)) { t->element = (findMin (t->right))->element; remove (t->element, t->right);} else { BinaryNode<Comparable> *oldNode = t; t = (t->left != NULL) ? T->left : t->right; delete oldNode; } } Constraint: delete a node, maintain BST property 1. Deleting a leaf. Easy. Just do it. BST property is not affected. 2. Deleting a non-leaf node v a. v has no left child -- replace v by its right child b. v has no right child -- replace v by its left child c. v has both left and right children, either: 1. Replace data in v by data of predecessor and delete predecessor 2. Replace data in v by data in successor and delete successor Note that private remove method takes a reference to a pointer to a node. This allows the pointer to be changed to point to another node. If this were not a reference to a pointer, the pointer would be passed by value and could not be changed inside the method ie the last else clause t = (t->left != NULL) ? T->left : t->right; would change to t inside the method, but not cause any change after the method returns. Asymptotic performance of remove is similar to that for find. It’s O(n) worst case and O(lg n) best/average case. Remove traverses tree from root to some leaf.

The insert Operation template <class Comparable> void BinarySearchTree<Comparable>:: insert(const Comparable &x) // public insert( ) { insert (x, root); // calls private insert( ) } insert(const Comparable &x, BinaryNode<Comparable> *&t) const { if (t == NULL) t = new BinaryNode<Comparable>(x, NULL, NULL); else if (x < t->element) insert (x, t->left); else if (t->element < x) insert (x, t->right); else ; // Duplicate; do nothing Method: recursively traverses the tree looking for a null pointer at the point of insertion. If found, constructs a new node and stitches it into the tree. If duplicate found, simply returns with no insertion done. Question: Suppose Item_Not_Found object is inserted? The insert code allows it. This would mean the find operation could no longer indicate an unsuccessful search. Esoteric but possible -- and a reason not to use the text’s approach to indicating unsuccessful search. Asymptotic performance of insert = O(n) as usual.

Implementation of makeEmpty template <class Comparable> void BinarySearchTree<Comparable>:: makeEmpty() // public makeEmpty () { makeEmpty(root); // calls private makeEmpty ( ) } makeEmpty(BinaryNode<Comparable> *&t) const if (t != NULL) { // post order traversal makeEmpty (t->left); makeEmpty (t->right); delete t; t = NULL; Must hang onto each node until its children are deleted Postorder traversal is used in makeEmpty Asymptotic performance = O(n) Traversals of theentire tree (pre, post, in, level) require that each node be visited. Since n nodes are visited, operation is O(n) 5/8/2019

Tree Iterators Could provide separate iterators for each desired order Iterator<T> *GetInorderIterator(); Iterator<T> *GetPreorderIterator(); Iterator<T> *GetPostorderIterator (); Iterator<T> *GetLevelorderIterator (); Approach 1: Easy to code using existing list iterators. But: O(n) storage for the list (can do better) Difficult to free list storage when iterator is deleted (memory leak) 5/8/2019

Tree Iterator Implementation Approach 1: Store traversal in list. Return list iterator for list. Iterator<T> BinaryTree::GetInorderIterator() { List<T> *lst = new ArrayList<T>; FullListInorder(list, getRoot()); return list->GetIterator(); } void FillListInorder(ArrayList<T> *lst, Bnode<T> *node) { if (node == NULL) return; FillListInorder(list, node->left); lst->Append(node->data); FillListInorder(lst, node->right); 5/8/2019

Tree Iterators (cont) Approach 2: store traversal in stack to mimic recursive traversal template <class T> class InOrderIterator : public Iterator { private: Stack<T> _stack; BinaryTree<T> *_tree; public: InOrderIterator(BinaryTree<T> *t); bool hasNext() {return (!_stack.isEmpty()); } T Next(); }; 5/8/2019

Tree Iterators (cont’d) template <class T> InOrderIterator<T>::InOrderIterator(BinaryTree<T> *t) { _tree = t; Bnode<T> *v = t->getRoot(); while (v != NULL) { _stack.Push(v); // push root v = v->left; // and all left descendants } Example: 50 30 80 20 40 70 90 O(h) stoarage (=O(lg n) in best and avg cases) Easy to free stack (destroyed when iterator is destroyed) More difficult to code Similar code for re-order and post-order 5/8/2019

Tree Iterators (cont’d) template <class T> T InOrderIterator<T>::Next() { Bnode<T> *top = _stack.Top(); _stack.Pop(); Bnode<T> *v = top->right; while (v != NULL) { _stack.Push(v); // push right child v = v->left; // and all left descendants } return top->element; 5/8/2019