Chapter 14 The Binary Search Tree



Similar documents
Binary Search Trees (BST)

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Analysis of Algorithms I: Binary Search Trees

Ordered Lists and Binary Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Data Structures and Algorithms

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Algorithms Chapter 12 Binary Search Trees

TREE BASIC TERMINOLOGIES

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Converting a Number from Decimal to Binary

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Binary Trees and Huffman Encoding Binary Search Trees

How To Create A Tree From A Tree In Runtime (For A Tree)

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

The ADT Binary Search Tree

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Binary Search Trees CMPSC 122

Binary Heaps. CSE 373 Data Structures

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Heap Algorithms

An Evaluation of Self-adjusting Binary Search Tree Techniques

7.1 Our Current Model

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

From Last Time: Remove (Delete) Operation

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Symbol Tables. Introduction

Alex. Adam Agnes Allen Arthur

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Binary Search Trees. Ric Glassey

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

OPTIMAL BINARY SEARCH TREES

Rotation Operation for Binary Search Trees Idea:

Data Structure [Question Bank]

Exam study sheet for CS2711. List of topics

Binary Search Trees 3/20/14

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

6 March Array Implementation of Binary Trees

Persistent Binary Search Trees

Algorithms and Data Structures

Why Use Binary Trees?

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Introduction to Data Structures and Algorithms

Analysis of Algorithms I: Optimal Binary Search Trees

Java Software Structures

PES Institute of Technology-BSC QUESTION BANK

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CSE 326: Data Structures B-Trees and B+ Trees

Data Structure with C

Chapter 8: Binary Trees

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

DATA STRUCTURES USING C

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

A Comparison of Dictionary Implementations

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Heaps & Priority Queues in the C++ STL 2-3 Trees

Lecture Notes on Binary Search Trees

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Cpt S 223. School of EECS, WSU

Algorithms and Data Structures

Sample Questions Csci 1112 A. Bellaachia

Lecture Notes on Binary Search Trees

Any two nodes which are connected by an edge in a graph are called adjacent node.

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Introduction to data structures

Linked Lists Linked Lists, Queues, and Stacks

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

International Journal of Software and Web Sciences (IJSWS)

Lecture 2 February 12, 2003

External Memory Geometric Data Structures

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structures Fibonacci Heaps, Amortized Analysis

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

2. FINDING A SOLUTION

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structures and Data Manipulation

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

Lecture 4: Balanced Binary Search Trees

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Parallelization: Binary Tree Traversal

Laboratory Module 6 Red-Black Trees

Transcription:

Chapter 14 The Binary Search Tree In Chapter 5 we discussed the binary search algorithm, which depends on a sorted vector. Although the binary search, being in O(lg(n)), is very efficient, inserting a new element into a vector without destroying its sorted ordering is relatively inefficient by comparison. In this chapter we will examine the binary search tree, a linked binary tree container that supports efficient searching and efficient insertion. 14.1 Our Current Model Our current software design appears below in Figure 14.1.

144 Chapter 14 The Binary Search Tree Fig. 14.1. Our current design, in which BinarySearchTree specializes BinaryTree

145 14.2 The Binary Search Tree ADT A binary search tree (BST) is a totally ordered binary tree. The BST's total ordering does the heap's partial ordering one better; not only is there a relationship between a BST node and its children, but there is also a definite relationship between the children. In a BST, the value of a node's left child is less than the value of the node itself, and the value of a node's right child is greater than or equal to the value of the node. Consequently, the value of a node's left child is always less than the value of its right child. We will soon see how this total ordering makes the BST's fundamental operations efficient in the average case. The BST operations are shown in the following ADT specification. binary search tree (BST): a totally ordered binary tree operations: clear() Make the collection empty. find(element) Search for the given element. height() What is the height of the tree? inordertraverse(processor) Perform an inorder traversal of the tree, applying some operation to each node. insert(element) Add the given element to the collection. isempty() Is the collection empty? maximum() Get the maximum element of the collection. minimum() Get the minimum element of the collection. postordertraverse(processor) - Perform a postorder traversal of the tree, applying some operation to each node. predecessor(element) Get the inorder predecessor of the given element. preordertraverse(processor) - Perform a preorder traversal of the tree, applying some operation to each node. remove(element) Remove the given element from the collection. size() How many elements are in the collection? successor(element) Get the inorder successor of the given element. 14.3 BST Operations Now we will have a look at the insert, find, and remove operations of the BST.

146 Chapter 14 The Binary Search Tree 14.3.1 Insertion into a BST Let us insert into a BST the following values, in the order given: 5, 3, 4, 8, 1, 6, 4. Since the tree is initially empty, the first value, 5, becomes the new tree's root. The next value, 3, is less than 5, and so the 3 becomes 5's left child. The third value, 4, is less than 5, which means that the 4 must be placed somewhere in the root's left subtree. Thus we move to 5's left child, the node containing 3. Since 4 is greater than 3 and the node containing 3 has no right child, a new node containing 4 becomes 3's right child. The next value, 8, is greater than the value at the root, and so the 8 must be placed somewhere in the root's right subtree. Since the root does not yet have a right child, a new node containing 8 becomes the root's right child.

147 The next value to be inserted is 1. We again start at the root and move left or right depending on the relationship between the value to be inserted and the value at the current node. Since 1 is less than 5, we move to 5's left child. The 1 is also less than 3, the value of the current node. That node has no left child, and so a new node containing 1 is placed as 3's left child. The sixth value, 6, is greater than the root value, and so we move to the root's right child, 8. Since 6 is less than 8 and 8 has no left child, the 6 becomes 8's left child.

148 Chapter 14 The Binary Search Tree Insertion of the last value, a second 4, requires that we go left and then right, arriving at the first 4. Since 4 is greater than or equal to 4, the second 4 becomes the right child of the first. Thus we arrive at the final tree shown below. Fig. 14. 1 4.2. The BST that results from the insertion of 5, 3, 4, 8, 1, 6, and 4, in that order 14.3.2 Finding an Element of a BST Searching a BST involves the same navigation that is employed for insertion. A search begins at the root, and left or right "turns" are made until the target element is located or until the search encounters an empty child, in which case the target element is not a member of the collection. 14.3.3 Removing from a BST Removal is by far the most complex of the BST operations, owing to the fact that a number of cases must be handled. Pseudocode for the remove operation appears below.

149 REMOVING AN ELEMENT OF A BST 1. Find the node to be removed. If no such node exists, terminate. 2. (1) if (the node to be removed is a leaf) If the target node has a parent, i.e., if the target node is not the root, then overwrite the parent's reference to the target node with null, unlinking the target node from the tree. (2) else if (the target node has no left child) Overwrite the target node's data portion and the links to its children with those of its right child, effectively transforming the node into its right child. (3) else if (the target node has no right child) Transform the target node into its left child. (4) else // the target node has two children. Replace the target node's data portion with that of its successor, which is the minimum node of the target node's right subtree. Then delete the successor, to which case (1) or case (2) must apply. You should verify the correctness of this algorithm by applying it to the BST of Figure 14.2. Delete elements 1, 4, 8, and 5, restoring the tree to its original form after each deletion. 14.4 BST Implementation The code for class BinarySearchTree is shown below. Most of the work is done recursively, following the pattern used in implementing class BinaryTree.

150 Chapter 14 The Binary Search Tree public class BinarySearchTree extends BinaryTree // A BST needs Comparable nodes because a BST is totally // ordered. public static class BSTNode extends BinaryTreeNode implements Comparable public BSTNode(Comparable dat) super(dat); public BSTNode(Comparable dat, BSTNode lc, BSTNode rc, BSTNode par) super(dat, lc, rc, par); public int compareto(object o) Comparable c = (Comparable)((BSTNode)o).data; return ((Comparable)data).compareTo(c); // The following method is the recursive helper for find(). It // returns the found node or null. protected BSTNode findhelper(bstnode current, BSTNode target) if (current == null) // If we have left the tree, then // the target was not found. // If the target node is less than the current node, then // turn left. if (target.compareto(current) < 0) return findhelper((bstnode)current.leftchild, target); // If the target node is greater than the current node, // then turn right. if (target.compareto(current) > 0) return findhelper((bstnode)current.rightchild, target); // Otherwise the current node is the target node. return current; public Comparable find(comparable target) BSTNode found = findhelper((bstnode)root, new BSTNode(target)); if (found == null) return (Comparable)found.data;

151 // The following method is the recursive helper for insert(). protected BSTNode inserthelper(bstnode current, BSTNode newnode) // If we have found the correct location for the new node, // then return the reference to the new node, linking it // to the tree. if (current == null) numitems++; return newnode; // If the new node is less than the current node, then go // left. if (newnode.compareto(current) < 0) current.leftchild = inserthelper((bstnode)current.leftchild, newnode); current.leftchild.parent = current; else // Otherwise go right. current.rightchild = inserthelper((bstnode)current.rightchild, newnode); current.rightchild.parent = current; return current; public void insert(comparable newitem) root = inserthelper((bstnode)root, new BSTNode(newItem)); /* The following method is the helper for maximum(). This method is not recursive because the maximum node of a BST is easy to find using a loop: go right until the rightmost node is found. */ protected BSTNode maxhelper(bstnode current) if (current == null) while (current.rightchild!= null) current = (BSTNode)current.rightChild; return current; public Comparable maximum() BSTNode max = maxhelper((bstnode)root); if (max == null) return (Comparable)max.data;

152 Chapter 14 The Binary Search Tree // The minimum node of a BST is the leftmost node. protected BSTNode minhelper(bstnode current) if (current == null) while (current.leftchild!= null) current = (BSTNode)current.leftChild; return current; public Comparable minimum() BSTNode min = minhelper((bstnode)root); if (min == null) return (Comparable)min.data; /* The inorder predecessor of node n is: */ 1. the maximum node of n's left subtree, provided that n has a left subtree. 2. the first ancestor of n such that n is in the ances tor's right subtree. public Comparable predecessor(comparable target) BSTNode found = findhelper((bstnode)root, new BSTNode(target)); if (found == null) if (found.leftchild!= null) return (Comparable)maxHelper((BSTNode)found.leftChild).data; BSTNode parent = (BSTNode)found.parent; while (parent!= null && parent.compareto(found) > 0) parent = (BSTNode)parent.parent; if (parent == null) return (Comparable)parent.data;

153 // Method removehelper() implements the algorithm specified // in 14.3.3. protected BSTNode removehelper(bstnode current, BSTNode target) if (current == null) if (target.compareto(current) < 0) current.leftchild = removehelper((bstnode)current.leftchild, target); else if (target.compareto(current) > 0) current.rightchild = removehelper((bstnode)current.rightchild, target); else if (current.isleaf()) numitems--; BSTNode temp; if (current.leftchild == null) temp = (BSTNode)current.rightChild; current.data = temp.data; current.leftchild = temp.leftchild; if (current.leftchild!= null) current.leftchild.parent = current; current.rightchild = temp.rightchild; if (current.rightchild!= null) current.rightchild.parent = current; else if (current.rightchild == null) temp = (BSTNode)current.leftChild; current.data = temp.data; current.leftchild = temp.leftchild; if (current.leftchild!= null) current.leftchild.parent = current; current.rightchild = temp.rightchild; if (current.rightchild!= null) current.rightchild.parent = current; else temp = (BSTNode)current.rightChild; if (temp.isleaf()) current.data = temp.data; current.rightchild = null; else if (temp.leftchild == null) current.data = temp.data; current.rightchild = temp.rightchild; if (current.rightchild!= null) current.rightchild.parent = current; else while (temp.leftchild.leftchild!= null) temp = (BSTNode)temp.leftChild; current.data = temp.leftchild.data; removehelper((bstnode)temp, new BSTNode((Comparable)temp.leftChild.data)); numitems++; numitems--; return current;

154 Chapter 14 The Binary Search Tree public void remove(comparable target) removehelper((bstnode)root, new BSTNode(target)); /* The inorder successor of node n is: */ 1. the minimum node of n's right subtree, provided that n has a right subtree. 2. the first ancestor of n such that n is in the ances tor's left subtree. public Comparable successor(comparable target) BSTNode found = findhelper((bstnode)root, new BSTNode(target)); if (found == null) if (found.rightchild!= null) return (Comparable)minHelper((BSTNode)found.rightChild).data; BSTNode parent = (BSTNode)found.parent; while (parent!= null && parent.compareto(found) <= 0) parent = (BSTNode)parent.parent; if (parent == null) return (Comparable)parent.data; 14.5 Time Analysis of the Fundamental BST Operations A tree is said to be balanced if it has the least possible height. The height of a balanced binary tree with n nodes is approximately lg(n). Since a BST insertion, search, or removal may require navigation to the deepest node of the tree, the BST insert, find, and remove operations are all in O(lg(n)) for balanced, or nearly balanced, BSTs. In the worst case, however, operations insert, find, and remove are in O(n), for in the worst case a BST is simply a list. Consider the BST shown below, which results from insertion of 3, 5, 6, 18, and 27, in that order.

Exercises 155 This sorry state of affairs can be avoided by rebalancing the BST should it become unbalanced through insertions and deletions. Each of two balanced BST specializations addresses rebalancing in its own way. These specializations are the AVL tree and the red-black tree. The splay tree is a kind of BST that guarantees efficient amortized, i.e., long-run, performance, rather than guaranteeing the efficiency of any single operation. 14.6 BST Applications The most natural application of the BST is as a dictionary. Two end-ofchapter exercises deal with this application.

156 Chapter 14 The Binary Search Tree Exercises 1. Add attributes and operations to the UML class diagram of Figure 14.1 2. Design and code a class called DirectoryEntry, an instance of which represents an entry in the white pages of a telephone directory. Code a console application that allows its user to query and alter a telephone directory that takes the form of a BST. 3. When a Java compiler encounters a non-symbol token in a Java source file, the compiler must identify the token as a Java reserved word or as a user-defined identifier. Code a console application that reads a comment-free Java source file and lists alphabetically all of the file's identifiers. Your application should prompt the user for the path to the desired Java source file. Store Java's reserved words in one BST and the program's identifiers in another. Use an instance of java.util.stringtokenizer to tokenize each line of the input file. You should shuffle the reserved words before inserting them into their BST. The code below performs a Fisher-Yates shuffle on a vector. int i, j; Random rand = new Random(); for (i = vec.size() - 1; i > 0; i--) j = rand.nextint(i); Sort.swap(vec, i, j);