Searching and Binary Search Trees CSCI 3333 Data Structures.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

Chapter 12 Binary Search Trees
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
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.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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:
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
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.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
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,
© 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.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
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.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
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.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 25 Binary Search Trees
Binary Search Trees < > =
Recursive Objects (Part 4)
BST Trees
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Tree Traversals – reminder
ITEC 2620M Introduction to Data Structures
Binary Search Trees.
Tree Traversals – reminder
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees < > =
Find in a linked list? first last 7  4  3  8 NULL
Lecture 12 CS203 1.
Binary Search Trees < > =
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Searching and Binary Search Trees CSCI 3333 Data Structures

Acknowledgement  Dr. Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr. Richard F. Gilberg

Searching  Google: era of searching!  Many type of searching: Search engines: best match. Key searching: searching for records with specified key values.  Keys are usually assumed to come from a total order (e.g. integers or strings)  Keys are usually assumed to be unique. No two records have the same key.

Naïve Linear Search Algorithm LinearSearch(r, key) Input: r: an array of record. Each record contains a unique key. key: key to be found. Output: i: the index such that r[i] contains the key, or -1 if not found. foreach element in r with index i if r[i].key = key return i; end foreach return -1

Time Complexity OperationsAverage Case Worst Case FindO(n) InsertO(1) DeleteO(n) Sorted order traversal Need to sort first

Naïve Linear Search  Linear time in finding!  Only suitable when n is very small.  No ‘pre-processing’.

Binary Search  Assume the array has already been sorted in ascending key value.  Example: find(7) m l h m l h m l h l  m  h

Binary Search Algorithm Algorithm BinarySearch(r, key) Input: r: an array of record sorted by its key values. Each record contains a unique key. key: key to be found. Output: i: the index such that r[i] contains the key, or -1 if not found. low = 0; high = r.size – 1 while (low <= high) do mid = (high – low) / 2 if r[mid].key > key then high = mid-1 if r[mid].key = key then return mid; if r[mid].key < key then low = mid + 1 end while return -1;

Time Complexity OperationsAverage Case Worst Case FindO(lg n) InsertO(n) DeleteO(n) Sorted order traversal O(n)

Binary Search  O(lg n) in finding!  Especially suitable for relatively static sets of records.  Question: can we find a searching algorithm of O(1)?  Question: can we find a searching algorithm of O(lg N), where the time complexity for insert and delete is better? Average case Worst case

Binary Search Trees  A binary search tree (BST) is a binary tree which has the following properties: Each node has a unique key value. The left subtree of a node contains only values less than the node's value. The right subtree of a node contains only key values greater than the node's value. Each subtree is itself a binary search tree. Each subtree is itself a binary search tree.

BST  An inorder traversal of a binary search trees visits the keys in increasing order.  Note that there are variations in the formulation of BST. The one defined in the textbook is somewhat different and is best mapped to implement a dictionary. Duplicate key values allowed. Records stored in leaf nodes. Leaf nodes store no key.  The version of BST used here is more basic, easier to understand, and more popular.

Searching a BST  Similar to binary search.  Each key comparison with a node will result in one of these: target search left subtree. target = node key => found! target > node key => search right subtree.  Difference with binary search: do not always eliminate about half of the candidates. The BST may not be balanced.

BST Find Algorithm (Recursive) Algorithm Find(r, target) Input: r: root of a BST. target: key to be found. Output: p: the node in the BST that contains the target; null if not found. if r = null return null; if target = r.key return r; if target < r.key return find(r.leftChild(),target) else // target > r.key return find(r.rightChild(),target) end if

BST Find Algorithm (Iterative) Algorithm Find(r, target) Input: r: root of a BST. target: key to be found. Output: p: the node in the BST that contains the target; null if not found. result = r while result != null do if target = result.key return result; if target < result.key then result = result.leftChild() else // target > r.key result = result.rightChild() end if end while return result

Time Complexity  Depend on the shape of the BST.  Worst case: O(n) for unbalanced trees.  Average case: O(lg N)

BST Insert  Insert a record rec with key rec.key.  Idea: search for rec.key in the BST.  If found => error (keys are supposed to be unique)  If not found => ready to insert (however, need to remember the parent node for insertion).

BST Insert Algorithm (Iterative) Algorithm Insert(r, rec) Input: r: root of a BST. rec: record to be found. Output: whether the insertion is successful. Side Effect: r may be updated. curr = r parent = null //find the location for insertion. while (curr != null) do if curr.key = rec.key then return false // duplicate key: unsuccessful insertion else if rec.key < curr.key then curr = curr.leftChild() parent = curr else // rec.key > curr.key curr = curr.rightChild() parent = curr end if end while

BST Insert (Continue) // create new node for inserrtion newNode = new Node(rec) newNode.parent = parent // Insertion. if parent = null then r = newNode; else if rec.key < parent.key then parent.leftChild = newNode else parent.rightChild = newNode end if return true

BST Delete  More complicated Cannot delete a parent without arranging for the children! Who take care of the children? Grandparent. If there is no grandparent? The root is deleted and a child will become the root. Need to keep track of the parent of the node to be deleted.

BST Delete  Find the node with the key to be deleted.  If not found, deletion is not successful.  Deletion of the node p: No child: simple deletion Single child: connect the parent of p to the single child of p. Both children:  Connect the parent of p to the right child of p.  Connect the left child of p as the left child of the immediate successor of p.

BST Delete Algorithm Algorithm Delete(r, target) Input: r: root of a BST. target: key of the record to be found. Output: whether the deletion is successful. Side Effect: r is updated if the root node is deleted. curr = r; parent = null //find the location for insertion. while (curr != null and curr.key != target) do if target < curr.key then curr = curr.left parent = curr else // target > curr.key curr = curr.right parent = curr end if end while

BST Delete Algorithm (Continue) if curr = null then return false; // not found end if if curr.right = null then // no right child if parent = null then // root deleted r = r.left else if target < parent.key then parent.left = curr.left else parent.right = curr.left end if else

BST Delete Algorithm (Continue) // has right child if curr.left = null then // has a right child but no left child if parent = null then // root deleted r = r.right else if target < parent.key then parent.left = curr.right else parent.right = curr.right end if else

BST Delete Algorithm (Continue) // has both left and right children // find immediate successor. immedSucc = curr.right while (immedSucc.left != null) immedSucc = immedSucc.left end while immedSucc.left = curr.left

BST Delete Algorithm (Continue) if parent = null then // root deleted r = r.right else if target < parent.key then parent.left = curr.right else parent.right = curr.right end if delete curr end if

Time Complexity  Worst case: O(N)  Result in unbalanced tree.

Time Complexity (general BST) OperationsAverage Case Worst Case FindO(lg n)O(n) InsertO(lg n)O(n) DeleteO(lg n)O(n) Sorted order traversal O(n)

Unbalanced BST  Two approaches: Reorganization when the trees become very unbalanced. Use balanced BST  Balanced BST Many potential definitions. Height = O(lg n)

Time Complexity (balanced BST) OperationsAverage Case Worst Case FindO(lg n) InsertO(lg n) DeleteO(lg n) Sorted order traversal O(n)O(lg n)

Questions and Comments?