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

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

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

Transcription

1 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 pointer is NIL. K L Binary Trees Binary tree is a root left subtree (maybe empty) right subtree (maybe empty) Properties max # of leaves: max # of nodes: average height for N nodes: Representation: D B A E G C F H 1.Is this binary tree complete? Why not? (C has just one child, right side is much deeper than left). What s the maximum # of leaves a binary tree of depth d/height h can have? d 3.What s the max # of nodes a binary tree of depth d/height h can have? d Minimum? d ; d 4.We won t go into this, but if you take N nodes and assume all distinct trees of the nodes are equally likely, you get an average depth/height of SQRT(N). D B I A E G C F J H left pointer Data right pointer I J Is that bigger or smaller than log n? Bigger, so it s not good enough! We will see we need to impose structure to get the bounds we want

2 Representation Implementations of Binary Trees A representation of a binary tree data node is similar to a doubly linked list in that it has two pointers. A left right pointer pointer B A C The graphic of a node shows a data area, and a left and right pointer. Some trees only store data in the leaf nodes. B left right pointer pointer C left right pointer pointer D E F D left right pointer pointer E left right pointer pointer F left right pointer pointer In this tree of five nodes, the following properties can be seen: Here is an example of a union binary tree implementation where the internal nodes are of a different construct than the leaf nodes. The internal nodes contain pointers, and a data element representing a mathematical operator. The leaf nodes are data nodes only, and contain the operands. there are nodes 3 internal leaf nodes there are a total of pointers 6 of the pointers are null pointers 4 * x * ( * x + a) - c For the above equation: Post-order traversal of the above tree will regenerate the post-fix notation of the equation. Pre-order traversal regenerates prefix notation In-order traversal regenerates the form depicted to the left of the tree

3 Binary Search Tree Dictionary Data Structure Every BST satisfies the BST property Binary tree property each node has children result: storage is small operations are simple average depth is small normally Search tree property all keys in left subtree smaller than root s key all keys in right subtree larger than root s key result: easy to find any given key i. If y is in the LEFT subtree of x then key [ y ] < key [x ] ii. if y is in the RIGHT subtree of x then key [ y ] > key [ x ]. This property ensures that data in a B-S-T are stored in such a way as to satisfy the B-S-T property. Examples Examples NEITHER IS A BINARY SEARCH TREE 1 NEITHER IS A BINARY SEARCH TREE 1

4 Binary Search Trees ( BST) The defining property of BST is that each node has left and right links pointing to another binary search tree or to external nodes ( which have no non-nil links). Compare key values in internal nodes with the search key and use result to control progress of the search. Insert ASERCGHIN into an initially empty BST Notice that each insertion follows a search miss at the bottom of the tree. Insertion is as easy to implement as Search. Run Times of BST algorithms depend on the shape of the tree Best Case: Tree is perfectly balanced ~ log n nodes from root to the bottom Worst Case : Could be n nodes from root to the bottom. Searches on BST : On average require about log n comparisons on a tree with n nodes. Proof: # of compares = 1 + distance of node to the root Adding over all nodes gives internal path length If C n = average internal path length of BST with n nodes, Sorting If look at BST in proper manner, it represents a sorted file i.e. read the tree from left to right, ignoring the level (height) of the nodes in the tree i.e. an In Order traversal of the tree ( left subtree => root => right subtree ) BST's are a dual model to quicksort : Node at root corresponds to the pivot element Traversals Many algorithms involve walking through a tree, and performing some computation at each node Walking through a tree is called a traversal Common kinds of traversal Pre-order Post-order Level-order Insert { A S E R A H C G I E N X M P E A L } into an empty BST

5 Consider the following pseudocode : InOrder_Traversal ( x) If x = Nil Then InOrder_Traversal( Left [ x ]) Print key [x ] InOrder_Traversal ( Right [ x ] ) In Order Listing What is printed if this is applied to the B-S-T in the graph? How long does the tree traversal take? 1 0 O ( n ) - time for a tree with n items Visiting each node once and printing the value An InOrder_Traversal prints the node values in monotonically increasing order. In order listing: Operations on a BST Find D in the preceding B-S-T : Searching : = NIL

6 What happens if search for C? Maximum and Minimum : Very straightforward from the structure of the B-S-T Tree_ Minimum ( x) While left [ x ] not null Do x left [ x ] Return x 1 Tree_ Maximum( x) While right [ x ] not null Do x right [ x ] Return x 1 0 How long does each procedure take to run? O ( h ) where h = height of the tree. Just traveling down the tree one level at a time. Successor and Predecessor : If all keys are distinct, then the successor of a node x is the node with the smallest key greater than the key [x] If all keys are distinct, then the predecessor of a node x is the node with the largest key less than the key [x] Successor and Predecessor : The structure of the B-S-T allows determination of the successor without any comparison of keys : Tree_Successor (x) 1. If right [ x ] not null. Then return Tree_Minimum ( right [ x ]) 3. y p [ x ] 4. while y not null and x = right [ y ] do x y. y p [ y ] 6. return y What is happening in the situation when the key has no right subtree? In this case, if x has a successor then it is the lowest ancestor of x whose left child is also an ancestor of x. to find the successor, in this case, move up the tree from x until find a node that is the left child of its parent.

7 1. Find successor of 1 right [ x ] is not null so execute a call to Extract_Min on right [ 1 ] points to 18 and returns 1 = x.. Find successor to 13 i. y gets p[x] and points to node ii. y not null and x = right [ y ] iii. x set to point to node ; y set to point to 6 node iv. y not null and x = right [ y ] v. x3 set to point to 6 node ; y3 set to point to 1 node vi. y3 is not null and x3 = right [y3 ] vii. return y3 as long as move left up the subtree, we visit smaller keys our successor is the node of which we are the predecessor What is the running time? In either case follow path up the tree or down the tree (and only one of these paths) O ( h ) run time. What would code look like for the predecessor of x? Theorem : The dynamic set operations : Search, Minimum, Maximum, Successor and Predecessor can run in O ( h ) time on a B-S-T of height h. Idea behind Insertion 1. goal of the algorithm is to find a place to insert a new node. similar to the search code but with a few twists 3. as you go keep two pointers : one to where you are ; one to where you have been ( to allow for a quick connection) 4. trace a path from the root to a null this locates where the node will go. what if there is no tree? set this new node to be the root What if the input string is : B D F H J L and no tree exists at first insert?

8 Insertion and Deletion These operations cause the dynamic set represented by the B-S-T to change. Changes are made so that the B-S-T property is preserved. Insertion : Deleting a Node 1. If the node is an external node, simply replace it with a NIL value. If it is an internal node, then it has 1 or children that cannot simply be orphaned they need to be reattached to the BST tree while preserving the BST property. Case 1 : node has one child : Replace the node with the value ( key) of its child Begin at the root and trace a path downward in the tree - x traces the path ; y retains the pointer to the parent of x -directional choices are determined by the compare : key [x] vs key [z] until x is set to nil - nil occupies the location where z is to be stored - Running time : as with others, this is O ( h ) Case : node has two children : Find the successor ( or predecessor ) of the node to be removed replace the node with the value ( key) of the successor ( or predecessor )move to earlier cases to resolve any created orphans. Deletion This operation is a bit more complicated depends basically on whether the node to be deleted, z, has: - No children In this case, remove the node by changing its parent, p[z], by replacing z with NIL as its child - A single child Remove the child and create a spliced link from the parent, p [ z ] to the child of z - Two children A bit more complicated find the successor y that has no left child and replace the contents of z with the contents of y. In this case it s successor is the minimum in its right subtree, and so, that successor has no left children Tree_Delete ( T, z )

9 Theorem : The dynamic set operations Insert and Delete can run in O( h ) time, in a binary search tree of height h. Note : h not n Sorting : Sort ( A ) for i 1 to n do Tree_Insert ( A [ i ] ) InOrder_Traversal (root) What should you expect for a lower bound on the run time? ## Ω ( n lg n ) ### Why? - Is this a comparison based sort? Average Case Analysis ( same as Quicksort ) The algorithm is a quicksort in which the partitioning process maintains the order of the elements in each partition. Consider : given : In turn everything is compared to 3 then to 1 or 8, etc. -order is different than quicksort, concept same : namely, at each level n compares, depth ~ lg n Ω ( n lg n ) running time. For a priority queue to extract the minimum : Extract_Min ( x) - returns a pointer to the Min key while left(x) = null do x left [ x ] return x *** examine the first tree ( F B ) and see what happens.

10 Deletion Lazy Deletion 1 0 Instead of physically deleting nodes, just mark them as deleted + Simpler + some adds just flip deleted flag + physical deletions done in batches + extra memory for deleted flag many lazy deletions slow finds some operations may have to be modified (e.g., min and max) Why might deletion be harder than insertion? 1 Lazy Deletion Deletion - Leaf Case Delete(1) Delete(1) Delete() 1 Delete(1) 1 Find() 0 0 Find(16) 1 1 Insert() Find(1)

11 Deletion - One Child Case Deletion - Two Child Case Delete(1) Delete() replace node with value guaranteed to be between the left and right subtrees: the successor Could we have used the predecessor instead? Deletion - Two Child Case Delete() always easy to delete the successor always has either 0 or 1 children! 0 Delete Code void delete(comparable x, Node *& p) { Node * q; if (p!= NULL) { if (p->key < x) delete(x, p->right); else if (p->key > x) delete(x, p- >left); else { /* p->key == x */ if (p->left == NULL) p = p->right; else if (p->right == NULL) p = p- >left; else { q = successor(p); p->key = q->key; delete(q->key, p->right); } } } }

12 Beauty is Only Θ(log n) Deep Balance Binary Search Trees are fast if they re shallow: e.g.: perfectly complete e.g.: perfectly complete except the fringe (leafs) any other good cases? Balance measure : height(left subtree) - height(right subtree) t zero everywhere perfectly balanced small everywhere balanced enough What matters here? Problems occur when one branch is much longer than the other! Balance between -1 and 1 everywhere maximum height of 1.44 log n

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

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

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

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

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

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

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

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

Multi-Way Search Trees (B Trees)

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

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

Symbol Tables. IE 496 Lecture 13

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

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

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

12 Abstract Data Types

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

More information

CSCI Trees. Mark Redekopp David Kempe

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

More information

Trees & Binary Search Trees

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

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

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

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

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,

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

Balanced Binary Search Tree

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

More information

Binary Search Trees. parent. right. left

Binary Search Trees. parent. right. left Binary Search Trees Binary search trees provide a data structure which efficiently supports all six dictionary operations. A binary tree is a rooted tree where each node contains at most two children.

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

Trees. Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees?

Trees. Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees? Section 11.1 Trees Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees? Solution: G 1 and G 2 are trees both are connected and have no simple

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

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

COT5405 Analysis of Algorithms Homework 3 Solutions

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

More information

1 2-3 Trees: The Basics

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

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

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

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

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

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

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

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

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());

More information

CSE 5311 Homework 2 Solution

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

More information

UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK PSD2023 ALGORITHM & DATA STRUCTURE

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

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

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

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

Lecture 2 February 12, 2003

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.

More information

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

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:

More information

Trees. Tree Definitions: Let T = (V, E, r) be a tree. The size of a tree denotes the number of nodes of the tree.

Trees. Tree Definitions: Let T = (V, E, r) be a tree. The size of a tree denotes the number of nodes of the tree. Trees After lists (including stacks and queues) and hash tables, trees represent one of the most widely used data structures. On one hand trees can be used to represent objects that are recursive in nature

More information

CS 3410 Ch 19 Binary Search Trees

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

More information

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

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

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

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

Balanced Trees Part One

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 /

More information

Trees LOGO STYLE GUIDE Schools within the University 1 19

Trees LOGO STYLE GUIDE Schools within the University 1 19 Trees 1 Example Tree 2 A Generic Tree 3 Tree ADT n Tree definition q A tree is a set of nodes which may be empty q If not empty, then there is a distinguished node r, called root and zero or more non-empty

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

Balanced search trees

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

More information

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

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

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

Topic 15. The Binary Search Tree ADT

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

More information

Labworks 1: Binary Search Trees and Red-Black trees

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

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

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

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

CMPS101: Homework #4 Solutions

CMPS101: Homework #4 Solutions CMPS101: Homework #4 s TA: Krishna (vrk@soe.ucsc.edu) Due Date: March 5, 2013 12.1-5 Argue that since sorting n elements takes (nlgn) time in the worst case in the comparison model, any comparison-based

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

Binary Search Trees. Yes! So, a binary tree T is a binary search tree if T is a single node.

Binary Search Trees. Yes! So, a binary tree T is a binary search tree if T is a single node. Which are Binary Search Trees (BST)? Binary Search Trees So, a binary tree T is a binary search tree if T is a single node. Yes! No! All the values on the left subtree must be less than the value at the

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

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

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

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,

More information

Data Structures. Level 6 C30151. www.fetac.ie. Module Descriptor

Data Structures. Level 6 C30151. www.fetac.ie. Module Descriptor The Further Education and Training Awards Council (FETAC) was set up as a statutory body on 11 June 2001 by the Minister for Education and Science. Under the Qualifications (Education & Training) Act,

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

AVL Trees. CSE 373 Data Structures

AVL Trees. CSE 373 Data Structures AVL Trees CSE 373 Data Structures Readings Reading Chapter 1 Section 1.2 AVL Trees 2 Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is = log N for a binary

More information

Week 8: Trees. Readings. Tree. Relationship. 9 October internal nodes. root. leaves

Week 8: Trees. Readings. Tree. Relationship. 9 October internal nodes. root. leaves Week : Trees nus.soc.cs0b.week Readings Required [Weiss] ch.. [Weiss] ch.. [Weiss] ch.. Exercises [Weiss].,.,.,. [Weiss].,.. Fun http://www.seanet.com/users/arsen/avltree.html nus.soc.cs0b.week Tree root

More information

Sidebar: Data Structures

Sidebar: Data Structures Sidebar: Data Structures A data structure is a collection of algorithms for storing and retrieving information. The operations that store information are called updates, and the operations that retrieve

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

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets We ve been talking a lot about efficiency in computing and run time. But thus far mostly ignoring data

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

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

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

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

Merge Sort. 2004 Goodrich, Tamassia. Merge Sort 1

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

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

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

CS271: Data Structures. Binary Search Trees

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

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

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

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

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

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

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

Universidad Carlos III de Madrid

Universidad Carlos III de Madrid 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

More information

Multiway Search Tree (MST)

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

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

Overview of Data Structures

Overview of Data Structures UNIT 3 Concrete Data Types Classification of Data Structures Concrete vs. Abstract Data Structures Most Important Concrete Data Structures Arrays Records Linked Lists Binary Trees Overview of Data Structures

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

The relationship of a trees to a graph is very important in solving many problems in Maths and Computer Science

The relationship of a trees to a graph is very important in solving many problems in Maths and Computer Science Trees Mathematically speaking trees are a special class of a graph. The relationship of a trees to a graph is very important in solving many problems in Maths and Computer Science However, in computer

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

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

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

Introduction to Data Structures and Algorithms

Introduction to Data Structures and Algorithms Introduction to Data Structures and Algorithms Chapter: Elementary Data Structures(1) Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen Overview on simple data structures

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

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

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

CE204 Data Structures and Algorithms Part 3

CE204 Data Structures and Algorithms Part 3 CE204 Data Structures and Algorithms Part 3 23/01/2016 CE204 Part 3 1 Trees The ADTs encountered so far have been linear; list and array implementations have been adequate. We now consider a nonlinear

More information