Searching and Binary Search Trees

Slides:



Advertisements
Similar presentations
Searching: Binary Trees and Hash Tables
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Gordon College Prof. Brinton
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
Fundamentals of Python: From First Programs Through Data Structures
Marc Smith and Jim Ten Eyck
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 6.
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.
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.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
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.
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.
Binary Search Trees Chapter 7 Objectives
Data Structure By Amee Trivedi.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Trees Chapter 11 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Trees Chapter 11 (continued)
BST Trees
Binary Search Trees Chapter 7 Objectives
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
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.
Chapter 21: Binary Trees.
Searching: 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
Searching: Hash Tables
Binary Trees, 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 and Binary Search Trees Chapter 12 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Typical Database Application for an employee phone list Read file into big data structure (list, vector) Ask which record to process Search data structure for record… Retrieve and Process Record change address, phone #, etc Replace record back in data structure Save data back to disk Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Data Structures, pros/cons Vector, array Linked List Random access…vec[k] + Inserts/deletes slow -- Binary Search…fast + Sequential Access -- Inserts/deletes fast + Linear Search…slow -- Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Why binary search trees? Usually outperforms a linear search Disadvantage: Requires a sequential storage…array/vector Not appropriate for linked lists (Why?) The Good News-- It is possible to make a linked structure which can be searched in a binary-like manner…enter Binary Search Trees! Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Data Structures, pros/cons Vector, array Linked List Binary Search Tree Random access + Insert/del slow -- Binary Search + Seq Access -- Ins/del fast + Lin Search -- Log2 access + Ins/del fast + Bin Srch + Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Contents 12.1 Review of Linear Search and Binary Search 12.2 Introduction to Binary Trees 12.3 Binary Trees as Recursive Data Structures 12.4 Binary Search Trees 12.5 Case Study: Validating Computer Logins 12.6 Threaded Binary Search Trees (Optional) 12.7 Hash Tables – in 2 weeks Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Objectives Look at ways to search collections of data Begin with review of linear and binary search Introduce trees in general and then focus on binary trees, looking at some of their applications and implementations See how binary trees can be viewed as recursive data structures and how this simplifies algorithms for some of the basic operations Develop a class to implement binary search trees using linked storage structure for the data items (Optional) Look briefly at how threaded binary search trees facilitate traversals Introduce the basic features of hash tables and examine some of the ways they are implemented Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Linear Search Vector based search function template <typename t> void LinearSearch (const vector<t> &v, const t &item, bool &found, int &loc) { found = false; loc = 0; for ( ; ; ) { if (found || loc == v.size()) return; if (item == v[loc]) found = true; else loc++; } } Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

In both cases, the worst case computing time is still O(n) Linear Search Singly-linked list based search function template <typename t> void LinearSearch (NodePointer first, const t &item, bool &found, int &loc) { found = false; locptr = first; for ( ; ; ) { if (found || locptr == NULL) return; if (item == locptr->data) found = true; else locptr++; } } In both cases, the worst case computing time is still O(n) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Binary Search Binary search function for vector template <typename t> void BinarySearch (const vector<t> &v, const t &item, bool &found, int &loc) { found = false; 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 == v[loc] */ } Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Binary Search Usually outperforms a linear search Disadvantage: Requires a sequential storage Not appropriate for linked lists (Why?) It is possible to use a linked structure which can be searched in a binary-like manner Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Binary Search Tree Redraw the previous structure so that it has a treelike shape – a binary tree Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Trees Tree terminology Root node Children of the parent (3) Siblings to each other Leaf nodes Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Morse code Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Array Representation of Binary Trees Store the ith node in the ith location of the array Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Array Representation of Binary Trees Works OK for complete trees, not for sparse trees Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Linked Representation of Binary Trees Example Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 … Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 The inductive step Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Traversal Order Given expression A – B * C + D Represent each operand as The child of a parent node Parent node, representing the corresponding operator Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Traversal Order Inorder traversal produces infix expression A – B * C + D Preorder traversal produces the prefix expression + - A * B C D Postorder traversal produces the postfix or RPN expression A B C * - D + Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

ADT Binary Search Tree (BST) Collection of Data Elements binary tree 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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 View BST class template, Fig. 12-1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

BST Traversals Note that recursive calls must be made To left subtree To right subtree Must use two functions Public method to send message to BST object Private auxiliary method that can access BinNodes and pointers within these nodes Similar solution to graphic output Public graphic method Private graphAux method Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 View search() Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Inserting into a BST Insert function View insert() function Uses modified version of search to locate insertion location or already existing item Pointer parent trails search pointer locptr, keeps track of parent node Thus new node can be attached to BST in proper place View insert() function R Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Recursive Deletion Three possible cases to delete a node, x, from a BST 1. The node, x, is a leaf Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Recursive Deletion 2. The node, x has one child Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Recursive Deletion x has two children Delete node pointed to by xSucc as described for cases 1 and 2 Replace contents of x with inorder successor K View remove() function Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

BST Class Template View complete binary search tree template, Fig. 12.7 View test program for BST, Fig. 12.8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Problem of Lopsidedness Tree can be balanced each node except leaves has exactly 2 child nodes Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Problem of Lopsidedness Trees can be unbalanced not all nodes have exactly 2 child nodes Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Case Study: Validating Computer Logins Consider a computer system which logs in users Enter user name Enter password System does verification The structure which contains this information must be able to be searched rapidly Must also be dynamic Users added and removed often Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Case Study: Validating Computer Logins Design Create class, UserInfo which contains ID, password Build BST of UserInfo objects Provide search capability for matching ID, password Display message with results of validity check View source code, Fig. 12.9 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Threaded Binary Search Trees Use special links in a BST These threads provide efficient nonrecursive traversal algorithms Build a run-time stack into the tree for recursive calls Given BST Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Threaded Binary Search Trees Note the sequence of nodes visited Left to right A right threaded BST Whenever a node, x, with null right pointer is encountered, replace right link with thread to inorder successor of x Inorder successor of x is its parent if x is a left child Otherwise it is nearest ancestor of x that contains x in its left subtree Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Threaded Binary Search Trees Traversal algorithm 1. Initialize a pointer, ptr, to the root of tree 2. While ptr != null a. While ptr->left not null replace ptr by ptr->left b. Visit node pointed to by ptr c. While ptr->right is a thread do following i. Replace ptr by ptr->right ii. Visit node pointed to by ptr d. Replace ptr by ptr->right End while Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Threaded Binary Search Trees public: DataType data; bool rightThread; BinNode * left; BinNode * right; // BinNode constructors // Default -- data part is default DataType value // -- right thread is false; both links are null BinNode() : rightThread(false, left(0), right(0) { } //Explicit Value -- data part contains item; rightThread // -- is false; both links are null BinNode (DataType item) : data(item), rightThread(false), left(0), right(0) { } }; // end of class BinNode declared Additional field, rightThread required Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3