Binary Search Trees, etc.

Size: px
Start display at page:

Download "Binary Search Trees, etc."

Transcription

1 Chapter 11 Binary Search Trees, etc. The Dictionary data type is a collection of data, each of which has a key component. If we use an hashing table to represent a dictionary, it only takes, on average, Θ(1) to carry out such basic operations as search, insertion, and deletion. However, the worst time is O(n). It also can not provide good average time to carry out other operations such as output in ascending order of keys, find the k th element in ascending order, etc.. All those basic operations can be done in Θ(n), using a balanced search tree, a sorted output can be done in Θ(n), and the other operations can be done in Θ(log n). We will begin with binary search tree, which is not always balanced. 1

2 Binary Search Trees A binary search tree is a binary tree that may be empty. A nonempty binary tree has the following properties: 1) Every element has a key component, whose values are distinct. 2) The keys in the left subtree of the root are smaller than the key in the root. 3) The keys in the right subtree of the root are larger than that in the root. 4) Both the left and right subtrees are also binary search trees. An indexed BST is derived from a BST by adding a field, LeftSize, to each tree node, whose value is the number of nodes in its leftsubtree plus 1. 2

3 The BSTree class Since the number of nodes, as well as its shape, will change frequently, it is appropriate to use a linked structure to represent a binary search tree. Moreover, it will be convienient to derive it from the class of binary trees. For example, we can call the InOutput, operation defined for binary trees to output all the elements in an ascending order. class BSTree : public BinaryTree<E> { public: bool Search(const K& k, E& e) const; BSTree<E,K>& Insert(const E& e); BSTree<E,K>& Delete(const K& k, E& e); void Ascend() {InOutput();} }; 3

4 The search operation When we search for x, we begin at the root. If it is NULL, then x can t be found. Otherwise, we compare x with the key of the root. If they are equal, the search is a success. If it is larger that the key of the root, it must be in the right subtree, if it is inside the tree at all; so, we recursively search for x in the right subtree. Otherwise, we search the left one. bool BSTree<E,K>::Search (const K& k, E &e) const{ BinaryTreeNode<E> *p = root; while (p) // examine p->data if (k < p->data) p = p->leftchild; else if (k > p->data) p = p->rightchild; else { e = p->data; return true;} return false; } 4

5 Obviously, the search can be done in O(h), where h is the height of the tree. With an indexed BST, we can also search for the k th element, also in O(h). For example, to look for the third element in the following indexed BST, we check the LeftSize filed of the root, since it is 4, the element must be in the left subtree. We then check this field again for the root of its left subtree, it is a 2. hence, the element we are looking for is the smallest one of its right subtree. We find out that the LeftSize of its root is 1, i.e., so we can conclude that this node the root itself is the element we are looking for. 5

6 The insertion operation To insert an element with key k inside a BST, we first verify that this key does not occur anywhere inside the tree. If the search succeeds, the insertion will not be done; otherwise, the element will be inserted where the search reports a failure. The following are two examples: If we insert into an indexed BST, we also have to adjust the LeftSize field of all the elements from the insertion point back to the root. In both cases, it takes Θ(log n). 6

7 The code BSTree<E,K>& BSTree<E,K>::Insert(const E& e){ BinaryTreeNode<E> *p = root,*pp = 0; while (p){ pp = p; if (e < p->data) p = p->leftchild; else if (e > p->data) p = p->rightchild; else throw BadInput(); // duplicate } BinaryTreeNode<E> *r=new BinaryTreeNode<E> (e); if (root) { if (e < pp->data) pp->leftchild = r; else pp->rightchild = r;} else root = r; return *this; } 7

8 The deletion operation If the node to be deleted is a leaf, we simply delete it. It is also easy to do when it has just one child. When it has two children, it becomes a bit tricky, since we have only one location for two nodes to be hooked on. 8

9 BSTree<E,K>& BSTree<E,K>::Delete (const K& k, E& e){ BinaryTreeNode<E> *p = root, *pp = 0; while (p && p->data!= k){ pp = p; if (k < p->data) p = p->leftchild; else p = p->rightchild; } if (!p) throw BadInput(); e = p->data; if(p->leftchild && p->rightchild) { BinaryTreeNode<E> *s = p->leftchild,*ps = p; while (s->rightchild){ ps = s; s = s->rightchild;} p->data = s->data; p = s; pp = ps; } BinaryTreeNode<E> *c; if (p->leftchild) c = p->leftchild; else c = p->rightchild; if (p == root) root = c; else { if (p == pp->leftchild) pp->leftchild = c; else pp->rightchild = c;} delete p; return *this; } 9

10 BST could be high The height of a binary search tree can be quite high. For example, if we use the original insertion function to add in element 1, 2,...,n, in an empty BST, then the resulted BST is actually a linear list. As a result, a basic operation can take as must as O(n). But, if insertion and deletions are made at random, then the height of such a BST will be O(log n). 10

11 A little assignment Homework Generate a random permutation of the integers 1 through n, n [100, 500, 1000, 10000, 20000, 50000]. Insert them into an initially empty binary search tree according to the random order. Measure the height of the resulting search tree. Repeat this experiment for several random permutations and then calculate the average of the measured heights. Compare the just obtained average with the theoretical figure of 2 log 2 (n +1). 11

12 AVL trees We want to define the BST operations in such a way that the trees stay balanced all the times, while preserving the binary search tree properties. The essential algorithms for insertion and deletion will be exactly the same as for BST, except that as they might destroy the property of being balanced, we have to restore the balance of the tree, if needed. Such a tree is called a balanced tree, it is guaranteed that all the basic operations applied to a balanced tree will be done in Θ(log n). The class of AVL trees is one of the more popular balanced trees. An nonempty binary tree with T L and T R as its left and right subtrees is an AVL tree iff both T L and T R are AVL trees, and h L h R 1. 12

13 AVL search trees An AVL search tree is a binary search tree, which is also an AVL tree. It has the following properties. 1. The height of an AVL tree with n nodes is O(log n). 2. For every n 0, there exists an AVL tree with n nodes. 3. We can search an n element AVL search tree in O(log n). 4. A new element can be added into an n element AVL tree so that the resulted (n + 1) element tree is also an AVL tree. 5. An element can be deleted from an n element AVL search tree, and the resulted (n 1) element tree is also an AVL tree, and the deletion can be done in O(log n). 13

14 Fibonacci tree We calculate the least number of nodes in a balanced binary tree with depth h first: Let S(h) be this number. Obviously, S(0) = 1 and S(1) = 2. In general, we can construct a balanced tree with depth h by merging two subtrees of heights h 1 and h 2, each of which has the least number of nodes. We call such trees Fibonacci trees for the obvious reason. By an inductive argument, we have that S(h) = S(h 1) + S(h 2) + 1 fib(h), which leads to that S(h) > 3 2h 1. Thus, the least number of nodes in such a tree, n 0, satisfy that n 0 > 3 2h 1. 14

15 Height of an AVL tree Therefore, given any AVL tree with n nodes, we have that n n 0 > 3 2 h 1 Further calculation leads to the following result, h 1.44 log(n +2) A simpler, but unproved, result is that h log(n +1)

16 Represent an AVL tree An AVL tree is usually represented using the linked representation for binary trees. To provide information to restore the balance, a balance factor is added to each node. For any node, this factor is defined to be the difference between the heights of its left and right subtrees. It is easy to see that any search takes O(log n). 16

17 Restore the Balance When a tree becomes unbalanced, as a result of either insertion or deletion, we can restore its balance by rotating the tree at and/or below the node where the imbalance is detected. Case 1: Assume that the left subtree of k 2 has height larger than that of its right subtree, caused by adding a node into subtree X or deleting a node from subtree Z. We apply a right rotation, which will restore the balance of the tree rooted at k 1. Notice that the above rotation is symmetric. 17

18 An example Below shows how to restore the balance of a tree by applying a single rotation. Notice that here we just added a node with value into the left subtree rooted at 7, labeled as k 1 which itself is the left subtree of the tree rooted at 8, labeled as k 2. 18

19 Restore the Balance Case 2: Suppose that the imbalance is caused by the right subtree rooted at k 1 being too high, whose balance can t be restored by a single rotation. We must apply two successive single rotations, as follows. When it is caused by the left subtree being too high, we have the following similar double rotation. 19

20 An example Below shows how to restore the balance of a tree by applying a double rotation. Notice that here we just added a node with value 14, labeled as k 2 into the left subtree rooted at 15, labeled as k 1, which itself is the right subtree of the tree rooted at 7, labeled as k 3. 20

21 Histogramming We start with a collection of n keys and must output a list of distinct keys and their frequencies. Assume that n =10, and keys = [2, 4, 2, 2, 3, 4, 2, 6, 4, 2]. When the range of those keys is quite small, this problem can be solved, in Θ(n), by using an array h[0..r], where r is the maximum key value, and letting h[i] store the frequency of i. 21

22 When the key value are not integers, the above procedure can t be used. Instead, we can sort the n keys, in Θ(n log n), then scan the sorted list from left to right to find the frequencies of respective keys. This solution can be further improved, when m, the number of distinct keys, is quite small. We can use a (balanced) BST tree, each of whose nodes contains two fields, key and frequency. We insert all the n keys into such a tree, when a key is already there, we simply increment its frequency by 1. This procedure takes an expected complexity of Θ(n log m). When a balanced tree is used, this complexity is guaranteed, even in the worst case. 22

23 m way search trees An m way search tree, when it is not empty, satisfies the following conditions: 1) each internal node has up to m children and contains between 1 and m 1 elements. 2) Every node with p elements has exactly p + 1 children. 3) For any node with p elements. Let k 1 <,...,< k p be the keys of those elements, and let c 0,...,c p be the p + 1 children. Then, the keys in all the elements in the subtree c 0 are smaller than k 1, the keys in all the elements in the subtree c p are bigger than k p, and the keys in all the elements in subtree c i are between k i and k i+1, 1 i p 1. 23

24 It is easy to do a search in such a tree. To insert an element, we search for it first. The place where the search terminates indicates an insertion point. But, if there are no space left, we have to add an node to the tree to contain this element. It is a bit trickier to do a deletion. An m way search tree of height h may have as few as h elements, and as many as m h 1. As a result, the height of such a tree ranges between log m (n+1) and n. For example, a 200- way tree of height 5 can hold up to elements. When the search tree is located on a disk, the disk access time is in proportional to O(h), where h is the height of the tree. Thus, we have to cut the height as much as possible. 24

25 B tree A B tree of order m is an m way search tree. When it is not empty, it satisfies the following: 1) The root has at least two children. 2) All internal nodes other than the root have at least m 2 children. 3) All the external nodes are at the same level. 25

26 2-3 trees For a B tree of order 3, as it is a 3-way search tree, each of its internal node can have no more than 3 children. On the other hand, it is also required that such a node should have at least 2 children. Thus, each internal node will have either 2 or 3 children. Thus, it is also called a 2-3 tree. 26

27 Height of a B tree of order m Let N be the total number of keys. Since such a tree is an m way search tree, we immediately have that h log m (N +1). On the other hand, let d be m 2. As the root has at least two children, each of the other internal node has at least d children, we have that the least number of nodes at each level is 1, 2, 2d,...,2d h 1. Hence, the total number of nodes is at least dh 1 d 1. Further calculation shows that (d 1)(n 1) h log d ( 2 +1). We have that n N 1 d 1 +1, thus, h log d ( N +1 ). 2 27

28 Operations of B-tree Search is done pretty straightforward. Basically, to look for a node, we start with the root, following the lead given by the keys, we eventually either find it, or report a failure. When inserting an element, because of the number restriction, i.e., each node contains at most m 1 keys in an m ary B tree, we might have to split a node along the way, which may even lead to the growth of a tree. When deleting a node, there are two cases, w.r.t. the nature of the node to be deleted. 1) It is deleted from a leaf. 2) It is to be deleted from a non-leaf. In case 2) we can replace this node with either the largest element from its left neighboring subtree, or the smallest from its right subtree, then to delete the substituting element. Hence, case 2 can always be converted to case 1. 28

29 In case 1, if the element is to be deleted from a node that has at least one more than the minimum required number, i.e., m 2 1 for an m ary tree, we simply delete it and write back the modified node. Otherwise, we will try to replace it with an element from either its left or right sibling. When both siblings don t have that extra element, we have to merge those two nodes with the element between them in the parent node. This might have a chain reaction, ending up reducing the height of the whole tree. 29

30 The ISAM method AVL trees and other balanced trees will provide good performance when the dictionary is small enough to fit into the internal memory. When we have to deal with larger quantity of data, we can get improved performance by using search trees of higher degree. Let s look at ISAM (indexed sequential access method,) which is a popular access method for external memory, first. In ISAM, the available disk space will be cut into blocks, which is the smallest unit of disk space that will be input or output, at the cost of a single seek operation. The dictionary elements are packed into blocks in ascending order. 30

31 For sequential access, the blocks are input in order, and the elements will be retrieved in ascending order. If each block contains m elements, then the number of disk access time is m 1. On the other hand, to support random access, an index is maintained, which keeps the largest key in each block. Since, the index holds as many keys as the number of blocks, and since a block usually holds many elements, this index can usually be kept in the internal memory. To search for a record, the index is searched first for the block that contains this record. Then, that block is retrieved, and searched internally for that record. Thus, a single data access is enough. 31

32 This technique can be generalized, when data have to be packed into several disks, whence each disk will keep an index, and there will be an overall index kept in the internal memory. When search for something in this case, the overall index will be searched first to determine the single disk that contains the record. Then, the block index for that disk will be retrieved in a disk access, and finally, the relevant block will be brought in to do an internal search. So, in total, just two disk accesses are needed. In the next slide, we will see a more specific example, using B tree to maintain those indexes. 32

33 An Example Assume that we have a 128MB hard disk divided into 2 16 pages of 2048 bytes each. We use 4 bytes to represent the addresses of those pages. We also assume that the whole disk is used to store just one file of N records, each of which needs 512 bytes. Thus, each page can store 4 such records. Further assume that the key for each record is 4 bytes long. Thus, the key and the disk address of each record will need 8 bytes, so that each page can hold 256 of them. If we keep these index information separate from the records, we need 256 N pages to store them. We also need N 4 pages to keep the records. Also, the total number of pages must be less than This leads to the fact that N 258, 111. Assume that the equality holds. 33

34 We will use a B-tree to hold the indices. As each page can hold 256 indices, we decide to use a tree of order 256. The formula tells us that the height of such a tree will be less than 3, 2.43 to be exact. Therefore, to get access to any record in the disk, it takes at most four accesses to find the index and one more to get the record itself for a total of 5 disk accesses. Can we store the whole B-tree in RAM with 4 MB, thus, only one disk access is needed? Each node of the tree must store 255 indices of 8 bytes each and 256 pointers of 4 bytes each, thus a single node costs 3064 bytes, which leads to the fact that at most 1368 nodes will fit the RAM. However, as the root may contain just 1 index and each of the other node may contain just 128 indexes, thus the total indices contained by those nodes might be just 174,977, which is less than the total number of records. Thus, the whole tree cannot fit into the RAM. 34

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

More information

TREE BASIC TERMINOLOGIES

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

More information

Analysis of Algorithms I: Binary Search Trees

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

More information

From Last Time: Remove (Delete) Operation

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

More information

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

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

More information

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

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

More information

Full and Complete Binary Trees

Full and Complete Binary Trees Full and Complete Binary Trees Binary Tree Theorems 1 Here are two important types of binary trees. Note that the definitions, while similar, are logically independent. Definition: a binary tree T is full

More information

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

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:

More information

The ADT Binary Search Tree

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

More information

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

More information

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

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

More information

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

How To Create A Tree From A Tree In Runtime (For A Tree) 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:

More information

Symbol Tables. Introduction

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

More information

Ordered Lists and Binary Trees

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:

More information

Converting a Number from Decimal to Binary

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

More information

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:

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)

More information

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

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

More information

Persistent Binary Search Trees

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

More information

Data Structures Fibonacci Heaps, Amortized Analysis

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:

More information

Binary Heap Algorithms

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

More information

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

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

More information

Data Structures and Algorithms

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

More information

Chapter 13: Query Processing. Basic Steps in Query Processing

Chapter 13: Query Processing. Basic Steps in Query Processing Chapter 13: Query Processing! Overview! Measures of Query Cost! Selection Operation! Sorting! Join Operation! Other Operations! Evaluation of Expressions 13.1 Basic Steps in Query Processing 1. Parsing

More information

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

More information

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

More information

Binary Search Trees (BST)

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

More information

Physical Data Organization

Physical Data Organization Physical Data Organization Database design using logical model of the database - appropriate level for users to focus on - user independence from implementation details Performance - other major factor

More information

A Comparison of Dictionary Implementations

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

More information

DATABASE DESIGN - 1DL400

DATABASE DESIGN - 1DL400 DATABASE DESIGN - 1DL400 Spring 2015 A course on modern database systems!! http://www.it.uu.se/research/group/udbl/kurser/dbii_vt15/ Kjell Orsborn! Uppsala Database Laboratory! Department of Information

More information

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)

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) Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse

More information

File Management. Chapter 12

File Management. Chapter 12 Chapter 12 File Management File is the basic element of most of the applications, since the input to an application, as well as its output, is usually a file. They also typically outlive the execution

More information

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

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

More information

Binary Search Trees CMPSC 122

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

More information

Algorithms and Data Structures

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

More information

Review of Hashing: Integer Keys

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

More information

Lecture Notes on Binary Search Trees

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

More information

An Immediate Approach to Balancing Nodes of Binary Search Trees

An Immediate Approach to Balancing Nodes of Binary Search Trees Chung-Chih Li Dept. of Computer Science, Lamar University Beaumont, Texas,USA Abstract We present an immediate approach in hoping to bridge the gap between the difficulties of learning ordinary binary

More information

Big Data and Scripting. Part 4: Memory Hierarchies

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)

More information

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers B+ Tree and Hashing B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers B+ Tree Properties Balanced Tree Same height for paths

More information

Lecture 1: Data Storage & Index

Lecture 1: Data Storage & Index Lecture 1: Data Storage & Index R&G Chapter 8-11 Concurrency control Query Execution and Optimization Relational Operators File & Access Methods Buffer Management Disk Space Management Recovery Manager

More information

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, cheapest first, we had to determine whether its two endpoints

More information

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

More information

Binary Trees and Huffman Encoding Binary Search Trees

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

More information

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

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,

More information

- 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

- 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

More information

Lecture Notes on Binary Search Trees

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

More information

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

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

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 6: AVL Trees Michael Bader Winter 2011/12 Chapter 6: AVL Trees, Winter 2011/12 1 Part I AVL Trees Chapter 6: AVL Trees, Winter 2011/12 2 Binary Search Trees Summary Complexity

More information

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

More information

10CS35: Data Structures Using C

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

More information

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

More information

Algorithms Chapter 12 Binary Search Trees

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

More information

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

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

More information

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92. Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure

More information

GRAPH THEORY LECTURE 4: TREES

GRAPH THEORY LECTURE 4: TREES GRAPH THEORY LECTURE 4: TREES Abstract. 3.1 presents some standard characterizations and properties of trees. 3.2 presents several different types of trees. 3.7 develops a counting method based on a bijection

More information

Section IV.1: Recursive Algorithms and Recursion Trees

Section IV.1: Recursive Algorithms and Recursion Trees Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller

More information

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n) Hash Tables Tables so far set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n) Worst O(lg n) O(lg n) O(lg n) Table naïve array implementation

More information

Cpt S 223. School of EECS, WSU

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)

More information

Binary Heaps. CSE 373 Data Structures

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

More information

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

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

More information

Analysis of Binary Search algorithm and Selection Sort algorithm

Analysis of Binary Search algorithm and Selection Sort algorithm Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to

More information

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011 November 14, 2011 1 Real Time Applications 2 3 Results of 4 Real Time Applications Splay trees are self branching binary search tree which has the property of reaccessing the elements quickly that which

More information

Merkle Hash Trees for Distributed Audit Logs

Merkle Hash Trees for Distributed Audit Logs Merkle Hash Trees for Distributed Audit Logs Subject proposed by Karthikeyan Bhargavan Karthikeyan.Bhargavan@inria.fr April 7, 2015 Modern distributed systems spread their databases across a large number

More information

Chapter 8: Structures for Files. Truong Quynh Chi tqchi@cse.hcmut.edu.vn. Spring- 2013

Chapter 8: Structures for Files. Truong Quynh Chi tqchi@cse.hcmut.edu.vn. Spring- 2013 Chapter 8: Data Storage, Indexing Structures for Files Truong Quynh Chi tqchi@cse.hcmut.edu.vn Spring- 2013 Overview of Database Design Process 2 Outline Data Storage Disk Storage Devices Files of Records

More information

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 13-1

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 13-1 Slide 13-1 Chapter 13 Disk Storage, Basic File Structures, and Hashing Chapter Outline Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files Hashed Files Dynamic and Extendible

More information

Chapter 13. Chapter Outline. Disk Storage, Basic File Structures, and Hashing

Chapter 13. Chapter Outline. Disk Storage, Basic File Structures, and Hashing Chapter 13 Disk Storage, Basic File Structures, and Hashing Copyright 2007 Ramez Elmasri and Shamkant B. Navathe Chapter Outline Disk Storage Devices Files of Records Operations on Files Unordered Files

More information

Lecture 1: Course overview, circuits, and formulas

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

More information

Analysis of Algorithms I: Optimal Binary Search Trees

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

More information

DATA STRUCTURES USING C

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

More information

Exam study sheet for CS2711. List of topics

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

More information

Why Use Binary Trees?

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

More information

Sample Questions Csci 1112 A. Bellaachia

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

More information

Record Storage and Primary File Organization

Record Storage and Primary File Organization Record Storage and Primary File Organization 1 C H A P T E R 4 Contents Introduction Secondary Storage Devices Buffering of Blocks Placing File Records on Disk Operations on Files Files of Unordered Records

More information

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.

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. 1. The advantage of.. is that they solve the problem if sequential storage representation. But disadvantage in that is they are sequential lists. [A] Lists [B] Linked Lists [A] Trees [A] Queues 2. The

More information

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

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4) 1 Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Binary Tree 2 Binary Tree tree with

More information

Binary Search Trees. Ric Glassey glassey@kth.se

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

More information

Chapter 13. Disk Storage, Basic File Structures, and Hashing

Chapter 13. Disk Storage, Basic File Structures, and Hashing Chapter 13 Disk Storage, Basic File Structures, and Hashing Chapter Outline Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files Hashed Files Dynamic and Extendible Hashing

More information

Chapter 13 Disk Storage, Basic File Structures, and Hashing.

Chapter 13 Disk Storage, Basic File Structures, and Hashing. Chapter 13 Disk Storage, Basic File Structures, and Hashing. Copyright 2004 Pearson Education, Inc. Chapter Outline Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files

More information

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

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

More information

Algorithms and Data Structures

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

More information

Unit 4.3 - Storage Structures 1. Storage Structures. Unit 4.3

Unit 4.3 - Storage Structures 1. Storage Structures. Unit 4.3 Storage Structures Unit 4.3 Unit 4.3 - Storage Structures 1 The Physical Store Storage Capacity Medium Transfer Rate Seek Time Main Memory 800 MB/s 500 MB Instant Hard Drive 10 MB/s 120 GB 10 ms CD-ROM

More information

PES Institute of Technology-BSC QUESTION BANK

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

More information

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

More information

Why you shouldn't use set (and what you should use instead) Matt Austern

Why you shouldn't use set (and what you should use instead) Matt Austern Why you shouldn't use set (and what you should use instead) Matt Austern Everything in the standard C++ library is there for a reason, but it isn't always obvious what that reason is. The standard isn't

More information

Class Notes CS 3137. 1 Creating and Using a Huffman Code. Ref: Weiss, page 433

Class Notes CS 3137. 1 Creating and Using a Huffman Code. Ref: Weiss, page 433 Class Notes CS 3137 1 Creating and Using a Huffman Code. Ref: Weiss, page 433 1. FIXED LENGTH CODES: Codes are used to transmit characters over data links. You are probably aware of the ASCII code, a fixed-length

More information

6 March 2007 1. Array Implementation of Binary Trees

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

More information

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

More information

Data Structure with C

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

More information

Rotation Operation for Binary Search Trees Idea:

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

More information

Data Warehousing und Data Mining

Data Warehousing und Data Mining Data Warehousing und Data Mining Multidimensionale Indexstrukturen Ulf Leser Wissensmanagement in der Bioinformatik Content of this Lecture Multidimensional Indexing Grid-Files Kd-trees Ulf Leser: Data

More information

Chapter 8: Bags and Sets

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

More information

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

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

More information

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

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

More information

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search Chapter Objectives Chapter 9 Search Algorithms Data Structures Using C++ 1 Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential

More information

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis CS711008Z Algorithm Design and Analysis Lecture 7 Binary heap, binomial heap, and Fibonacci heap 1 Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 The slides were

More information

Chapter 14 The Binary Search Tree

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

More information

INTRODUCTION The collection of data that makes up a computerized database must be stored physically on some computer storage medium.

INTRODUCTION The collection of data that makes up a computerized database must be stored physically on some computer storage medium. Chapter 4: Record Storage and Primary File Organization 1 Record Storage and Primary File Organization INTRODUCTION The collection of data that makes up a computerized database must be stored physically

More information

Persistent Data Structures

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

More information

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

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)

More information

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

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

More information