Binary Search Trees CMPSC 122

Size: px
Start display at page:

Download "Binary Search Trees CMPSC 122"

Transcription

1 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 in 360. Starting in Spring 2014, I've split the introduction to trees in 360 into two packets: one that encompasses all we do here and a second on the deeper mathematical analysis, namely a proof by strong induction of an important theorem relating the height and number of terminal vertices. If you are not concurrently taking both courses with me, but take 360 with me later, check in with me about potentially being excused from a lecture that will be review for you there. I. Motivation We've learned about various structures in which to store data arrays, lists, stacks, queues and each has something about it that makes it unique. What often motivates the choice of structure is what we want to do with it, or how we want to get information out of it. All of those other structures were linear structures. We can use the idea of binary trees to store data in a way that allows branching. Let's do an activity. You'll give me some numbers, and I'll put them into a binary tree in a particular way. As we go, write down the list of numbers in order and the tree. See if you can figure out what I'm doing. List of numbers: Resulting tree: Page 1 of 7 Prepared by D. Hogan for PSU CMPSC 360 and CMPSC 122

2 II. Binary Search Trees, Defined The kind of tree we're working with is something called a binary search tree, sometimes abbreviated BST. For a binary tree to be a binary search tree, it must satisfy the binary search tree property. That is, for each node n, n's left child must be less than n. More formally n's right child must be greater than n. More formally In this definition, we work under the assumption that all keys in a BST are unique. (This isn't a stretch, but if we wanted to allow non-unique keys, there are few different strategies we could employ for "same" keys.) Now then, it's worth noting how BSTs can be used. While we could certainly use a BST to store a list of numbers, it's really the meaning of those numbers that makes a BST useful. We really want to use a BST to store records. But, in practice, we don't really store an entire record in a node of a BST; we instead store some key to the record (think primary keys in database tables as we'll see in CMPSC 221). So, we store keys to records in a tree and use the structure of a binary tree to locate a record easily. That's why it's called a binary search tree. III. Searching A BST Question: In the tree we drew above, how would go about searching for the key 50 systematically, given that the tree must follow the BST property? Question: How would we determine that a key isn't found in a BST? So, let's generalize and write down pseudocode for an algorithm to search for a node in a BST. It should take as an input a pointer to the tree's root and a search key. It should return a pointer to a node containing the search key, or, in the case of failure, NIL. Page 2 of 7 Prepared by D. Hogan for PSU CMPSC 360 and CMPSC 122

3 Problem: What is the precondition for the above algorithm? IV. An Algorithm for Insertion into a BST To build a binary search tree from a set of input numbers: 1. Make the first input the root of the BST. 2. For each remaining input, recursively compare the input to the root of the tree. a. If the input is less than the root, it becomes the left child of the root (or, recursively, it goes into the left subtree.) b. If the input is greater than the root, it becomes the right child of the root (or, recursively, it goes into the right subtree.) Example 1: Build a BST from the following lists: a. 6, 4, 7 b. 6, 4, 7, 2, 5, 9 Problem: a. Build a BST from these inputs: 10, 20, 30, 40, 5, 8, 50, 60, 70, 15, 80 b. Comment on the shape of the BST. Page 3 of 7 Prepared by D. Hogan for PSU CMPSC 360 and CMPSC 122

4 Problem: Write a recursive algorithm to insert a key into a BST, given that key and a pointer to the BST's root. Page 4 of 7 Prepared by D. Hogan for PSU CMPSC 360 and CMPSC 122

5 V. Tree Traversal Once a tree is in place, we can traverse or walk the tree to list the elements of the tree. There are three kinds of traversals. The first is called an inorder traversal of the tree. Algorithm: Inorder Traversal(Tree T) 1. Do an Inorder Traversal on the left subtree of T 2. Print the root of T 3. Do an Inorder Traversal on the right subtree of T Notice the recursive nature of this procedure. Example: Let's go back and do an inorder traversal on a BST from the first page. The other two kinds of traversals are called preorder and postorder. In short, here's how all three go: Inorder Traversal: left, root, right Preorder Traversal: root, left, right Postorder Traversal: left, right, root Example: Let's do a preorder traversal on a BST from the first page. Example: Let's do a postorder traversal on a BST from the first page. Page 5 of 7 Prepared by D. Hogan for PSU CMPSC 360 and CMPSC 122

6 VI. Tree Sort Question: Suppose we had a list of numbers we wanted to sort. How could we use a BST to do this? Question: What advantages does this method have? VII. Performance of BST Algorithms Problem: Build a BST from these values: 50, 30, 20, 40, 70, 80, 60. Trace a search for 50. How many comparisons are necessary? Trace a search for 20. How many comparisons are necessary? Trace a search for 45. How many comparisons are necessary? Can we call any of these best or worst-case scenarios? Page 6 of 7 Prepared by D. Hogan for PSU CMPSC 360 and CMPSC 122

7 Let's now consider a tree that's slightly larger, one where each of the leaves of the last tree had 2 children. Let's again extend the last tree in the same way and get a maximum number of comparisons. Let's generalize the worst-case number of comparisons for the special case of a binary search tree where each node has exactly 2 children: Number of nodes (n) Worst-Case Number of Comparisons Question: Does this count as a worst-case running time for a search in a BST? Why? If not, what would an accurate worst case be? Searching wasn't the only algorithm we looked at. Let's consider the performance of others: Insertion Traversal Finally, it would seem, then, that having perfectly balanced binary trees yields optimal performance. So, it would behoove us to have a way of balancing BSTs. We'll leave that for the middle of 465 (and, in the meantime, do some other things with trees in 360, as well as graphs, of which trees are just a special case). Page 7 of 7 Prepared by D. Hogan for PSU CMPSC 360 and CMPSC 122

Expression Trees, Binary Search Trees, and Tree Traversal Advanced Computer Programming

Expression Trees, Binary Search Trees, and Tree Traversal Advanced Computer Programming Expression Trees, Binary Search Trees, and Tree Traversal Advanced Computer Programming We now look more specifically at two special kinds of binary trees. These two types of trees are very important in

More information

Computer Sciences Department 1

Computer Sciences Department 1 1 Binary Search Trees Chapter 12 3 Objectives The idea of Binary Search Tree Operations of Binary Search Tree The run time of Binary Search Tree Analysis of Binary Search Tree 4 Search trees -1 Search

More information

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

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

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

How do hashes work? Sunday, December 05, :28 PM

How do hashes work? Sunday, December 05, :28 PM Trees Page 1 How do hashes work? 5:28 PM How do Perl hashes work? So far, we've found out that Perl hashes can do most anything. But how do they work? Key to how hashes work: associating a key with a value.

More information

General Trees and Conversion to Binary Trees

General Trees and Conversion to Binary Trees General Trees and Conversion to Binary Trees General trees are those in which the number of subtrees for any node is not required to be,, or. The tree may be highly structured and therefore have 3 subtrees

More information

Lecture: #9-10. Binary Trees

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

More information

INF 2220: algorithms and data structures Series 1

INF 2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk D. Karabeg, I. Yu INF 2220: algorithms and data structures Series 1 Høst 2014 19. 08. 2014 Topic: Function growth & estimation of running time, trees (Exercises

More information

Fundamentals of Data Structures Trees Example test questions for the course

Fundamentals of Data Structures Trees Example test questions for the course 2008 November Tree Questions Page 1 of 6 Fundamentals of Data Structures Trees Example test questions for the course These questions or similar questions have been used in tests in previous years. The

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

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

Linked Lists CMPSC 122

Linked Lists CMPSC 122 Linked Lists CMPSC 122 I. Motivating Example We'll dive right in with an example linked list. Our list will hold the values 1, 2, and 3. Linked lists can easily grow and shrink. In that spirit, let's Add

More information

B-Trees. CMSC 420: Lecture 9

B-Trees. CMSC 420: Lecture 9 B-Trees CMSC 420: Lecture 9 Another way to achieve balance Height of a perfect binary tree of n nodes is O(log n). Idea: Force the tree to be perfect. - Problem: can t have an arbitrary # of nodes. - Perfect

More information

Binary Search Tree (Part 1)

Binary Search Tree (Part 1) Binary Search Tree (Part 1) Yufei Tao ITEE University of Queensland This and the next lecture will be devoted to the most important data structure of this course: the binary search tree (BST). This is

More information

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

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

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

Problem Set 4 Solutions

Problem Set 4 Solutions Introduction to Algorithms October 23, 2002 Massachusetts Institute of Technology 6046J/18410J Professors Erik Demaine and Shafi Goldwasser Handout 18 Problem Set 4 Solutions (Exercises were not to be

More information

R13. II B. Tech I Semester Supplementary Examinations, June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

R13. II B. Tech I Semester Supplementary Examinations, June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) SET - 1 II B. Tech I Semester Supplementary Examinations, June - 2015 DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) Time: 3 hours Max. Marks: 70 Note: 1. Question Paper consists of two parts (Part-A

More information

Binary Trees CMPSC 122

Binary Trees CMPSC 122 Binary Trees CMPSC 122 I. From Lists to Trees Recall linked lists. Each node but the head and the tail had a and a. A tree is a similar structure, except that instead of one successor, each node in a tree

More information

Topics. DSAII: Binary Search Trees. Elements of a Dynamic Set. Dynamic Sets. Dynamic Sets Binary Search Trees:

Topics. DSAII: Binary Search Trees. Elements of a Dynamic Set. Dynamic Sets. Dynamic Sets Binary Search Trees: Topics DSAII: Binary Search Trees Sattiraju Prabhakar Source: Data Structures and Algorithms Cormen et al Wichita State University Dynamic Sets Binary Search Trees: Organization Visiting all elements Querying

More information

Binary Search Trees CMPSC 465 Related to CLRS Chapter 12 and Epp Section 10.6

Binary Search Trees CMPSC 465 Related to CLRS Chapter 12 and Epp Section 10.6 Binary Search Trees CMPSC 465 Related to CLRS Chapter 12 and Epp Section 10.6 I. Review & Extension Recall that we defined the term dictionary last chapter. Search trees fit the definition of dictionary;

More information

October 2004 John Edgar 2

October 2004 John Edgar 2 Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm October 2004

More information

Graph Traversals: Breadth-First Search and Depth-First Search CMPSC 465 CLRS Sections 22.2 and 22.3

Graph Traversals: Breadth-First Search and Depth-First Search CMPSC 465 CLRS Sections 22.2 and 22.3 Graph Traversals: Breadth-First Search and Depth-First Search CMPSC 465 CLRS Sections 22.2 and 22.3 I. The Idea Recall how in rooted trees had traversals inorder, preorder, and postorder to visit all of

More information

CSE3358 Problem Set 7 Solution

CSE3358 Problem Set 7 Solution CSE3358 Problem Set 7 Solution Problem : A remarkable property of BST (0 points) Professor R. E. Mark Cable thinks he has discovered a remarkable property of binary search trees. Suppose that the search

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

Lecture Notes 15 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture Notes 15 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 15 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 10.1-10.2 Introduction to trees The data structures we have seen so far

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

CS 2336 Discrete Mathematics

CS 2336 Discrete Mathematics CS 2336 Discrete Mathematics Lecture 16 Trees: Introduction 1 What is a Tree? Rooted Trees Properties of Trees Decision Trees Outline 2 What is a Tree? A path or a circuit is simple if it does not contain

More information

Red-Black Trees CMPSC 465 Related to CLRS Chapter 13

Red-Black Trees CMPSC 465 Related to CLRS Chapter 13 Red-Black Trees CMPSC 465 Related to CLRS Chapter 13 I. The Big Picture We ve studied binary search trees, but noted that their shortcoming is they may become unbalanced. But, if we can get a balanced

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

Standard Version of Starting Out with C++, 4th Edition. Binary Trees. Copyright 2003 Scott/Jones Publishing

Standard Version of Starting Out with C++, 4th Edition. Binary Trees. Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Binary Trees Copyright 2003 Scott/Jones Publishing Topics Definition and Application of Binary Trees Binary Search Tree Operations Template Considerations

More information

Decrease-and-Conquer. Dr. Yingwu Zhu

Decrease-and-Conquer. Dr. Yingwu Zhu Decrease-and-Conquer Dr. Yingwu Zhu Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend solution of smaller instance to obtain solution

More information

Lecture 4: Binary Trees

Lecture 4: Binary Trees ECE4050/CSC5050 Algorithms and Data Structures Lecture 4: Binary Trees 1 Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together

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

Nearly Complete Binary Trees and Heaps

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

More information

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: Binary trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: Binary trees DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: Binary trees Summary of the previous lecture Dynamic data structures Lists Stack Queue Deque Array based implementation Linked list based implementation

More information

Binary Search Trees (11.1) Example Application

Binary Search Trees (11.1) Example Application Binary Search Trees (11.1) EECS 011 7 February 01 1 Example Application Application: database of employee records search keys: social insurance numbers add employee remove employee search employee 1 Data

More information

General trees Terminology Representation of trees Tree traversal-

General trees Terminology Representation of trees Tree traversal- UNIT IV TREES Syllabus General trees Terminology Representation of trees Tree traversal- Binary tree Representation Expression tree Binary tree traversal - Threaded Binary Tree - Binary Search Tree Construction

More information

Definition A tree is either empty or consists of one node called the root and zero or more subtrees.

Definition A tree is either empty or consists of one node called the root and zero or more subtrees. Binary Trees Introduction We extend the concept of linked data structures to structure containing nodes with more than one self-referenced field. Definition A tree is either empty or consists of one node

More information

Trees and Binary Search Trees Chapters 18 and 19. Dynamic data structures. Tree: example. Tree: non-recursive definition.

Trees and Binary Search Trees Chapters 18 and 19. Dynamic data structures. Tree: example. Tree: non-recursive definition. Trees and Binary Search Trees Chapters 18 and 19 Dynamic data structures Linked Lists CS 3358 Summer I 2012 Jill Seaman - dynamic structure, grows and shrinks with data - most operations are linear time

More information

Binary Search Trees (BST)

Binary Search Trees (BST) Binary Search Trees (BST) 1. Hierarchical data structure with a single pointer to node 2. Each node has at most two child nodes (a left and a right child) 3. Nodes are organized by the Binary Search property:

More information

! Linked Lists. ! Can we make a simple data structure that. ! Trees. ! edges are directed down (source is higher)

! Linked Lists. ! Can we make a simple data structure that. ! Trees. ! edges are directed down (source is higher) Trees and Binary Search Trees Chapters 18 and 19 Dynamic data structures! Linked Lists CS 3358 Summer II 2013 Jill Seaman Sections 18.1-4, 19.1-3 - dynamic structure, grows and shrinks with data - most

More information

Can we do faster? What is the theoretical best we can do?

Can we do faster? What is the theoretical best we can do? Lecture Notes Kenrick Mock CS411 Non-Comparison Based Sorting How fast can we sort? 2 Insertion-Sort O( n ) Merge-Sort, Quicksort (expected), Heapsort :Θ( n lg n ) Can we do faster? What is the theoretical

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

Chapter 6. Binary Trees. Data Structures and Algorithms in Java

Chapter 6. Binary Trees. Data Structures and Algorithms in Java Chapter 6 Binary Trees Data Structures and Algorithms in Java Objectives Discuss the following topics: Trees, Binary Trees, and Binary Search Trees Implementing Binary Trees Searching a Binary Search Tree

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

CMSC 206. Introduction to Trees

CMSC 206. Introduction to Trees CMSC 206 Introduction to Trees 1 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 subtrees

More information

Tree is a collection of nodes in which there is a root node and all other nodes are recursively children

Tree is a collection of nodes in which there is a root node and all other nodes are recursively children Tree Tree is a collection of nodes in which there is a root node and all other nodes are recursively children of this root node. What does it mean is that root node has some child nodes, those child nodes

More information

CSC148H. Lecture 6. Binary Search Trees

CSC148H. Lecture 6. Binary Search Trees CSC148H Lecture 6 Binary Search Trees Motivating Binary Search Trees Last week we saw examples of where a tree is a more appropriate data structure than a linear structure. Sometimes we may use a tree

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

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 Search Trees (BST) Dr. Yingwu Zhu

Binary Search Trees (BST) Dr. Yingwu Zhu Binary Search Trees (BST) Dr. Yingwu Zhu Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, x n Assume == and < operators defined for the type Linear search

More information

A study on Binary Tree

A study on Binary Tree A study on Binary Tree Ankit Dalal1* and Ankur Atri2 1 Department of Information Technology, Computer Science and Information Technology, Dronacharya College of Engineering,Gurgaon-122001, India *E-mail:

More information

Selected Homework Solutions Unit 2 CMPSC 465

Selected Homework Solutions Unit 2 CMPSC 465 Selected Homework Solutions Unit 2 CMPSC 465 Exercise 6.1-1 Problem: What are the minimum and maximum numbers of elements in a heap of height h? Since a heap is an almost-complete binary tree (complete

More information

Design and Analysis of Algorithms Chapter 5

Design and Analysis of Algorithms Chapter 5 CSCE 310J: Data Structures & Algorithms Decrease and Conquer Dr. Steve Goddard goddard@cse.unl.edu CSCE 310J: Data Structures & Algorithms Giving credit where credit is due: Most of the lecture notes are

More information

Red-Black Trees (12.2)

Red-Black Trees (12.2) Red-Black Trees v 6 3 8 4 z 02/27/2007 1 Red-Black Trees (12.2) A red-black tree can also be defined as a binary search tree that satisfies the following properties: Every node is colored either red or

More information

This Lecture. SOFT : BST, Priority Queue, Heap. Binary Search Trees. Recall binary trees. The Binary Search Tree. The Binary Search Tree 6/4/07

This Lecture. SOFT : BST, Priority Queue, Heap. Binary Search Trees. Recall binary trees. The Binary Search Tree. The Binary Search Tree 6/4/07 SOFT1002 25: BST, Priority Queue, Heap Binary Search Trees Priority Queues This Lecture School of Information Technologies SOFT1002 The University of Sydney 1 2 Binary Search Trees Recall binary 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

Chapter 11. Trees Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11. Trees Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11 Trees 2006 Pearson Addison-Wesley. All rights reserved 11 A-1 Terminology Definition of a general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint

More information

Basic Traversal and Search Techniques

Basic Traversal and Search Techniques Basic Traversal and Search Techniques Traversal vs Search Definition 1 Traversal of a binary tree involves examining every node in the tree. Definition 2 Search involves visiting nodes in a graph in a

More information

Sorted Tables. Ordered Dictionary. Binary Search. Binary Search

Sorted Tables. Ordered Dictionary. Binary Search. Binary Search Ordered Dictionary Sorted Tables Dictionary is a set of elements, the keys, supported by the dictionary operations, such as: findelement(k) -- k position, e element insertitem(k,e) removeelement(k) Ordered

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 The program of Figs. 20.16 20.17 creates a Queue class template (Fig. 20.16) through private inheritance (line 9) of the List class template (Fig. 20.4). The Queue has member functions enqueue (lines 13

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

Lecture 9 March 4, 2010

Lecture 9 March 4, 2010 6.851: Advanced Data Structures Spring 010 Dr. André Schulz Lecture 9 March 4, 010 Scribe: Paul Christiano 1 Overview Last lecture we defined the Least Common Ancestor (LCA) and Range Min Query (RMQ) problems.

More information

Lecture 8 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson)

Lecture 8 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson) Lecture 8 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson) Trees A tree is a hierarchical data structure Each node can have several children but only

More information

In this lecture we will consider another type of ADT, called Heap, which facilitates efficient insertion and deletion of items to and from a

In this lecture we will consider another type of ADT, called Heap, which facilitates efficient insertion and deletion of items to and from a In this lecture we will consider another type of ADT, called Heap, which facilitates efficient insertion and deletion of items to and from a collection of sorted elements. Make sure you don t confuse the

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

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

Data structures & Algorithms - Multiple Choice Questions (MCQs) - Objective Set 1

Data structures & Algorithms - Multiple Choice Questions (MCQs) - Objective Set 1 Following are the multiple choice questions (MCQs) or objective questions from Data Structures and Algorithms. The questions are set from the topics such as arrays, records, pointers, linked lists, stacks,

More information

Comp 245 Data Structures. Trees

Comp 245 Data Structures. Trees Comp 245 Data Structures Trees Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one access point to the tree; it is called the root. A

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

Homework 15 Solutions

Homework 15 Solutions PROBLEM ONE (Trees) Homework 15 Solutions 1. Recall the definition of a tree: a tree is a connected, undirected graph which has no cycles. Which of the following definitions are equivalent to this definition

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

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

GOVERNMENT POLYTECHNIC HIMATNAGAR Information Technology

GOVERNMENT POLYTECHNIC HIMATNAGAR Information Technology GOVERNMENT POLYTECHNIC HIMATNAGAR Information Technology Subject:Data Structures Subject Code:3330704 Semester: III Assignment-1 1.What is Data Structure? Explain Primitives & Non-Primitive data structure

More information

Data Structures. Topic #8

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

More information

CMSC 341. Introduction to Trees. Textbook sections:

CMSC 341. Introduction to Trees. Textbook sections: CMSC 341 Introduction to Trees Textbook sections: 4.1-4.2 1 Tree ADT Tree definition A tree is a set of nodes which may be empty If not empty, then there is a distinguished node r, called root and zero

More information

CS 2321 Data Structures and Algorithms. Trees. Dr. Mahdi Khemakhem

CS 2321 Data Structures and Algorithms. Trees. Dr. Mahdi Khemakhem CS 2321 Data Structures and Algorithms Trees Dr. Mahdi Khemakhem Outlines 1. Definitions and Terminologies 2. Binary Trees 3. Binary Search Trees 4. AVL-Trees CS 2321: D.S.A (-Trees-) 2 1. Defintions and

More information

Trees. Kruskal s Algorithm... 19

Trees. Kruskal s Algorithm... 19 Kruskal s Algorithm...................................... 19 Trees Algorhyme by Radia Perlman I think that I shall neer see A graph more loely than a tree. A tree whose crucial property Is loop-free connectiity.

More information

Trees Outline Tree Structures

Trees Outline Tree Structures Trees Outline Tree Structures Tree Node Level and Path Length Binary Tree Definition Binary Tree Nodes Binary Search Trees Tree Structures Arrays, vectors, lists are linear structures, one element follows

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

2-3 Trees. Balanced Search Trees. 2-3 Trees. 2-3 Trees

2-3 Trees. Balanced Search Trees. 2-3 Trees. 2-3 Trees Balanced Search Trees The efficiency of the binary search tree implementation of the ADT table is related to the tree s height Height of a binary search tree of n items Maximum: n Minimum: log 2 (n + 1)

More information

Trees are also important in parsing (breaking a sentence into its parts), which we ll study later.

Trees are also important in parsing (breaking a sentence into its parts), which we ll study later. 9.1 9.2 This lecture is about trees, which are another common data structure. We ll be looking at binary trees, how they re represented and built. We ll also look at ordered binary trees, which are a good

More information

Indexing with Trees. Index Trees. Data Structures & File Management. Computer Science Dept Va Tech July McQuain WD

Indexing with Trees. Index Trees. Data Structures & File Management. Computer Science Dept Va Tech July McQuain WD Indexing with Trees 1 Hash tables suffer from several defects, including: - good, general purpose hash functions are very difficult to find - static table size requires costly resizing if indexed set is

More information

COURSE: B.TECH-ECE. IV Sem. Data structure Using C. b) Deletion of element in an array

COURSE: B.TECH-ECE. IV Sem. Data structure Using C. b) Deletion of element in an array COURSE: B.TECH-ECE. IV Sem Data structure Using C 1. Determine the formula to find the address location of an element in three dimensions array, suppose each element takes four bytes of space & elements

More information

ceiling function for binary search tree

ceiling function for binary search tree ceiling function for binary search tree Key ceiling(const Key& x) Returns the smallest key in the tree that is >= x Throws logic_error if all keys in the tree are < x Example ceiling(45)? ceiling(15)?

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 12 / 12 / 2012 Instructor: Michael Eckmann Today s Topics Comments/Questions? Huffman coding compression Recap of what we did this semester Michael Eckmann -

More information

2-3 trees, AA trees, B-trees

2-3 trees, AA trees, B-trees 2-3 trees, AA trees, B-trees Announcement Consultation times 10-12, room ES61! Monday 9 th May Thursday 12 th May Thursday 19 th May Monday 23 rd May No plan just come and ask questions P.S. don't forget

More information

Binary search trees (BST) Binary search trees (BST)

Binary search trees (BST) Binary search trees (BST) Tree A tree is a structure that represents a parent-child relation on a set of object. An element of a tree is called a node or vertex. The root of a tree is the unique node that does not have a parent

More information

MITOCW 4. Heaps and Heap Sort

MITOCW 4. Heaps and Heap Sort MITOCW 4. Heaps and Heap Sort The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free.

More information

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Review for Test 2 (Chapter 6-10) Chapter 6: Template functions & classes 1) What is the primary purpose of template functions? A. To allow a single function to be used with varying types of arguments B.

More information

CSE 326: Data Structures Binary Search Trees

CSE 326: Data Structures Binary Search Trees nnouncements (4/18/08) CSE 36: Data Structures Binary Search Trees HW #3 will be assigned this afternoon, due at beginning of class next Friday. Project due next Wed. night. Brian Curless Spring 08 1 Outline

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

Outline Trees CSCI Slide 2

Outline Trees CSCI Slide 2 2-3 Tree Outline Balanced Search Trees 2-3 Trees 2-3-4 Trees Slide 2 Why care about advanced implementations? Same entries, different insertion sequence: Not good! Would like to keep tree balanced. Slide

More information

Binary search trees. Bởi: Nguyễn Việt Hà truongninhthuan vuquangdung. Binary tree

Binary search trees. Bởi: Nguyễn Việt Hà truongninhthuan vuquangdung. Binary tree Binary search trees Bởi: Nguyễn Việt Hà truongninhthuan vuquangdung Introduction to binary trees Binary tree A binary search tree of size 9 and depth 3, with root 8 and leaves 1, 4, 7 and 13. In computer

More information

Topics. Linked lists Trees Tutorial Problems Design Exercises

Topics. Linked lists Trees Tutorial Problems Design Exercises Tutorial 9 Topics Linked lists Trees Tutorial Problems Design Exercises Linked lists From earlier experiences in this course, you have already worked with two of the most basic data structures, arrays

More information

Marks : 10X1. Note: Question Number 1 is compulsory.

Marks : 10X1. Note: Question Number 1 is compulsory. MODEL ANSWER B.Sc, computer Science, Integrated U.G/P.G(sixth Semester) Examination,2014 Subject: Introduction to Data-structure (PCSC-603) [Time: Three Hours] [Maximum Marks : 30] Note: Question Number

More information

Priority Queues and Heaps

Priority Queues and Heaps riority Queues and Heaps riority Queue A priority queue is a queue where each element has a priority and the element with the highest priority is at the front of the queue and will be the first element

More information

9/28/ Open addressing

9/28/ Open addressing 11.4 Open addressing In open addressing, all elements occupy the hash table itself That is, each table entry contains either an element of the dynamic set or NIL Search for element systematically examines

More information

Stacks and Queues CMPSC 122

Stacks and Queues CMPSC 122 Stacks and Queues CMPSC 122 Stacks and queues are two fundamental abstract data types that will come up quite a bit in computer science. They're quite similar, so we'll look at them together here. I. Fundamentals

More information