Searching: Binary Trees

Slides:



Advertisements
Similar presentations
Searching: Binary Trees and Hash Tables
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Gordon College Prof. Brinton
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 6.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Searching. Motivation Find parts for a system Find an address for name Find a criminal –fingerprint/DNA match Locate all employees in a dept. based on.
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,
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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 Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Search: Binary Search Trees Dr. Yingwu Zhu. Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n Assume == and.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
Chapter 12 – Data Structures
Trees Chapter 15.
Searching and Binary Search Trees
BST Trees
UNIT III TREES.
Binary Search Tree (BST)
CMSC 341 Introduction to Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 7 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.
Chapter 21: Binary Trees.
Trees Chapter 15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Find in a linked list? first last 7  4  3  8 NULL
CMSC 341 Binary Search Trees.
CMSC 202 Trees.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Searching: Hash Tables
Binary Trees, Binary Search Trees
CMSC 341 Binary Search Trees.
Heaps and priority queues
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Searching: Binary Trees Dr. Yingwu Zhu

Review of Linear Search Collection of data items to be searched is organized in a list x1, x2, … xn Assume == and < operators defined for the type Linear search begins with item 1 continue through the list until target found or reach end of list

Linear Search Vector based search function template <typename t> void LinearSearch (const vector<t>& v, const t &item, boolean &found, int &loc) { found = false; loc = 0; for ( ; ; ) { if (found || loc == v.size()) return; if (item == v[loc]) found = true; else loc++; }

Linear Search Singly-linked list based search function template <typename t> void LinearSearch (NodePointer first, const t &item, bool &found, int &loc) { NodePointer locptr=first; for{loc=0; !found && locptr!=NULL; locptr=locptr->next) { if (item == locptr->data) found = true; else loc++; }

Linear Search Iterator-based linear search template <typename t> void LinearSearch (const vector<t>& v , const t &item, bool &found, int &loc) { found = false; loc = 0; for (vector<t>::iterator it=v.begin(); !found && it != v.end(); it++) { if (*it == item) found = true; else loc++; }

Binary Search Two requirements?

Binary Search Two requirements The data items are in ascending order (can they be in decreasing order?? ) Direct access of each data item for efficiency (why linked-list is not good!)

Binary Search Binary search function for vector template <typename t> void LinearSearch (const vector<t> &v, const t &item, bool &found, int &loc) { found = false; loc = 0; int first = 0; last = v.size() - 1; for ( ; ; ){ if (found || first > last) return; loc = (first + last) / 2; if (item < v[loc]) last = loc - 1; else if (item > v[loc]) first = loc + 1; else found = true; // item found }

Binary Search vs. Linear Search Usually outperforms Linear search O(logn) vs. O(n) Disadvantages Sorted list of data items Direct access of storage structure, not good for linked-list Good news: It is possible to use a linked structure which can be searched in a binary-like manner

Binary Search Tree Consider the following ordered list of integers Examine middle element Examine left, right sublist (maintain pointers) (Recursively) examine left, right sublists 80 66 62 49 35 28 13

Binary Search Tree Redraw the previous structure so that it has a treelike shape – a binary tree

Trees A data structure which consists of If the tree is nonempty a finite set of elements called nodes or vertices a finite set of directed arcs which connect the nodes If the tree is nonempty one of the nodes (the root) has no incoming arc every other node can be reached by following a unique sequence of consecutive arcs (or paths)

Trees Tree terminology Root node Children of the parent (3) Siblings to each other Leaf nodes

Binary Trees Each node has at most two children Useful in modeling processes where a comparison or experiment has exactly two possible outcomes the test is performed repeatedly Example multiple coin tosses encoding/decoding messages in dots and dashes such as Mores code

Array Representation of Binary Trees Store the ith node in the ith location of the array

Array Representation of Binary Trees Works OK for complete trees, not for sparse trees

Some Tree Definition, p656 Complete trees Balanced trees Each level is completely filled except the bottom level The leftmost positions are filled at the bottom level Array storage is perfect for them Balanced trees Binary trees |left_subtree – right_subtree|<=1 Tree Height/Depth: # of levels

Tree Question? A complete tree must be a balanced tree? Give a node with position i in a complete tree, what are the positions of its child nodes?

Linked Representation of Binary Trees Uses space more efficiently Provides additional flexibility Each node has two links one to the left child of the node one to the right child of the node if no child node exists for a node, the link is set to NULL

Linked Representation of Binary Trees Example

Binary Trees as Recursive Data Structures A binary tree is either empty … or Consists of a node called the root root has pointers to two disjoint binary (sub)trees called … right (sub)tree left (sub)tree Anchor Inductive step Which is either empty … or … Which is either empty … or …

Tree Traversal is Recursive The "anchor" If the binary tree is empty then do nothing Else N: Visit the root, process data L: Traverse the left subtree R: Traverse the right subtree If the binary tree is empty then do nothing Else N: Visit the root, process data L: Traverse the left subtree R: Traverse the right subtree The inductive step

Traversal Order Three possibilities for inductive step … Left subtree, Node, Right subtree the inorder traversal Node, Left subtree, Right subtree the preorder traversal Left subtree, Right subtree, Node the postorder traversal

ADT Binary Search Tree (BST) Collection of Data Elements binary tree BST property: for each node x, value in left child of x < value in x < in right child of x Basic operations Construct an empty BST Determine if BST is empty Search BST for given item

ADT Binary Search Tree (BST) Basic operations (ctd) Insert a new item in the BST Maintain the BST property Delete an item from the BST Traverse the BST Visit each node exactly once The inorder traversal must visit the values in the nodes in ascending order

BST template <typename T> class BST { public: BST() : myRoot(0) {} bool empty() { return myRoot==NULL; } private: class BinNode { public: T data; BinNode* left, right; BinNode() : left(0), right(0) {} BinNode(T item): data(item), left(0), right(0) {} }; //end of BinNode typedef BinNode* BinNodePtr; BinNodePtr myRoot; };

BST operations Inorder, preorder and postorder traverasl (recursive) Non-recursive traversal

BST Traversals, p.670 Why do we need two functions? Public: void inorder(ostream& out) Private: inorderAux(…) Do the actual job To the left subtree and then right subtree Can we just use one function? Probably NO

Non-recursive traversals Let’s try non-recusive inorder Any idea to implement non-recursive traversals? What ADT can be used as an aid tool?

Non-recursive traversals Basic idea Use LIFO data structure: stack #include <stack> (provided by STL) Chapter 9, google stack in C++ for more details, members and functions Useful in your advanced programming

Non-recursive inorder Let’s see how it works given a tree Step by step

Non-recursive inorder 1. Set current node pointer ptr = myRoot 2. Push current node pointer ptr and all the leftmost nodes on its subtree into a stack until meeting a NULL pointer 3. Check if the stack is empty or not. If yes, goto 5, otherwise goto 4 4. Take the top element in stack and assign it to ptr, print our ptr->data, pop the top element, and set ptr = ptr->right (visit right subtree), goto 2 5. Done

Non-recursive inorder template <typename DataType> void non_recur_inorder(ostream& out) { if (!myRoot) return; // empty BST BinNodePointer ptr = myRoot; stack<BST<DataType>::BinNodePointer> s; // empty stack for ( ; ; ) { while (ptr) { s.push(ptr); ptr = ptr->left; } // leftmost nodes if (s.empty()) break; // done ptr = s.top(); s.pop(); out << ptr->data << “ “; ptr = ptr->right; // right substree }

Non-recursive traversals Can you do it yourself? Preorder? Postorder?

BST Searches Search begins at root If that is desired item, done If item is less, move down left subtree If item searched for is greater, move down right subtree If item is not found, we will run into an empty subtree

BST Searches Write recursive search Write Non-recursive search algorithm bool search(const T& item) const

Insertion Operation Basic idea: Use search operation to locate the insertion position or already existing item Use a parent point pointing to the parent of the node currently being examined as descending the tree

Insertion Operation Non-recursive insertion Recursive insertion Go through insertion illustration p.677 Initially parent = NULL, locptr = root; Location termination at NULL pointer or encountering the item Parent->left/right = item

Recursive Insertion See p. 681 Thinking! Why using & at subtreeroot

Deletion Operation Three possible cases to delete a node, x, from a BST 1. The node, x, is a leaf

Deletion Operation 2. The node, x has one child

Deletion Operation x has two children Replace contents of x with inorder successor Delete node pointed to by xSucc as described for cases 1 and 2 K View remove() function

Question? Why do we choose inorder predecessor/successor to replace the node to be deleted in case 3?

Implement Delete Operation void delete(const T& item) //remove the specified item if it exists Assume you have this function template <typename T> void BST<T>::search2(const T& item, bool& found, BST<T>::BinNodePtr& locptr, BST<T>::BinNodePtr& parent) const { locptr = myRoot; parent = NULL; found = false; while(!found && locptr) { if (item < locptr->data) { parent = locptr; locptr = locptr->left; } else if (item > locptr->data) { parent = locptr; locptr = loc->right; } else found = true; } //end while }

Implement Delete Operation Search the specified item on the tree. If not found, then return; otherwise go to 2. Check if the node to be deleted has two child nodes (case 3). If so, find the inorder successor/predecessor, replace the data, and then make the successor/predecessor node the one to be deleted In order to delete the node, you should keep track of its parent node in step 1 and 2. Delete the node according to the first two simple cases. Be careful with parent Release the memory of the node deleted

delete(const T& item) { BinNodePtr parent, x; bool found; search2(item, found, x, parent); //search item on the tree if (!found) return; if (x->left && x->right) { //the case 3 with two child nodes BinNodePtr xsucc =x->right; parent = x; while (xsucc->left) { //find inorder successor parent = xsucc; xsucc = xsucc->left; } x->data = xsucc->data; x = xsucc; } //end if BinNodePtr subtree = x->left; if (!subtree) subtree = x->right; if (!parent) myRoot = subtree; else { if (x == parent->left) parent->left = subtree; else parent->right = subtree; } delete x;

BST Class Template View complete binary search tree template, Fig. 12.7 View test program for BST, Fig. 12.8 No such functions Copy constructor, assignment operator So we can NOT do what?

In-class Exercises #1 Write a recursive member function height() for the BST class template to return the height of the BST int height(); // See how concise your implementation could be?

In-class Exercises #2 Write a recursive member function leafCount() for the BST class template to return the number of leaf nodes of the BST int leafCount(); // See how concise your implementation could be?

Questions? What is T(n) of search algorithm in a BST? Why? What is T(n) of insert algorithm in a BST? Other operations?

Problem of Lopsidedness The order in which items are inserted into a BST determines the shape of the BST Result in Balanced or Unbalanced trees Insert O, E, T, C, U, M, P Insert C, O, M, P, U, T, E

Balanced

Unbalanced

Problem of Lopsidedness Trees can be totally lopsided Suppose each node has a right child only Degenerates into a linked list Processing time affected by "shape" of tree