# 1 2-3 Trees: The Basics

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 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 more depth. We saw how to traverse them, search in them, and mostly, how to insert and delete Trees: The Basics The beginning of these notes is just a recap from the previous lecture s notes. 2-3 Trees differ from Binary Search trees in that each node contains 1 or 2 keys. Unless it is a leaf, a node with 1 key has exactly 2 children, and a node with 2 keys has exactly 3 children. The defining properties of 2-3 Trees are the following: All leaves must be at the same level. For nodes with 1 key k, the binary search tree property holds: all keys in the left subtree are smaller than k, and all keys in the right subtree are greater than k. For nodes with 2 keys k 1 < k 2, the three subtrees T 1,T 2,T 3 satisfy the following property: 1. All keys in T 1 are smaller than k All keys in T 2 are strictly between k 1 and k All keys in T 3 are greater than k 2. It will be our job in building these trees to ensure that the defining properties hold at all times. To implement a 2-3 Tree, we will need a slightly revised version of a node, to accommodate multiple keys and values. The class would look as follows 1 : template <class KeyType, class ValueType> class Node { // variables could be public, or could add getters/setters int numberkeys; KeyType key[2]; // space for up to 2 keys ValueType value[2]; // space for the corresponding values Node* subtree[3]; // pointers to the up to 3 subtrees } The first useful observation about 2-3 Trees is that like other search trees they make it really easy to output all keys in sorted order. We simply traverse the tree using depth-first in-order traversal. That is, at any node, we first recursively visit the entire left subtree, then the key (or left key, if there are two), then the middle tree (if it exists), the right key (if it exists), and finally the right subtree. This is the natural generalization of in-order traversal from binary trees to 2-3 Trees. If you do not remember how to traverse a tree, check back to the lecture notes from Lecture 17. This describes how to implement an ordered iterator over search trees. This is one of the main advantages of search trees over Hashtables as data structures for Dictionaries. For Hashtables, it s much harder to implement an efficient Iterator. (Hashtables have other advantages to compensate.) In implementing the operations on 2-3 Trees (in particular, insertions and deletions), we should keep in mind the following: 1 This is slightly different from what I showed in lecture, where I used variables leftkey and rightkey. When you put them in an array, some of the operations we see later are actually easier to implement, hence the change. 1

2 Our 2-3 Tree properties must always hold, so that the tree remains balanced. Thus, we must ensure that two-key nodes have three children, one-key nodes have two children, and we can t have 3 keys in one node. All insertions and removals in search trees typically (and definitely for 2-3 Trees) happen at a leaf; that s much easier to deal with than changing things at internal nodes. Typically, pre-processing (for removals) and/or post-processing (to restore various properties) is required, in addition to just adding or removing keys. Internally, the best way to store a 2-3 Tree is probably with a dynamic memory structure and pointers, storing a pointer to the root somewhere. Initially, when the tree is empty, we can set the root pointer to NULL. The tree will grow both in number of nodes and height as insertions happen, so obviously, we do not want to fix its size ahead of time. We could also try to store the tree in an array (like we did for Priority Queues), but lose all of the advantages of doing so, as index calculations are not possible any more. When the tree was complete and binary, it was very easy to figure out which were the children of a given node, but here, it would depend on the degrees of many other nodes, which would take way too long to look up. Next, we will explore how the different operations (insertions and removals) work on trees. Our running example will be the tree T shown in Figure 1: 6 1 1, Figure 1: Our running example T of a 2-3 Tree. 2 Insertions Whenever we insert a key (or, more precisely, a key-value pair) into a tree, the first thing we do is search for it, unsuccessfully, of course. We then insert it into the leaf node where the search terminated. If the leaf node only had one key before, it has two keys now, and everything is good. But if the leaf node already had two keys, then it now has three keys, so we ll need to fix it. We will see how to fix it by looking at the effects of inserting a number of keys into our example tree T. First, let s look at the case of inserting 21 into T. To do so, we search for 21. When we do that, we will terminate at the node that already contains the key 23. This leaf node has only one key, so we can simply add 21. The result will only be a local change, and our tree now looks as in Figure 2: This was easy. Of course, we won t always be so lucky as to insert into a node with only a single key. So next, let s look at what happens when we insert 26 into the tree we got after inserting 21. The key 26 also belongs in the node with 21 and 23, so when we add it to the node, that leaf now contains three keys, as shown in Figure 3: To fix this, we can break up a node with 3 keys, by moving its middle key up to the parent node, and making the two other keys into separate nodes with one key each. In our example, 23 gets moved into the node that currently contains only the key, turning it into a node containing and 23. The other two elements, 21 and 26, are split into two separate nodes (since the parent node is now a two-key node, it can 2

3 Figure 2: T after inserting the key 21. In this and subsequent figures, we ll just write triangles to denote parts that are unchanged or not particularly interesting Figure 3: After inserting the key 26. Having three keys in a node is illegal, so we need to fix this. support three children), and these nodes become the children nodes for the node containing and 23. The resulting tree looks as shown in Figure Figure : After fixing the problem from inserting the key 26. So in this case, with just one split operation, the problem was fixed. Next, let s see what happens when we insert 12 into T. (So we re thinking of inserting it into the original tree, but it makes no difference in this particular case if you want to think of inserting it into the tree after we already inserted 21 and 26.) Again, we first search for 12, ending up in the leaf that contains the keys 11 and 13 already. Again, we add it to the leaf node, resulting in 3 keys in that node. The resulting situation is shown in Figure 5. As before, we take the middle key here, 12 and move it up to the parent s node, splitting the keys 11 and 13 into separate new nodes. But now, the parent has 3 keys, and subtrees, as shown in Figure 6. So we ll have to fix that. To fix this new problem of a node with 3 keys, we again take the middle key in this case, 10 and move it up one level, into the parent node with keys 6 and 1. Now, the keys 8 and 12 get split and become their own nodes; the new node containing 8 obtains the left two subtrees (here, nodes with keys 7 and 9), 3

4 Figure 5: The result of inserting the key 12 into T Figure 6: Moving the key 12 one level up. while the node with key 12 obtains the right two subtrees, with keys 11 and 13. The resulting tree is shown in Figure Figure 7: Moving the key 10 one level up. Now, we have moved the problem yet another level up. The root node now contains three keys 6, 10, and 1. So we do the same thing: we push up the middle key (here, 10) one layer, and split the other two keys into separate nodes, each inheriting two of the subtrees. The result is shown in Figure 8 At this point, we finally have obtained a legal 2-3 tree again, and the process terminates. Notice that we have now increased the height of the tree by one. Some students were wondering in class how the tree can ever grow in height if we always insert at leaves. Now we know. It grows at the root, even though insertion happens at the leaves. The important thing to keep in mind is that whenever a node has too many keys, you move up the middle key to the parent node, and split the remaining two keys into two separate new nodes. The left node inherits the two left subtrees, while the right node inherits the two right subtrees. Below is pseudocode to help you understand how to insert into 2-3 Trees in general.

5 Figure 8: Moving the key 10 one more level up. Inserting a new key 1. Search for the key. If found, deal with duplicate. Otherwise, we have found the leaf where the key belongs. 2. Insert the key into the leaf. If the leaf only had one key before, we are done now. 3. Otherwise, we must now fix the leaf. To fix a node with 3 keys (a b c): 1. Move b up into the parent node if there is one. (Otherwise, create a new root for the tree, containing just b.) 2. Create a node with just a as the key, and make it the left child of the b node. 3. Create a node with just c as the key, and make it the right child of the b node. Notice that insertion as we defined it maintains the properties of a 2-3 Tree: the number of levels on all subtrees is increased by one simultaneously when creating a new root, and not increased for any node otherwise. Let s try to analyze the running time. To find the right leaf node takes Θ(h), where h is the height of the tree. To actually insert the key into the tree takes Θ(1). Breaking any three-key node into new smaller nodes takes Θ(1), and we have to do this at most once for each level of the tree, so it takes Θ(h). So next, we want to calculate the height of a 2-3 Tree. We do this the way we have calculated tree heights before. If the tree has height h, because all leaves are at the same level, and each internal node has at least 2 children, it has at least as many nodes as a complete binary tree of h levels (plus one more), giving us an upper bound of log 2 (n) on the height, as we calculated before in the context of heaps. On the other hand, a tree of h levels with maximum degree 3 has at most 1 node on level 0, 3 nodes on level 1, 9 nodes on level 2, and generally 3 k nodes on level k, for a total of at most h 1 k=0 3k = 3h h 2 nodes. Thus, n 3h /2, giving us that h log 3 (2n) log 3 (n). Thus, h is between log 3 (n) and log 2 (n), and those two are within small constant factors of each other. Thus, the runtime for insertion is Θ(log(n)). (We can ignore the base of the logarithm, as they are off only by constant factors.) 3 Removals To understand how removing a key from a 2-3 Tree works, let s return to our original tree T, but also add another key 16 to it. The resulting tree is shown in Figure 9. Now, let s see what happens when we want to remove the key 1. As we mentioned earlier, removing keys from internal nodes is a glorious mess, since we wouldn t have the right number of keys for the corresponding 5

6 Figure 9: The tree T with the key 16 added. number of subtrees any more. Thus, removals should always happen at leaves. In order to accomplish this, we need to first do some swapping. The swap will always be with the immediate predecessor (next smallest key in the tree) or immediate successor (next largest key in the tree). For concreteness in this class, we will use the immediate successor. To find it, take the next edge right from the key to delete, and then the leftmost edge for all subsequent levels. So the pre-processing step is to find the successor whenever the key is not already in a leaf, and swap the key with the successor key. The result of swapping 1 (key to delete) with its successor (16) is shown in Figure Figure 10: After 16 and 1 have been swapped. Now, the key can be safely deleted from the leaf. Notice that this maintains the search tree property, as the successor was safe to take the place of the key we want to delete. The result of deleting 1 now is shown in Figure Figure 11: After the deletion of the key 1. Deleting the key 1 was pretty easy: after the swap, its leaf node contained two keys, which made it safe to just delete a key. Next, let s see what happens if we remove the key 5. That key is in a node all by itself, 6

7 so after we remove it, we have an empty leaf node. This situation is shown in Figure Figure 12: After the deletion of the key 5. The now-empty node is shown in black. Here, the empty node has a sibling with two keys 1 and 3. In this case, we can move the key from the parent down to fill the node, and move one of the keys from the sibling up to the parent. The result is the tree in Figure 13: Figure 13: Borrowing a key from the sibling via the parent. Next, let s see how borrowing from a sibling works when a node has two siblings, and the one with two keys is the furthest. To see this, let s look at what happens if we next remove the key 7. The intermediate stage will be the following: Figure 1: Deleting the key 7. The now-empty node is shown in black. This time, borrowing a key from the sibling still works, but takes a few more intermediate steps. The key 11 from the sibling gets moved up to the parent node; the key 10 from the parent gets moved down to the other sibling, while the key 9 from the other sibling gets moved up to the parent. Finally, we can move the key 8 down to the empty node. The result is only locally rearranged, and shown in Figure 15. Next, let s see what happens when there s no sibling to borrow from. To see this, we delete the key 8 next. The resulting tree is shown in Figure 16. 7

8 Figure 15: Borrowing from a 2-step removed sibling via the parent. This requires some moving up and down of keys, but only between the siblings and the parent Figure 16: Deleting the key 8. The now-empty node is shown in black. Now, we cannot borrow from a sibling directly. But here, fortunately, the empty node has a parent with two keys, so it s possible to borrow a key from the parent, and another one from the immediately neighboring sibling. The resulting tree is shown in Figure Figure 17: Borrowing a key from the parent. So far, we have been able to handle every deletion with just one constant-time fix. But you will have noticed that there is one case we have not yet considered: that the node s parent has one key (so one cannot borrow from the parent), and its only sibling has only one key (so one cannot borrow from the sibling). To explore this case, let s look at the effect of deleting the key 17 from the tree next. The first step of deletion is shown in Figure 18. Now, there is no easy way to fix the problem at this level. So we need to escalate the problem one level up. To do so, we combine the one key in the parent node with the one key in the sibling node to form a node with two keys, and make the parent empty. In other words, we produce the tree shown in Figure : At the next higher level, we now have a parent with two keys, so we can borrow from the parent to fix the problem, giving us the tree shown in Figure 20. 8

9 Figure 18: Deleting the key 17. The now-empty node is shown in black Figure : Combining a parent key with a sibling key, and passing the problem of the empty node up one level. Of course, we were a bit lucky if the parent had had only one key, we may have had to pass the problem up yet another level, possibly all the way up to the root. Once the root is empty, though, it is safe to simply delete it, thereby reducing the height of the tree by 1. In summary, we get the following cases for deletion of a key: If the leaf containing the key had 2 keys, we can just remove it and are done. Otherwise, we will now describe different cases for dealing with a node (leaf or internal) that has 0 keys left, and possibly one subtree hanging off: 1. If the node has a sibling with 2 keys, then rearrange one key from the sibling and keys from the parent to fix the problem completely. 2. If all siblings have only one key, but the parent has 2 keys, then use a key from the parent and one from the sibling to fix the problem completely Figure 20: Borrowing from the parent to resolve the empty node in the middle of the tree. 9

10 3. If the parent has only one key and the only sibling also has only one key, then merge the parent s key and the sibling s key, and shift the empty node up one level. Recurse (or iterate) to solve the problem there. We will see these removal rules again in a little more detail in the next lecture (and its notes). 10

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

### Lecture: #9-10. Binary Trees

Lecture: #9-10 Binary Trees Topics 1. Definition and Application of Binary Trees 2. Binary Search Tree Operations 3. Template Considerations for Binary Search Trees 19-2 1. Definition and Application of

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

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

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

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

### Data Structures. Topic #9

Data Structures Topic #9 Today s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency of tree Are there any alternatives? 2-3 2-3-4

### CSE 5311 Homework 2 Solution

CSE 5311 Homework 2 Solution Problem 6.2-6 Show that the worst-case running time of MAX-HEAPIFY on a heap of size n is Ω(lg n). (Hint: For a heap with n nodes, give node values that cause MAX- HEAPIFY

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

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

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

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

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

### Trees & Binary Search Trees

CMSC 132: Object-Oriented Programming II Trees & Binary Search Trees Department of Computer Science University of Maryland, College Park Trees Trees are hierarchical data structures One-to-many relationship

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

### Unit 6: Binary Trees Part 2. Deletion. Deletion by merging. Deletion by merging Deletion by copying. Engineering 4892: Data Structures.

Unit : Binary Trees Part 2 Engineering 92: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland 1 Deletion Deletion by copying 1 Balanced trees AVL trees July 11,

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

### The ADT Binary Search Tree. Recursive Tree Traversals. The ADT Binary Search Tree. The ADT Binary Search Tree. In-order

Recursive Tree Traversals The ADT Binary Search Tree In-order private void printinorder(treenode node) { if (node!= null) { printinorder(node.getleft()); System.out.print(node.getItem() + " "); printinorder(node.getright());

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

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

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

### Data Structures Using C++ 2E. Chapter 11 Binary Trees and B-Trees

Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees Binary Trees Definition: a binary tree, T, is either empty or such that T has a special node called the root node T has two sets of nodes,

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

### Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Prof. Dr. Harald Gall. Institut für Informatik Universität Zürich

Stacks, Queues & Trees Prof. Dr. Harald Gall Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch The Stack Interface!! Stack!! Data structure holding several items.!! New items added to the

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

### Balanced search trees

Lecture 8 Balanced search trees 8.1 Overview In this lecture we discuss search trees as a method for storing data in a way that supports fast insert, lookup, and delete operations. (Data structures handling

### CS 3410 Ch 19 Binary Search Trees

CS 3410 Ch 19 Binary Search Trees Sections 19.1, 19.3-19.6 Pages 687-697, 702-737 Exercises 19 The find operation for a linked list is ( ), which can be prohibitive with a large amount of data. In this

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

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

### Data Structures. Topic #8

Data Structures Topic #8 Today s Agenda Continue Discussing Table Abstractions But, this time, let s talk about them in terms of new non-linear data structures trees which require that our data be organized

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

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

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

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

### Labworks 1: Binary Search Trees and Red-Black trees

Labworks 1: Binary Search Trees and Red-Black trees October 12, 2016 Deadline for these labworks: October 26 2016 How? Make an archive with the source files of your implementation, and send it by email

### Nearly Complete Binary Trees and Heaps

Nearly Complete Binary Trees and Heaps DEFINITIONS: i) The depth of a node p in a binary tree is the length (number of edges) of the path from the root to p. ii) The height (or depth) of a binary tree

### CS271: Data Structures. Binary Search Trees

CS271: Data Structures Binary Search Trees This project has three parts: an implementation of a binary search tree class, a testing plan, and an experiment in tree height. 1. PART 1 Implement a binary

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

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

### Multiway Search Tree (MST)

Multiway Search Tree (MST) Generalization of BSTs Suitable for disk MST of order n: Each node has n or fewer sub-trees S1 S2. Sm, m n Each node has n-1 or fewer keys K1 Κ2 Κm-1 : m-1 keys in ascending

### School of Informatics, University of Edinburgh Computer Science 1 Ah [03 04]

School of Informatics, University of Edinburgh Computer Science 1 Ah [03 04] CS1Ah Lecture Note 25 Binary Search Trees This lecture studies binary trees in more detail, in particular their application

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

Universidad Carlos III de Madrid Algorithms and Data Structures (ADS) Bachelor in Informatics Engineering Computer Science Department Binary Search Trees. Authors: Julian Moreno Schneider Isabel Segura-Bedmar

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

### Binary Search Trees (BST)

Binary Search Trees (BST) 1. Hierarchical data structure with a single pointer 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 property:

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

### Binary Search Trees (continued)

Binary Search Trees (continued) Remove: Starting at the root, search for the value to be remove. If we reach an empty tree, the value is not in the tree. Otherwise, at some point the value is the root

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

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

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

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

### Binary Trees and Binary Search Trees C++ Implementations

Binary Trees and Binary Search Trees C++ Implementations Learning Goals: Apply basic tree definitions to classification problems. Describe the properties of binary trees, binary search trees, and more

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

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

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

### Practice Problems (Midterm)

ECE-250 Algorithms and Data Structures (Winter 2012) Practice Problems (Midterm) Disclaimer: Please do keep in mind that this problem set does not reflect the exact topics or the fractions of each of the

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

### Lecture 6: Binary Search Trees CSCI Algorithms I

Lecture 6: Binary Search Trees CSCI 700 - Algorithms I Andrew Rosenberg September 24, 2009 Last Time Linear Time Sorting Counting Sort Radix Sort Bucket Sort Today Some C++ review Binary Search Trees Homework

### Introduction to Algorithms. Lecture 4

6.006- Introduction to Algorithms Lecture 4 Menu Priority Queues Heaps Heapsort Priority Queue A data structure implementing a set S of elements, each associated with a key, supporting the following operations:

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

### Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4

Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4 I. Algorithm Design and Divide-and-Conquer There are various strategies we

### 7.1 Introduction. CSci 335 Software Design and Analysis III Chapter 7 Sorting. Prof. Stewart Weiss

Chapter 7 Sorting 7.1 Introduction Insertion sort is the sorting algorithm that splits an array into a sorted and an unsorted region, and repeatedly picks the lowest index element of the unsorted region

### Deleting a Data Entry from a B+ Tree

Deleting a Data Entry from a B+ Tree Start at root, find leaf L where entry belongs. Remove the entry. If L is at least half-full, done! If L has only d-1 entries, Try to re-distribute, borrowing from

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

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

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

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

### Data Structure Instructor: Nabeel Alassaf Chapter 11 Binary Search Trees Lecture 3

Data Structure Instructor: Nabeel Alassaf Chapter 11 Binary Search Trees Lecture 3 Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١ Binary Search Trees A binary search

### Data Structure [Question Bank]

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

### CS383, Algorithms Notes on Lossless Data Compression and Huffman Coding

Prof. Sergio A. Alvarez http://www.cs.bc.edu/ alvarez/ Fulton Hall 410 B alvarez@cs.bc.edu Computer Science Department voice: (617) 552-4333 Boston College fax: (617) 552-6790 Chestnut Hill, MA 02467 USA

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

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

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

### Sorting algorithms. CS 127 Fall A brief diversion: What's on the midterm. Heaps: what to know. Binary trees: What to know

CS 127 Fall 2003 Sorting algorithms A brief diversion: What's on the midterm Same format as last time 5 questions, 20 points each, 100 points total 15% of your grade 2 topics: trees, trees, and more trees

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

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

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

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

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

### CS174 Lecture 3 John Canny

CS174 Lecture 3 John Canny Randomized Quicksort and BSTs A couple more needed results about permutations: Q1: What s the probability that 4 comes before 5 in a random permutation? Tempting to say 1/2,

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

### Class 28: Binary Search Trees. Binary Search Trees

Introduction to Computation and Problem Solving Class 2: Binary Search Trees Prof. Steven R. Lerman and Dr. V. Judson Harward Binary Search Trees In the previous lecture, we defined the concept of binary

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

### CS2 Final Study Guide

CS2 Final Study Guide - Know how to write an assignment operator, copy constructor and destructor for a simple class that has dynamically allocated memory. Trees 1. Define the following terms o binary

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

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

### Lecture 3. Reading: Weiss Ch 4, sections 1-4 CSE 100, UCSD: LEC 3. Page 1 of 22

Lecture 3 Binary search trees Toward a binary search tree implementation using C++ templates C++ iterators and the binary search tree successor function Reading: Weiss Ch 4, sections 1-4 Page 1 of 22 Binary

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

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) and the general binary

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

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

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

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

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

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

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

### Computational Geometry

Range trees 1 Range queries Database queries salary G. Ometer born: Aug 16, 1954 salary: \$3,500 A database query may ask for all employees with age between a 1 and a 2, and salary between s 1 and s 2 2

### COSC242 Lecture 8 More Quicksort

COSC242 Lecture 8 More Quicksort If the pivot divides the array into two pieces of roughly the same size each time, then Quicksort behaves like Mergesort with log n splits of the array. More precisely,

### 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 18 Indexing Structures for Files. Indexes as Access Paths

Chapter 18 Indexing Structures for Files Indexes as Access Paths A single-level index is an auxiliary file that makes it more efficient to search for a record in the data file. The index is usually specified

### Topic 15. The Binary Search Tree ADT

Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such that the data in any internal node is Greater than the

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

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

### 12 Abstract Data Types

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

### Algorithms Chapter 6 Heapsort

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