# Chapter 14 The Binary Search Tree

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 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 Our Current Model Our current software design appears below in Figure 14.1.

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

3 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 BST Operations Now we will have a look at the insert, find, and remove operations of the BST.

4 146 Chapter 14 The Binary Search Tree 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.

5 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.

6 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 The BST that results from the insertion of 5, 3, 4, 8, 1, 6, and 4, in that order 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 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.

7 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 Delete elements 1, 4, 8, and 5, restoring the tree to its original form after each deletion 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.

8 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;

9 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;

10 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;

11 153 // Method removehelper() implements the algorithm specified // in 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;

12 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.

13 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 BST Applications The most natural application of the BST is as a dictionary. Two end-ofchapter exercises deal with this application.

14 156 Chapter 14 The Binary Search Tree Exercises 1. Add attributes and operations to the UML class diagram of Figure 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);

### Binary Search Trees (BST)

Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to node 2. Each node has at most two child nodes (a left and a right child) 3. Nodes are organized by the Binary Search

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

Learning Outcomes COMP202 Complexity of Algorithms Binary Search Trees and Other Search Trees [See relevant sections in chapters 2 and 3 in Goodrich and Tamassia.] At the conclusion of this set of lecture

### Analysis of Algorithms I: Binary Search Trees

Analysis of Algorithms I: Binary Search Trees Xi Chen Columbia University Hash table: A data structure that maintains a subset of keys from a universe set U = {0, 1,..., p 1} and supports all three dictionary

### Ordered Lists and Binary Trees

Data Structures and Algorithms Ordered Lists and Binary Trees Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/62 6-0:

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

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

### Data Structures and Algorithms

Data Structures and Algorithms CS245-2016S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

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

Chapter 12: Binary Search Trees A binary search tree is a binary tree with a special property called the BST-property, which is given as follows: For all nodes x and y, if y belongs to the left subtree

### Algorithms Chapter 12 Binary Search Trees

Algorithms Chapter 1 Binary Search Trees Outline Assistant Professor: Ching Chi Lin 林 清 池 助 理 教 授 chingchi.lin@gmail.com Department of Computer Science and Engineering National Taiwan Ocean University

### TREE BASIC TERMINOLOGIES

TREE Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing hierarchical relationship between the grand father and his children and

### 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

Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent

### CS 253: Algorithms. Chapter 12. Binary Search Trees. * Deletion and Problems. Credit: Dr. George Bebis

CS 2: Algorithms Chapter Binary Search Trees * Deletion and Problems Credit: Dr. George Bebis Binary Search Trees Tree representation: A linked data structure in which each node is an object Node representation:

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

Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest

### 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 Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

### Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

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

Lecture 6: Binary Search Trees CSCI 700 - Algorithms I Andrew Rosenberg Last Time Linear Time Sorting Counting Sort Radix Sort Bucket Sort Today Binary Search Trees Data Structures Data structure is a

### Binary Trees and Huffman Encoding Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees Computer Science E119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Motivation: Maintaining a Sorted Collection of Data A data dictionary

### Binary Search Trees > = Binary Search Trees 1. 2004 Goodrich, Tamassia

Binary Search Trees < 6 2 > = 1 4 8 9 Binary Search Trees 1 Binary Search Trees A binary search tree is a binary tree storing keyvalue entries at its internal nodes and satisfying the following property:

### Algorithms and Data Structures Written Exam Proposed SOLUTION

Algorithms and Data Structures Written Exam Proposed SOLUTION 2005-01-07 from 09:00 to 13:00 Allowed tools: A standard calculator. Grading criteria: You can get at most 30 points. For an E, 15 points are

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

(1) Binary Search Tree ADT 56 26 200 18 28 190 213 12 24 27 (2) Binary Search Tree ADT (BST) It is a binary tree with the following properties 1. with each node associate a key 2. the key of each node

### The ADT Binary Search Tree

The ADT Binary Search Tree The Binary Search Tree is a particular type of binary tree that enables easy searching for specific items. Definition The ADT Binary Search Tree is a binary tree which has an

### Binary Search Trees CMPSC 122

Binary Search Trees CMPSC 122 Note: This notes packet has significant overlap with the first set of trees notes I do in CMPSC 360, but goes into much greater depth on turning BSTs into pseudocode than

### Balanced Binary Search Tree

AVL Trees / Slide 1 Balanced Binary Search Tree Worst case height of binary search tree: N-1 Insertion, deletion can be O(N) in the worst case We want a tree with small height Height of a binary tree with

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

Binary search tree Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later). Data strutcure fields usually include for a given node x, the following

### Binary Heaps. CSE 373 Data Structures

Binary Heaps CSE Data Structures Readings Chapter Section. Binary Heaps BST implementation of a Priority Queue Worst case (degenerate tree) FindMin, DeleteMin and Insert (k) are all O(n) Best case (completely

### 1.204 Lecture 6. Data structures: stacks, queues, trees, dictionaries. Data structures

1.204 Lecture 6 Data structures: stacks, queues, trees, dictionaries Data structures Correct and efficient representation of data and applicable rules Stack: last in, first out discipline Queue: first

### root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain

inary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each

### Binary Heap Algorithms

CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

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

B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

### 1 2-3 Trees: The Basics

CS10: Data Structures and Object-Oriented Design (Fall 2013) November 1, 2013: 2-3 Trees: Inserting and Deleting Scribes: CS 10 Teaching Team Lecture Summary In this class, we investigated 2-3 Trees in

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

Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

### Output: 12 18 30 72 90 87. struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

50 20 70 10 30 69 90 14 35 68 85 98 16 22 60 34 (c) Execute the algorithm shown below using the tree shown above. Show the exact output produced by the algorithm. Assume that the initial call is: prob3(root)

### 7.1 Our Current Model

Chapter 7 The Stack In this chapter we examine what is arguably the most important abstract data type in computer science, the stack. We will see that the stack ADT and its implementation are very simple.

### CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] Trees Important terminology: edge root node parent Some uses of trees: child leaf model arithmetic expressions and other expressions

### UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK PSD2023 ALGORITHM & DATA STRUCTURE

STUDENT IDENTIFICATION NO UNIVERSITI MALAYSIA SARAWAK 94300 KOTA SAMARAHAN SARAWAK FAKULTI SAINS KOMPUTER & TEKNOLOGI MAKLUMAT (Faculty of Computer Science & Information Technology) Diploma in Multimedia

### From Last Time: Remove (Delete) Operation

CSE 32 Lecture : More on Search Trees Today s Topics: Lazy Operations Run Time Analysis of Binary Search Tree Operations Balanced Search Trees AVL Trees and Rotations Covered in Chapter of the text From

### An Evaluation of Self-adjusting Binary Search Tree Techniques

SOFTWARE PRACTICE AND EXPERIENCE, VOL. 23(4), 369 382 (APRIL 1993) An Evaluation of Self-adjusting Binary Search Tree Techniques jim bell and gopal gupta Department of Computer Science, James Cook University,

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

B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

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

Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

### Symbol Tables. Introduction

Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The

### Binary Search Trees. Ric Glassey glassey@kth.se

Binary Search Trees Ric Glassey glassey@kth.se Outline Binary Search Trees Aim: Demonstrate how a BST can maintain order and fast performance relative to its height Properties Operations Min/Max Search

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

CHAPTER 5 The Tree Data Model There are many situations in which information has a hierarchical or nested structure like that found in family trees or organization charts. The abstraction that models hierarchical

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

Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using

### S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India e-mail: nellailath@yahoo.co.in. Dr. R. M.

A Sorting based Algorithm for the Construction of Balanced Search Tree Automatically for smaller elements and with minimum of one Rotation for Greater Elements from BST S. Muthusundari Research Scholar,

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

Data Structures, Practice Homework 3, with Solutions (not to be handed in) 1. Carrano, 4th edition, Chapter 9, Exercise 1: What is the order of each of the following tasks in the worst case? (a) Computing

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

Data Structures Page 1 of 24 A.1. Arrays (Vectors) n-element vector start address + ielementsize 0 +1 +2 +3 +4... +n-1 start address continuous memory block static, if size is known at compile time dynamic,

### Tree Properties (size vs height) Balanced Binary Trees

Tree Properties (size vs height) Balanced Binary Trees Due now: WA 7 (This is the end of the grace period). Note that a grace period takes the place of a late day. Now is the last time you can turn this

### Heap. Binary Search Tree. Heaps VS BSTs. < el el. Difference between a heap and a BST:

Heaps VS BSTs Difference between a heap and a BST: Heap el Binary Search Tree el el el < el el Perfectly balanced at all times Immediate access to maximal element Easy to code Does not provide efficient

### AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm

AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm Time: Three Hours] [Maximum Marks: 60 Note Attempts all the questions. All carry equal marks Section A

### Rotation Operation for Binary Search Trees Idea:

Rotation Operation for Binary Search Trees Idea: Change a few pointers at a particular place in the tree so that one subtree becomes less deep in exchange for another one becoming deeper. A sequence of

### 12 Abstract Data Types

12 Abstract Data Types 12.1 Source: Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT).

### Binary Search Trees 3/20/14

Binary Search Trees 3/0/4 Presentation for use ith the textbook Data Structures and Algorithms in Java, th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldasser, Wiley, 04 Binary Search Trees 4

### OPTIMAL BINARY SEARCH TREES

OPTIMAL BINARY SEARCH TREES 1. PREPARATION BEFORE LAB DATA STRUCTURES An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum.

### Exam study sheet for CS2711. List of topics

Exam study sheet for CS2711 Here is the list of topics you need to know for the final exam. For each data structure listed below, make sure you can do the following: 1. Give an example of this data structure

### Data Structure [Question Bank]

Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

### Persistent Binary Search Trees

Persistent Binary Search Trees Datastructures, UvA. May 30, 2008 0440949, Andreas van Cranenburgh Abstract A persistent binary tree allows access to all previous versions of the tree. This paper presents

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

Exercises Software Development I 11 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal January 16, 2013 Software Development I Winter term 2012/2013 Institute for Pervasive Computing Johannes

### 6 March 2007 1. Array Implementation of Binary Trees

Heaps CSE 0 Winter 00 March 00 1 Array Implementation of Binary Trees Each node v is stored at index i defined as follows: If v is the root, i = 1 The left child of v is in position i The right child of

### Algorithms and Data Structures

Algorithms and Data Structures CMPSC 465 LECTURES 20-21 Priority Queues and Binary Heaps Adam Smith S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Trees Rooted Tree: collection

### Why Use Binary Trees?

Binary Search Trees Why Use Binary Trees? Searches are an important application. What other searches have we considered? brute force search (with array or linked list) O(N) binarysearch with a pre-sorted

### Chapter 8: Binary Trees

Chapter 8: Binary Trees Why Use Binary Trees? Tree Terminology An Analogy How Do Binary Search Trees Work Finding a Node Inserting a Node Traversing the Tree Finding Maximum and Minimum Values Deleting

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

=============================================================================================================================== DATA STRUCTURE PSEUDO-CODE EXAMPLES (c) Mubashir N. Mir - www.mubashirnabi.com

### Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms Chapter: Binary Search Trees Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen Search Trees Search trees can be used

### In our lecture about Binary Search Trees (BST) we have seen that the operations of searching for and inserting an element in a BST can be of the

In our lecture about Binary Search Trees (BST) we have seen that the operations of searching for and inserting an element in a BST can be of the order O(log n) (in the best case) and of the order O(n)

### Java Software Structures

INTERNATIONAL EDITION Java Software Structures Designing and Using Data Structures FOURTH EDITION John Lewis Joseph Chase This page is intentionally left blank. Java Software Structures,International Edition

### Analysis of Algorithms I: Optimal Binary Search Trees

Analysis of Algorithms I: Optimal Binary Search Trees Xi Chen Columbia University Given a set of n keys K = {k 1,..., k n } in sorted order: k 1 < k 2 < < k n we wish to build an optimal binary search

### PES Institute of Technology-BSC QUESTION BANK

PES Institute of Technology-BSC Faculty: Mrs. R.Bharathi CS35: Data Structures Using C QUESTION BANK UNIT I -BASIC CONCEPTS 1. What is an ADT? Briefly explain the categories that classify the functions

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

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

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

Announcements (4//08) CSE 26: Data Structures B-Trees and B+ Trees Brian Curless Spring 2008 Midterm on Friday Special office hour: 4:-5: Thursday in Jaech Gallery (6 th floor of CSE building) This is

### Data Structure with C

Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which

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

Binary Search Trees Last lecture: Tree terminology Kinds of binary trees Size and depth of trees This time: binary search tree ADT Java implementation Keys and records So far most examples assumed that

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

S I N S UN I ER E P S I T VER M A TA S CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A-1998-5 UNIVERSITY OF TAMPERE DEPARTMENT OF

### 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

Binary Search Trees \I think that I shall never see a poem as lovely as a tree Poem's are wrote by fools like me but only G-d can make atree \ {Joyce Kilmer Binary search trees provide a data structure

### A Comparison of Dictionary Implementations

A Comparison of Dictionary Implementations Mark P Neyer April 10, 2009 1 Introduction A common problem in computer science is the representation of a mapping between two sets. A mapping f : A B is a function

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

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE) UNIT I LINEAR STRUCTURES Abstract Data Types (ADT) List ADT array-based implementation linked list implementation cursor-based linked lists

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

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION Tao Chen 1, Tarek Sobh 2 Abstract -- In this paper, a software application that features the visualization of commonly used

### DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT By GROUP Avadhut Gurjar Mohsin Patel Shraddha Pandhe Page 1 Contents 1. Introduction... 3 2. DNS Recursive Query Mechanism:...5 2.1. Client

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

Data Structures Jaehyun Park CS 97SI Stanford University June 29, 2015 Typical Quarter at Stanford void quarter() { while(true) { // no break :( task x = GetNextTask(tasks); process(x); // new tasks may

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

Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

### DATA STRUCTURES USING C

DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative

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

. iscuss following. Graph graph G consist of a non empty set V called the set of nodes (points, vertices) of the graph, a set which is the set of edges and a mapping from the set of edges to a set of pairs

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

CmSc 250 Intro to Algorithms Chapter 6. Transform and Conquer Binary Heaps 1. Definition A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called

### EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27 The Problem Given a set of N objects, do any two intersect? Objects could be lines, rectangles, circles, polygons, or other geometric objects Simple to

### Algorithms and Data Structures

Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2

### International Journal of Software and Web Sciences (IJSWS) www.iasir.net

International Association of Scientific Innovation and Research (IASIR) (An Association Unifying the Sciences, Engineering, and Applied Research) ISSN (Print): 2279-0063 ISSN (Online): 2279-0071 International

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 March 17, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure

### Cpt S 223. School of EECS, WSU

Priority Queues (Heaps) 1 Motivation Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important or timely than others (higher priority)

### Sample Questions Csci 1112 A. Bellaachia

Sample Questions Csci 1112 A. Bellaachia Important Series : o S( N) 1 2 N N i N(1 N) / 2 i 1 o Sum of squares: N 2 N( N 1)(2N 1) N i for large N i 1 6 o Sum of exponents: N k 1 k N i for large N and k

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

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION 2008-10-23/5:15-6:45 REC-200, EVI-350, RCH-106, HH-139 Instructions: No aides. Turn off all electronic media and store them under your desk. If

### Introduction to data structures

Notes 2: Introduction to data structures 2.1 Recursion 2.1.1 Recursive functions Recursion is a central concept in computation in which the solution of a problem depends on the solution of smaller copies

### Merge Sort. 2004 Goodrich, Tamassia. Merge Sort 1

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Merge Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets

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

Binary Search Tree A binary search tree is a data structure that allows for key lookup, insertion, and deletion. It is a binary tree, meaning every node of the tree has at most two child nodes, a left

Linked Lists Linked Lists, Queues, and Stacks CSE 10: Introduction to C Programming Fall 200 Dynamic data structure Size is not fixed at compile time Each element of a linked list: holds a value points

### Data Structures Using Java

Data Structures Using Java D. S. Malik P. S. Nair THOMSON COURSE TECHNOLOGY Australia Canada Mexico Singapore Spain United Kingdom United States TABLE OF Contents PREFACE XXV 1.Software Engineering Principles

### Data Structures Fibonacci Heaps, Amortized Analysis

Chapter 4 Data Structures Fibonacci Heaps, Amortized Analysis Algorithm Theory WS 2012/13 Fabian Kuhn Fibonacci Heaps Lacy merge variant of binomial heaps: Do not merge trees as long as possible Structure:

### External Memory Geometric Data Structures

External Memory Geometric Data Structures Lars Arge Department of Computer Science University of Aarhus and Duke University Augues 24, 2005 1 Introduction Many modern applications store and process datasets