# Lecture Notes on Binary Search Trees

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Lecture Notes on Binary Search Trees : Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, Introduction In this lecture, we will continue considering associative arrays as we did in the hash table lecture. This time, we will follow a different implementation principle, however. With binary search trees we try to obtain efficient insert and search times for associative arrays dictionaries, which we have previously implemented as hash tables. We will eventually be able to achieve O(log(n)) worst-case asymptotic complexity for insert and search. This also extends to delete, although we won t discuss that operation in lecture. In particular, the worst-case complexity of associative array operations implemented as binary search trees is better than the worst-case complexity when implemented as hash tables. 2 Ordered Associative Arrays Hashtables are associative arrays that organize the data in an array at an index that is determined from the key using a hash function. If the hash function is good, this means that the element will be placed at a reasonably random position spread out across the whole array. If it is bad, linear search is needed to locate the element. There are many alternative ways of implementing associative arrays. For example, we could have stored the elements in an array, sorted by key. Then lookup by binary search would have been O(log(n)), but insertion would be O(n), because it takes O(log n) steps to find the right place, but

3 Binary Search Trees L17.3 but we will ignore this for simplicity here. In Lecture 6, we also did consider int as the data type. Now we study data of an arbitrary type elem provided by the client. In particular, as one step of abstraction, we will now actually compare elements in terms of their keys. Unfortunately, inserting into arrays remains an O(n) operation. For other data structures, insertion is easy. For example, insertion into a doubly linked list at a given list node is O(1). But if we use a sorted doubly linked list, the insertion step will be easy, but finding the right position by binary search is challenging, because we can only advance one step to the left or right in a doubly linked list. That would throw us back into linear search through the list to find the element, which gives a lookup complexity of O(n). How can we combine the advantages of both: fast navigation by several elements as in arrays, together with fast insertion as in doubly linked lists? Before you read on, try to see if you can find an answer yourself.

4 Binary Search Trees L17.4 In order to obtain the advantages of both, and, thus, enable binary search on a data structure that supports fast insertion, we proceed as follows. The crucial observation is that arrays provide fast access to any arbitrary index in the array, which is why they are called a random access data structure, but binary search only needs very selective access from each element. Whatever element binary search is looking at, it only needs access to that element and one (sufficiently far away) left element and one (sufficiently far away) right element. If binary search has just looked at index mid, then it will subsequently only look at either (lower + mid) / 2 or (mid+1 + upper) / 2. In particular, for each element, we need to remember what its key is, what its left successor is and what its right successor is, but nothing else. We use this insight to generalize the principle behind binary search to a more general data structure. 4 Binary Search in Binary Search Trees The data structure we have developed so far results in a (binary) tree. A binary tree consists of a set of nodes and, for each node, its left and its right child. Finding an element in a binary search tree follows exactly the same idea that binary search did, just on a more abstract data structure: 1. Compare the current node to what we are looking for. Stop if equal. 2. If what we are looking for is smaller, proceed to the left successor. 3. If what we are looking for is bigger, proceed to the right successor. What do we need to know about the binary tree to make sure that this principle will always lookup elements correctly? What data structure invariant do we need to maintain for the binary search tree? Do you have a suggestion?

5 Binary Search Trees L The Ordering Invariant Binary search was only correct for arrays if the array was sorted. Only then do we know that it is okay not to look at the upper half of the array if the element we are looking for is smaller than the middle element, because, in a sorted array, it can then only occur in the lower half, if at all. For binary search to work correctly on binary search trees, we, thus, need to maintain a corresponding data structure invariant. All elements to the right of a node have keys that are bigger than the key of that node. And all the nodes to the left of that node have smaller keys than the key at that node. At the core of binary search trees is the ordering invariant. Ordering Invariant. At any node with key k in a binary search tree, all keys of the elements in the left subtree are strictly less than k, while all keys of the elements in the right subtree are strictly greater than k. This implies that no key occurs more than once in a tree, and we have to make sure our insertion function maintains this invariant. If our binary search tree were perfectly balanced, that is, had the same number of nodes on the left as on the right for every subtree, then the ordering invariant would ensure that search for an element with a given key has asymptotic complexity O(log(n)), where n is the number of elements in the tree. Why? When searching for a given key k in the tree, we just compare k with the key k of the entry at the root. If they are equal, we have found the entry. If k < k we recursively search in the left subtree, and if k < k we recursively search in the right subtree. This is just like binary search, except that instead of an array we traverse a tree data structure. Unlike in an array, however, we will see that insertion is quick as well. 6 The Interface The basic interface for binary search trees is almost the same as for hash tables, because both implement the same abstract principle: associative arrays. Binary search trees, of course, do not need a hash function. We assume that the client defines a type elem of elements and a type key of keys, as well as functions to extract keys from elements and to compare keys. Then the implementation of binary search trees will provide a type bst and functions to insert an element and to search for an element with a given key.

6 Binary Search Trees L17.6 /* Client-side interface */ typedef * elem; typedef key; key elem_key(elem e) e!= NULL; ; int key_compare(key k1, key k2) -1 <= \result && \result <= 1; ; /* Library interface */ typedef bst; bst bst_new(); void bst_insert(bst B, elem e) e!= NULL; ; elem bst_lookup(bst B, key k); /* return NULL if not in tree */ We stipulate that elem is some form of pointer type so we can return NULL if no element with the given key can be found. Generally, some more operations may be requested at the interface, such as the number of elements in the tree or a function to delete an element with a given key. The key_compare function provided by the client is different from the equality function we used for hash tables. For binary search trees, we actually need to compare keys k 1 and k 2 and determine if k 1 < k 2, k 1 = k 2, or k 1 > k 2. A standard approach to this in imperative languages is for a comparison function to return an integer r, where r < 0 means k 1 < k 2, r = 0 means k 1 = k 2, and r > 0 means k 1 > k 2. Our contract captures that we expect key_compare to return no values other than -1, 0, and 1. 7 A Representation with Pointers We will use a pointer-based implementation for trees where every node has two pointers: one to its left child and one to its right child. A missing child is represented as NULL, so a leaf just has two null pointers.

7 Binary Search Trees L17.7 struct tree_node { elem data; struct tree_node* left; struct tree_node* right; ; typedef struct tree_node tree; As usual, we have a header which in this case just consists of a pointer to the root of the tree. We often keep other information associated with the data structure in these headers, such as the size. struct bst_header { tree* root; ; 8 Searching for a Key In this lecture, we will implement insertion and lookup first before considering the data structure invariant. This is not the usual way we proceed, but it turns out finding a good function to test the invariant is a significant challenge meanwhile we would like to exercise programming with pointers in a tree a little. For now, we just assume we have two functions bool is_ordtree(tree* T); bool is_bst(bst B); Search is quite straightforward, implementing the informal description above. Recall that key_compare(k1,k2) returns 1 if k 1 < k 2, 0 if k 1 = k 2, and 1 if k 1 > k 2. elem tree_lookup(tree* T, key k) is_ordtree(t); \result == NULL key_compare(elem_key(\result), k) == 0; { if (T == NULL) return NULL; int r = key_compare(k, elem_key(t->data)); if (r == 0) return T->data; else if (r < 0) return tree_lookup(t->left, k); else r > 0; return tree_lookup(t->right, k);

8 Binary Search Trees L17.8 elem bst_lookup(bst B, key k) is_bst(b); \result == NULL compare(elem_key(\result), k) == 0; { return tree_lookup(b->root, k); We chose here a recursive implementation, following the structure of a tree, but in practice an iterative version may also be a reasonable alternative (see Exercise 1). We can check the invariant: if T is ordered when tree_lookup(t) is called (and presumably is_bst would guarantee that), then both subtrees should be ordered as well and the invariant is preserved. 9 Inserting an Element Inserting an element is almost as simple. We just proceed as if we are looking for the key of the given element. If we find a node with that key, we just overwrite its data field. If not, we insert it in the place where it would have been, had it been there in the first place. This last clause, however, creates a small difficulty. When we hit a null pointer (which indicates the key was not already in the tree), we cannot just modify NULL. Instead, we return the new tree so that the parent can modify itself. tree* tree_insert(tree* T, elem e) is_ordtree(t); e!= NULL; is_ordtree(\result); { if (T == NULL) { /* create new node and return it */ T = alloc(struct tree_node); T->data = e; T->left = NULL; T->right = NULL; return T; int r = key_compare(elem_key(e), elem_key(t->data)); if (r == 0)

9 Binary Search Trees L17.9 T->data = e; /* modify in place */ else if (r < 0) T->left = tree_insert(t->left, e); else r > 0; T->right = tree_insert(t->right, e); return T; For the same reason as in tree_lookup, we expect the subtrees to be ordered when we make recursive calls. The result should be ordered for analogous reasons. The returned subtree will also be useful at the root. void bst_insert(bst B, elem e) is_bst(b); e!= NULL; is_bst(b); { B->root = tree_insert(b->root, e); return;

10 Binary Search Trees L Checking the Ordering Invariant When we analyze the structure of the recursive functions implementing search and insert, we are tempted to say that a binary search is ordered if either it is null, or the left and right subtrees have a key that is smaller. This would yield the following code: /* THIS CODE IS BUGGY */ bool is_ordtree(tree* T) { if (T == NULL) return true; /* an empty tree is a BST */ key k = elem_key(t->data); return (T->left == NULL (key_compare(elem_key(t->left->data), k) < 0 && is_ordtree(t->left))) && (T->right == NULL (key_compare(k, elem_key(t->right->data)) < 0 && is_ordtree(t->right))); While this should always be true for a binary search tree, it is far weaker than the ordering invariant stated at the beginning of lecture. Before reading on, you should check your understanding of that invariant to exhibit a tree that would satisfy the above, but violate the ordering invariant.

11 Binary Search Trees L17.11 There is actually more than one problem with this. The most glaring one is that following tree would pass this test: Even though, locally, the key of the left node is always smaller and on the right is always bigger, the node with key 9 is in the wrong place and we would not find it with our search algorithm since we would look in the right subtree of the root. An alternative way of thinking about the invariant is as follows. Assume we are at a node with key k. 1. If we go to the left subtree, we establish an upper bound on the keys in the subtree: they must all be smaller than k. 2. If we go to the right subtree, we establish a lower bound on the keys in the subtree: they must all be larger than k. The general idea then is to traverse the tree recursively, and pass down an interval with lower and upper bounds for all the keys in the tree. The following diagram illustrates this idea. We start at the root with an unrestricted interval, allowing any key, which is written as (, + ). As usual in mathematics we write intervals as (x, z) = {y x < y and y < z. At the leaves we write the interval for the subtree. For example, if there were a left subtree of the node with key 7, all of its keys would have to be in the

12 Binary Search Trees L17.12 interval (5, 7). (, + ) 9 (, 9) 5 (, 5) (5, 9) (9, + ) 7 (5, 7) (7, 9) The only difficulty in implementing this idea is the unbounded intervals, written above as and +. Here is one possibility: we pass not just the key, but the particular element which bounds the tree from which we can extract the element. This allows us to pass NULL in case there is no lower or upper bound. bool is_ordered(tree* T, elem lower, elem upper) { if (T == NULL) return true; return T->data!= NULL && (lower == NULL elem_compare(lower, T->data) < 0) && (upper == NULL elem_compare(t->data, upper) < 0) && is_ordered(t->left, lower, T->data) && is_ordered(t->right, T->data, upper); bool is_bst(bst B) { return B!= NULL && is_ordered(b->root, NULL, NULL); A word of caution: the is_ordered(t, NULL, NULL) pre- and postcondition of the function tree_insert is actually not strong enough to prove the correctness of the recursive function. A similar remark applies to tree_lookup. This is because of the missing information of the bounds. We will return to this issue in a later lecture.

13 Binary Search Trees L The Shape of Binary Search Trees We have already mentioned that balanced binary search trees have good properties, such as logarithmic time for insertion and search. The question is if binary search trees will be balanced. This depends on the order of insertion. Consider the insertion of numbers 1, 2, 3, and 4. If we insert them in increasing order we obtain the following trees in sequence Similarly, if we insert them in decreasing order we get a straight line along, always going to the left. If we instead insert in the order 3, 1, 4, 2, we obtain the following sequence of binary search trees: Clearly, the last tree is much more balanced. In the extreme, if we insert elements with their keys in order, or reverse order, the tree will be linear, and search time will be O(n) for n items. These observations mean that it is extremely important to pay attention to the balance of the tree. We will discuss ways to keep binary search trees balanced in a later lecture.

14 Binary Search Trees L17.14 Exercises Exercise 1 Rewrite tree_lookup to be iterative rather than recursive. Exercise 2 Rewrite tree_insert to be iterative rather than recursive. [Hint: The difficulty will be to update the pointers in the parents when we replace a node that is null. For that purpose we can keep a trailing pointer which should be the parent of the note currently under consideration.] Exercise 3 The binary search tree interface only expected a single function for key comparison to be provided by the client: int elem_compare(elem k1, elem k2); An alternative design would have been to, instead, expect the client to provide a set of elem comparison functions, one for each outcome: bool elem_equal(elem k1, elem k2); bool elem_greater(elem k1, elem k2); bool elem_less(elem k1, elem k2); What are the advantages and disadvantages of such a design?

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

### Lecture Notes on Linear Search

Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is

### Symbol Tables. IE 496 Lecture 13

Symbol Tables IE 496 Lecture 13 Reading for This Lecture Horowitz and Sahni, Chapter 2 Symbol Tables and Dictionaries A symbol table is a data structure for storing a list of items, each with a key and

### Lecture Notes on Spanning Trees

Lecture Notes on Spanning Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 26 April 26, 2011 1 Introduction In this lecture we introduce graphs. Graphs provide a uniform model

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

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

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

### CSCI Trees. Mark Redekopp David Kempe

1 CSCI 104 2-3 Trees Mark Redekopp David Kempe 2 Properties, Insertion and Removal BINARY SEARCH TREES 3 Binary Search Tree Binary search tree = binary tree where all nodes meet the property that: All

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

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

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

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

### Sequential Data Structures

Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year

### Announcements. CSE332: Data Abstractions. Lecture 9: B Trees. Today. Our goal. M-ary Search Tree. M-ary Search Tree. Ruth Anderson Winter 2011

Announcements CSE2: Data Abstractions Project 2 posted! Partner selection due by 11pm Tues 1/25 at the latest. Homework due Friday Jan 28 st at the BEGINNING of lecture Lecture 9: B Trees Ruth Anderson

### Binary search algorithm

Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than

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

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

### recursion, O(n), linked lists 6/14

recursion, O(n), linked lists 6/14 recursion reducing the amount of data to process and processing a smaller amount of data example: process one item in a list, recursively process the rest of the list

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

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

### - Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Skip Lists CMSC 420 Linked Lists Benefits & Drawbacks Benefits: - Easy to insert & delete in O(1) time - Don t need to estimate total memory needed Drawbacks: - Hard to search in less than O(n) time (binary

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

### Lecture Notes on Stacks & Queues

Lecture Notes on Stacks & Queues 15-122: Principles of Imperative Computation Frank Pfenning, André Platzer, Rob Simmons Lecture 9 February 12, 2013 1 Introduction In this lecture we introduce queues and

### Lecture 12 Doubly Linked Lists (with Recursion)

Lecture 12 Doubly Linked Lists (with Recursion) In this lecture Introduction to Doubly linked lists What is recursion? Designing a node of a DLL Recursion and Linked Lists o Finding a node in a LL (recursively)

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

### Algorithms and recursion

Read: Chapter 13 from textbook Algorithms and recursion What programs implement is recipes for solving problems. These recipes are called algorithms. We have already seen (in the practice problems from

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

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

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

### Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know

### Lecture 6. Randomized data structures Random number generation Skip lists: ideas and implementation Skip list time costs. Reading:

Lecture 6 Randomized data structures Random number generation Skip lists: ideas and implementation Skip list time costs Reading: Skip Lists: A Probabilistic Alternative to Balanced Trees (author William

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

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

The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

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

### Algorithms. Theresa Migler-VonDollen CMPS 5P

Algorithms Theresa Migler-VonDollen CMPS 5P 1 / 32 Algorithms Write a Python function that accepts a list of numbers and a number, x. If x is in the list, the function returns the position in the list

### COT5405 Analysis of Algorithms Homework 3 Solutions

COT0 Analysis of Algorithms Homework 3 Solutions. Prove or give a counter example: (a) In the textbook, we have two routines for graph traversal - DFS(G) and BFS(G,s) - where G is a graph and s is any

### Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed

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

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

Chapter 4: Linked Lists Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during

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

### Discrete Mathematics, Chapter 3: Algorithms

Discrete Mathematics, Chapter 3: Algorithms Richard Mayr University of Edinburgh, UK Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 1 / 28 Outline 1 Properties of Algorithms

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

### 10CS35: Data Structures Using C

CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a

### Problem. Indexing with B-trees. Indexing. Primary Key Indexing. B-trees. B-trees: Example. primary key indexing

Problem Given a large collection of records, Indexing with B-trees find similar/interesting things, i.e., allow fast, approximate queries Anastassia Ailamaki http://www.cs.cmu.edu/~natassa 2 Indexing Primary

### Multi-Way Search Trees (B Trees)

Multi-Way Search Trees (B Trees) Multiway Search Trees An m-way search tree is a tree in which, for some integer m called the order of the tree, each node has at most m children. If n

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

### Quiz 1 Solutions. (a) T F The height of any binary search tree with n nodes is O(log n). Explain:

Introduction to Algorithms March 9, 2011 Massachusetts Institute of Technology 6.006 Spring 2011 Professors Erik Demaine, Piotr Indyk, and Manolis Kellis Quiz 1 Solutions Problem 1. Quiz 1 Solutions True

### Analysis of a Search Algorithm

CSE 326 Lecture 4: Lists and Stacks 1. Agfgd 2. Dgsdsfd 3. Hdffdsf 4. Sdfgsfdg 5. Tefsdgass We will review: Analysis: Searching a sorted array (from last time) List ADT: Insert, Delete, Find, First, Kth,

### Big Data and Scripting. Part 4: Memory Hierarchies

1, Big Data and Scripting Part 4: Memory Hierarchies 2, Model and Definitions memory size: M machine words total storage (on disk) of N elements (N is very large) disk size unlimited (for our considerations)

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

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

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

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

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

### Zabin Visram Room CS115 CS126 Searching. Binary Search

Zabin Visram Room CS115 CS126 Searching Binary Search Binary Search Sequential search is not efficient for large lists as it searches half the list, on average Another search algorithm Binary search Very

### Chapter 14 The Binary Search Tree

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

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

### Review of Hashing: Integer Keys

CSE 326 Lecture 13: Much ado about Hashing Today s munchies to munch on: Review of Hashing Collision Resolution by: Separate Chaining Open Addressing \$ Linear/Quadratic Probing \$ Double Hashing Rehashing

### Divide-and-Conquer Algorithms Part One

Divide-and-Conquer Algorithms Part One Announcements Problem Set One completely due right now. Solutions distributed at the end of lecture. Programming section today in Gates B08 from from 3:45PM 5:00PM.

### Laboratory Module 8 B Trees

Purpose: understand the notion of B trees to build, in C, a B tree 1 2-3 Trees 1.1 General Presentation Laboratory Module 8 B Trees When working with large sets of data, it is often not possible or desirable

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

### Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Hash Tables Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited We ve considered several data structures that allow us to store and search for data

### Software Development (cs2500)

Software Development (cs2500) Lecture 53: Tail Recursion and Search M.R.C. van Dongen March 4, 2011 Contents 1 Outline 1 2 Tail Recursion 2 3 Search 4 4 Linear Search 5 5 Binary Search 6 6 Analysis 7 7

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

### Computer Science 210: Data Structures. Searching

Computer Science 210: Data Structures Searching Searching Given a sequence of elements, and a target element, find whether the target occurs in the sequence Variations: find first occurence; find all occurences

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

### Balanced Trees Part One

Balanced Trees Part One Balanced Trees Balanced trees are surprisingly versatile data structures. Many programming languages ship with a balanced tree library. C++: std::map / std::set Java: TreeMap /

### INDEX ABOUT HASHING IN GENERAL

INDEX ABOUT HASHING IN GENERAL 1. URL... 2 2. INTRODUCTION.. 2 3. HASH TABLE STRUCTURE... 3 3.1 OPERATIONS 4 3.1.1 CREATE A TABLE 4 3.1.2 DELETE A TABLE 4 3.1.3 STRING LOOKUP.. 5 3.1.4 INSERT A STRING

### Trees and structural induction

Trees and structural induction Margaret M. Fleck 25 October 2010 These notes cover trees, tree induction, and structural induction. (Sections 10.1, 4.3 of Rosen.) 1 Why trees? Computer scientists are obsessed

### Node-Based Structures Linked Lists: Implementation

Linked Lists: Implementation CS 311 Data Structures and Algorithms Lecture Slides Monday, March 30, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org

### COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Binary numbers The reason humans represent numbers using decimal (the ten digits from 0,1,... 9) is that we have ten fingers. There is no other reason than that. There is nothing special otherwise about

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

### 2 Exercises and Solutions

2 Exercises and Solutions Most of the exercises below have solutions but you should try first to solve them. Each subsection with solutions is after the corresponding subsection with exercises. 2.1 Sorting

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

### Chapter 13. Pointers and Linked Lists

Chapter 13 Pointers and Linked Lists Overview 13.1 Nodes and Linked Lists 13.2 Stacks and Queues Slide 13-2 13.1 Nodes and Linked Lists Nodes and Linked Lists n A linked list is a list that can grow and

### Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010 Problem 1. In each of the following question, please specify if the statement

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

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

### Lecture 2 February 12, 2003

6.897: Advanced Data Structures Spring 003 Prof. Erik Demaine Lecture February, 003 Scribe: Jeff Lindy Overview In the last lecture we considered the successor problem for a bounded universe of size u.

### Persistent Data Structures

6.854 Advanced Algorithms Lecture 2: September 9, 2005 Scribes: Sommer Gentry, Eddie Kohler Lecturer: David Karger Persistent Data Structures 2.1 Introduction and motivation So far, we ve seen only ephemeral

### CMPS 102 Solutions to Homework 1

CMPS 0 Solutions to Homework Lindsay Brown, lbrown@soe.ucsc.edu September 9, 005 Problem..- p. 3 For inputs of size n insertion sort runs in 8n steps, while merge sort runs in 64n lg n steps. For which

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

### Lecture 3: Finding integer solutions to systems of linear equations

Lecture 3: Finding integer solutions to systems of linear equations Algorithmic Number Theory (Fall 2014) Rutgers University Swastik Kopparty Scribe: Abhishek Bhrushundi 1 Overview The goal of this lecture

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

### Stacks. Linear data structures

Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations

### Searching & Sorting. Searching & Sorting 1

Searching & Sorting Searching & Sorting 1 The Plan Searching Sorting Java Context Searching & Sorting 2 Search (Retrieval) Looking it up One of most fundamental operations Without computer Indexes Tables

### Lecture 1: Course overview, circuits, and formulas

Lecture 1: Course overview, circuits, and formulas Topics in Complexity Theory and Pseudorandomness (Spring 2013) Rutgers University Swastik Kopparty Scribes: John Kim, Ben Lund 1 Course Information Swastik

### Data Structures and Algorithms Written Examination

Data Structures and Algorithms Written Examination 22 February 2013 FIRST NAME STUDENT NUMBER LAST NAME SIGNATURE Instructions for students: Write First Name, Last Name, Student Number and Signature where

### Loop Invariants and Binary Search

Loop Invariants and Binary Search Chapter 4.3.3 and 9.3.1-1 - Outline Ø Iterative Algorithms, Assertions and Proofs of Correctness Ø Binary Search: A Case Study - 2 - Outline Ø Iterative Algorithms, Assertions

### Searching and Sorting Algorithms

Searching and Sorting Algorithms CS117, Fall 2004 Supplementary Lecture Notes Written by Amy Csizmar Dalal 1 Introduction How do you find someone s phone number in the phone book? How do you find your

### Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate

### Algorithms. Margaret M. Fleck. 18 October 2010

Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o

### CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science

CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science Semester and Year: Spring 2009 Classroom Section, Meeting Times,

### Fast Sequential Summation Algorithms Using Augmented Data Structures

Fast Sequential Summation Algorithms Using Augmented Data Structures Vadim Stadnik vadim.stadnik@gmail.com Abstract This paper provides an introduction to the design of augmented data structures that offer

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

### Chapter 8: Bags and Sets

Chapter 8: Bags and Sets In the stack and the queue abstractions, the order that elements are placed into the container is important, because the order elements are removed is related to the order in which