Binary Search Trees Readings: Chapter 11

Size: px
Start display at page:

Download "Binary Search Trees Readings: Chapter 11"

Transcription

1 Binary Search Trees Readings: Chapter 11 CSE214: Computer Science II Fall 2016 CHEN-WEI WANG

2 Binary Search Tree: Definition A binary search tree ( BST ) is a binary tree, where each internal node p stores a key-value pair (k, v), such that: For each node n of the LST of p: key(n) < key(p) For each node n of the RST of p: key(n) > key(p) Node p stores (key(p), value(p)) Each node n of LST is such that key(n) < key(p) Each node n of RST is such that key(n) > key(p) A BST can be used to efficiently implement a sorted map. ) All stored keys are unique (i.e., no duplicates). 2 of 39

3 BST: Sorting Property An in-order traversal of the keys stored in a BST will result in a sequence arranged in an increasing order Justification: In-order traversal: visit LST, then root, then RST 3 of 39 BST : keys in LST < root s key and keys in RST > root s key 80

4 BST: Internal Nodes vs. External Nodes We store key-value pairs only in internal nodes. We treat external nodes as sentinels, in order to simplify the definitions of BST algorithms. Similar to how we treat header and trailer in a doubly-linked list 4 of 39

5 BST Operation: Searching (1) We may attempt to locate a particular key k in a BST rooted at node p, by viewing it as a decision tree. BinarySearch(p, k): if(isexternal(p)) { return p; /* unsuccessful search */ } else if (k == key(p)) then { return p; /* successful search */ } else if (k < key(p)) then { return BinarySearch(left(p), k); /* recur on LST */ } else { /* k > key(p) */ return BinarySearch(right(p), k); /* recur on RST */ } 5 of 39

6 BST Operation: Searching (2) A successful search for key of 39

7 BST Operation: Searching (3) An unsuccessful search for key The search terminates at left external child of the key 76 7 of 39

8 BST Operation: RT of Searching (1) Recursive calls of BinarySearch are made on nodes of path which Starts from the root Goes down one level at a time Stops when the key is found, or when a leaf is reached Maximum number of nodes visited by the search? [h + 1] RT of deciding from each node to go to LST or RST? [O(1)] So RT of a binary search is O(h) Recall: Given a binary tree with n modes, the height h is bounded as log(n + 1) 1 apple h apple n 1 Best RT of a binary search is O(log(n)) Worst RT of a binary search is O(n) 8 of 39 [balanced BT] [ill-balanced BT]

9 BST Operation: RT of Searching (2) Height Time per level O(1) Tree T: O(1) h O(1) Total time: O(h) 9 of 39

10 BST Operation: Insertion (1) We may insert an entry with key k and value v into a BST. We first need to know if k is an existing key in the BST. BSTInsert(k, v): p = BinarySearch(root(), k) if (k == key(p)) { /* k is an existing key */ set p s value to v } else { /* k is not an existing key */ turn p into an internal node storing (k, v) } Running time? [ O(h) ] 10 of 39

11 BST Operation: Insertion (2.1) Insert an entry with key 68 into the following BST: of 39

12 BST Operation: Insertion (2.2) After inserting an entry with key 68 into the above BST: of 39

13 BST Operation: Deletion (1) We may delete a node storing the key k from a BST. We first call p = BinarySearch(root(), k) to check if k is an existing key in the BST. Case 1: Node p is external k is not an existing key ) Nothing to remove Case 2: Node p s both children are external No orphan subtrees to be handled ) Remove p [BST property?] Case 3: One of the node p s children, say r, is internal r s sibling is external ) Replace p with r [BST property?] Case 4: Both of node p s children are internal Locate r containing the greatest key s.t. key(r) < key(p) ) r is the right-most internal node r of the LST of p Replace p s entry with r s entry [BST property?] r being the right-most internal node does not have an internal right child ) Remove r as defined in Case 3 [BST Property?] Running time? [ O(h) ] 13 of 39

14 BST Operation: Deletion (2.1) Delete the node storing key 32 from the following BST: p 88 8 r of 39

15 BST Operation: Deletion (2.2) After deleting the node storing key 32 from the above BST: r of 39

16 BST Operation: Deletion (3.1) Delete the node storing key 88 from the following BST: p r of 39

17 BST Operation: Deletion (3.2) After deleting the node storing key 88 from the above BST: p r of 39

18 Homework (1) Study the Java implementation of a sorted Map using a binary search tree (Section ). 18 of 39

19 Balanced Binary Search Trees: Motivation After insertions into a BST, the worst-case RT of a search occurs when the height h is at its maximum: O(n) : Entries were inserted in an decreasing order of their keys ) One-path, left-slanted BST [e.g., h100, 75, 68, 60, 50, 1i] Entries were inserted in an increasing order of their keys ) One-path, right-slanted BST [e.g., h1, 50, 60, 68, 75, 100i] Last entry s key is in-between keys of the previous two entries ) One-path, side-alternating BST [e.g., h1, 100, 50, 75, 60, 68i] To avoid the worst-case RT (* a ill-balanced tree), we need to take actions as soon as the tree becomes unbalanced. 19 of 39

20 Balanced Binary Search Trees: Definition Given a position (or a node) p, the height of the subtree rooted at p is recursively defined as: ( 0 if p is external height (p) = 1 + MAX ({ height (c) parent (c) =p }) if p is internal A balanced BST T satisfies the height-balance property : For every internal node v, heights of v s child nodes differ apple of 39 Q: Will the tree remain balanced after inserting 55? [NO] Q: Will the tree remain balanced after inserting 63? [YES ]

21 Fixing Unbalanced BST: Rotations A tree rotation is performed: When the latest insertion/deletion creates unbalanced nodes, along the ancestor path of the node being inserted/deleted. To change the shape of tree, maintaining the search property h + 2 h + 2 a b h + 1 h h b c rotate node b a h + 1 h c h T4 to the right h h T3 T1 T2 T3 T4 T1 T2 An in-order traversal on the resulting tree still produces a sorted sequence of keys. [ht 1, c, T 2, b, T 3, a, T 4 i] After rotating node b to the right: Heights of descendants (c, T 1, T 2, T 3 ) and sibling (T 4 ) remain same. Height of parent (a) is decreased by 1. ) If node a was unbalanced, then this rotation may have fixed it. 21 of 39

22 After Insertions: Trinode Restructuring via Rotation(s) After inserting a new node v: Case 1: Nodes alone the ancestor path of v remains balanced ) No rotations needed Case 2: Found a non-empty sequence of nodes alone the ancestor path of v that become unbalanced 1. Get the first (i.e., lowest) unbalanced node a in v s ancestor path 2. Get a s child node b in v s ancestor path 3. Get b s child node c in v s ancestor path 4. Perform rotation(s) based on the alignment of a, b, and c: Slanted the same way ) single rotation on the middle node b Slanted different ways ) double rotations on the lower node c 22 of 39

23 Trinode Restructuring: Single, Left Rotation a b T1 c T2 T3 T4 Left Rotation on node b (height of unbalanced subtree #) b a c T1 T2 T3 T4 BST property maintained? [ht 1, a, T 2, b, T 3, c, T 4 i] 23 of 39

24 Exercise: Left Rotation Insert the following sequence of nodes into an empty BST: h44, 17, 78, 32, 50, 88, 95i Is the BST now balanced? Insert 100 into the BST Is the BST now balanced? Perform the left rotation on the appropriate node. 24 of 39

25 Trinode Restructuring: Single, Right Rotation a b c T4 T3 T1 T2 Right Rotation on node b (height of unbalanced subtree #) b c a T1 T2 T3 T4 25 of 39 BST property maintained? [ht 1, a, T 2, b, T 3, c, T 4 i]

26 Exercise: Right Rotation Insert the following sequence of nodes into an empty BST: h44, 17, 78, 32, 50, 88, 48i Is the BST now balanced? Insert 46 into the BST Is the BST now balanced? Perform the right rotation on the appropriate node. 26 of 39

27 Trinode Restructuring: Double, R-L Rotations a b T1 c T4 T2 T3 Right Rotation on node c a c T1 b T2 T3 T4 Then Left Rotation on c (height of unbalanced subtree #) 27 of 39

28 Exercise: R-L Rotations Insert the following sequence of nodes into an empty BST: h44, 17, 78, 32, 50, 88, 82, 95i Is the BST now balanced? Insert 85 into the BST Is the BST now balanced? Perform the R-L rotations on the appropriate node. 28 of 39

29 Trinode Restructuring: Double, L-R Rotations a b c T4 T1 T2 T3 Left Rotation on node c a c b T4 T3 T1 T2 Then Right Rotation on c (height of unbalanced subtree #) 29 of 39

30 Exercise: L-R Rotations Insert the following sequence of nodes into an empty BST: h44, 17, 78, 32, 50, 88, 48, 62i Is the BST now balanced? Insert 54 into the BST Is the BST now balanced? Perform the L-R rotations on the appropriate node. 30 of 39

31 After Deletions: Continuous Trinode Restructuring After deleting an existing node, whose only child is v: Case 1: Nodes alone the ancestor path of v remains balanced ) No rotations needed Case 2: Found a non-empty sequence of nodes alone the ancestor path of v that become unbalanced 1. Get the first (i.e., lowest) unbalanced node a in v s ancestor path 2. Get a s taller child node b [b 62 v s ancestor path] 3. Choose b s child node c as follows: b s two child nodes have different heights ) c is the taller child b s two child nodes have same height ) a, b, c slant the same way 4. Perform rotation(s) based on the alignment of a, b, and c: Slanted the same way ) single rotation on the middle node b Slanted different ways ) double rotations on the lower node c Continue to apply Case 2 (i.e., unbalanced node found along v s ancestor path) until Case 1 is satisfied. [ O(log n) rotations] 31 of 39

32 Exercise: Single Trinode Restructuring Step Insert the following sequence of nodes into an empty BST: h44, 17, 62, 32, 50, 78, 48, 54, 88i Is the BST now balanced? Delete 32 from the BST Is the BST now balanced? Perform the left rotation on the appropriate node. Is the BST now balanced? 32 of 39

33 Exercise: Multiple Trinode Restructuring Steps Insert the following sequence of nodes into an empty BST: h50, 25, 10, 30, 5, 15, 27, 1, 75, 60, 80, 55i Is the BST now balanced? Delete 80 from the BST Is the BST now balanced? Perform the right rotation on the appropriate node. Is the BST now balanced? Perform the right rotation on the appropriate node. Is the BST now balanced? 33 of 39

34 Restoring Balance from Insertions Before Insertion into T3 After Insertion into T3 h + 3 h + 4 a a h + 2 b h + 1 h + 3 b h + 1 h + 1 h + 1 c T4 h + 1 h + 2 c T4 T1 h h T1 h h + 1 T2 T3 T2 T3 After Performing L-R Rotations on Node c: Height of Subtree Being Fixed Remains h + 3 h + 3 c h + 2 b h + 2 a h + 1 h h + 1 h + 1 T1 T2 T3 T4 34 of 39

35 Restoring Balance from Deletions Before Deletion from T4 After Deletion from T4 h + 3 a h + 3 a h + 2 b h + 1 h + 2 b h h + 1 c h T4 h + 1 c h T4 h h T3 h h T3 T1 T2 T1 T2 After Performing Right Rotation on Node b: Height of Subtree Being Fixed Reduces its Height by 1! h + 2 b h + 1 c h + 1 a h h h h T1 T2 T3 T4 35 of 39

36 Restoring Balance: Insertions vs. Deletions Each rotation involves only primitive assignments to re-assign parent-child references ) O(1) running time for each tree rotation After each insertion, a trinode restructuring step can restore the height of the subtree rooted at the first-encountered unbalanced node. ) O(1) rotations suffices to restore the balance of tree After each deletion, a trinode restructuring step may reduce the height of the subtree rooted at the first-encountered unbalanced node. ) May take O(log n) rotations to restore the balance of tree 36 of 39

37 Index (1) Binary Search Tree: Definition BST: Sorting Property BST: Internal Nodes vs. External Nodes BST Operation: Searching (1) BST Operation: Searching (2) BST Operation: Searching (3) BST Operation: RT of Searching (1) BST Operation: RT of Searching (2) BST Operation: Insertion (1) BST Operation: Insertion (2.1) BST Operation: Insertion (2.2) BST Operation: Deletion (1) BST Operation: Deletion (2.1) BST Operation: Deletion (2.2) 37 of 39

38 Index (2) BST Operation: Deletion (3.1) BST Operation: Deletion (3.2) Homework (1) Balanced Binary Search Trees: Motivation Balanced Binary Search Trees: Definition Fixing Unbalanced BST: Rotations After Insertions: Trinode Restructuring via Rotation(s) Trinode Restructuring: Single, Left Rotation Exercise: Left Rotation Trinode Restructuring: Single, Right Rotation Exercise: Right Rotation Trinode Restructuring: Double, R-L Rotations Exercise: R-L Rotations 38 of 39

39 Index (3) Trinode Restructuring: Double, L-R Rotations Exercise: L-R Rotations After Deletions: Continuous Trinode Restructuring Exercise: Single Trinode Restructuring Step Exercise: Multiple Trinode Restructuring Steps Restoring Balance from Insertions Restoring Balance from Deletions Restoring Balance: Insertions vs. Deletions 39 of 39

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 3/20/14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Data Structure [Question Bank]

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:

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

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

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

Binary search algorithm

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

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

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

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

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

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

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

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

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

Laboratory Module 6 Red-Black Trees

Laboratory Module 6 Red-Black Trees Laboratory Module 6 Red-Black Trees Purpose: understand the notion of red-black trees to build, in C, a red-black tree 1 Red-Black Trees 1.1 General Presentation A red-black tree is a binary search tree

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

Optimal Binary Search Trees Meet Object Oriented Programming

Optimal Binary Search Trees Meet Object Oriented Programming Optimal Binary Search Trees Meet Object Oriented Programming Stuart Hansen and Lester I. McCann Computer Science Department University of Wisconsin Parkside Kenosha, WI 53141 {hansen,mccann}@cs.uwp.edu

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

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

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

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

Binary Search Tree. 6.006 Intro to Algorithms Recitation 03 February 9, 2011 Binary Search Tree A binary search tree is a data structure that allows for key lookup, insertion, and deletion. It is a binary tree, meaning every node of the tree has at most two child nodes, a left

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

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

Analysis of a Search Algorithm

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

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

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

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

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

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

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

Classification/Decision Trees (II)

Classification/Decision Trees (II) Classification/Decision Trees (II) Department of Statistics The Pennsylvania State University Email: jiali@stat.psu.edu Right Sized Trees Let the expected misclassification rate of a tree T be R (T ).

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

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

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

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

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

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

Unordered Linked Lists

Unordered Linked Lists Unordered Linked Lists Derive class unorderedlinkedlist from the abstract class linkedlisttype Implement the operations search, insertfirst, insertlast, deletenode See code on page 292 Defines an unordered

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

- 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

Persistent Data Structures and Planar Point Location

Persistent Data Structures and Planar Point Location Persistent Data Structures and Planar Point Location Inge Li Gørtz Persistent Data Structures Ephemeral Partial persistence Full persistence Confluent persistence V1 V1 V1 V1 V2 q ue V2 V2 V5 V2 V4 V4

More information

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

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

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

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

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

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

More information

1/1 7/4 2/2 12/7 10/30 12/25

1/1 7/4 2/2 12/7 10/30 12/25 Binary Heaps A binary heap is dened to be a binary tree with a key in each node such that: 1. All leaves are on, at most, two adjacent levels. 2. All leaves on the lowest level occur to the left, and all

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

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number

More information

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

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

More information

Data Structures and Algorithms Written Examination

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

More information

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

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node Binary Search Trees \I think that I shall never see a poem as lovely as a tree Poem's are wrote by fools like me but only G-d can make atree \ {Joyce Kilmer Binary search trees provide a data structure

More information

M(0) = 1 M(1) = 2 M(h) = M(h 1) + M(h 2) + 1 (h > 1)

M(0) = 1 M(1) = 2 M(h) = M(h 1) + M(h 2) + 1 (h > 1) Insertion and Deletion in VL Trees Submitted in Partial Fulfillment of te Requirements for Dr. Eric Kaltofen s 66621: nalysis of lgoritms by Robert McCloskey December 14, 1984 1 ackground ccording to Knut

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

Introduction to data structures

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

More information

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures Using C++ 2E. Chapter 5 Linked Lists Data Structures Using C++ 2E Chapter 5 Linked Lists Doubly Linked Lists Traversed in either direction Typical operations Initialize the list Destroy the list Determine if list empty Search list for a given

More information

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

Any two nodes which are connected by an edge in a graph are called adjacent node. . iscuss following. Graph graph G consist of a non empty set V called the set of nodes (points, vertices) of the graph, a set which is the set of edges and a mapping from the set of edges to a set of pairs

More information

Introduction to Data Structures and Algorithms

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

More information

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

schema binary search tree schema binary search trees data structures and algorithms 2015 09 21 lecture 7 AVL-trees material scema binary searc trees data structures and algoritms 05 0 lecture 7 VL-trees material scema binary searc tree binary tree: linked data structure wit nodes containing binary searc trees VL-trees material

More information

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques Searching (Unit 6) Outline Introduction Linear Search Ordered linear search Unordered linear search Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

More information

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs Binary Search Trees basic implementations randomized BSTs deletion in BSTs eferences: Algorithms in Java, Chapter 12 Intro to Programming, Section 4.4 http://www.cs.princeton.edu/introalgsds/43bst 1 Elementary

More information

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April 10 17-1

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April 10 17-1 inary Trees Wellesley ollege S230 Lecture 17 Thursday, pril 5 Handout #28 PS4 due 1:30pm Tuesday, pril 10 17-1 Motivation: Inefficiency of Linear Structures Up to this point our focus has been linear structures:

More information

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

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

More information

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

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees Binary Search Trees Last lecture: Tree terminology Kinds of binary trees Size and depth of trees This time: binary search tree ADT Java implementation Keys and records So far most examples assumed that

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

Alex. Adam Agnes Allen Arthur

Alex. Adam Agnes Allen Arthur Worksheet 29:Solution: Binary Search Trees In Preparation: Read Chapter 8 to learn more about the Bag data type, and chapter 10 to learn more about the basic features of trees. If you have not done so

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