Download presentation
Presentation is loading. Please wait.
Published byScarlett Joseph Modified over 9 years ago
1
Searching and Binary Search Trees CSCI 3333 Data Structures
2
Acknowledgement Dr. Yue Mr. Charles Moen Dr. Wei Ding Ms. Krishani Abeysekera Dr. Michael Goodrich Dr. Richard F. Gilberg
3
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.
4
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
5
Time Complexity OperationsAverage Case Worst Case FindO(n) InsertO(1) DeleteO(n) Sorted order traversal Need to sort first
6
Naïve Linear Search Linear time in finding! Only suitable when n is very small. No ‘pre-processing’.
7
Binary Search Assume the array has already been sorted in ascending key value. Example: find(7) 13457 8 91114161819 1 3 457891114161819 134 5 7891114161819 1345 7 891114161819 0 0 0 0 m l h m l h m l h l m h
8
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;
9
Time Complexity OperationsAverage Case Worst Case FindO(lg n) InsertO(n) DeleteO(n) Sorted order traversal O(n)
10
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
11
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.
12
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.
13
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.
14
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
15
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
16
Time Complexity Depend on the shape of the BST. Worst case: O(n) for unbalanced trees. Average case: O(lg N)
17
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).
18
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
19
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
20
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.
21
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.
22
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
23
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
24
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
25
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
26
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
27
Time Complexity Worst case: O(N) Result in unbalanced tree.
28
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)
29
Unbalanced BST Two approaches: Reorganization when the trees become very unbalanced. Use balanced BST Balanced BST Many potential definitions. Height = O(lg n)
30
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)
31
Questions and Comments?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.