Binary Search Trees CMPSC 122



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

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

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

Full and Complete Binary Trees

Converting a Number from Decimal to Binary

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Ordered Lists and Binary Trees

Data Structure [Question Bank]

Analysis of Algorithms I: Binary Search Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

DATA STRUCTURES USING C

Data Structures and Algorithms

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

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)

TREE BASIC TERMINOLOGIES

Data Structures. Level 6 C Module Descriptor

Algorithms Chapter 12 Binary Search Trees

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

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.

Data Structures and Algorithms Written Examination

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

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

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

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

PES Institute of Technology-BSC QUESTION BANK

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

The ADT Binary Search Tree

Binary Trees and Huffman Encoding Binary Search Trees

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

10CS35: Data Structures Using C

GRAPH THEORY LECTURE 4: TREES

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

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

From Last Time: Remove (Delete) Operation

Persistent Binary Search Trees

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Binary Heap Algorithms

Lecture Notes on Binary Search Trees

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

Chapter 14 The Binary Search Tree

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Exam study sheet for CS2711. List of topics

Symbol Tables. Introduction

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:

Data Structure with C

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

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

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

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Data Structures Fibonacci Heaps, Amortized Analysis

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

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Parallelization: Binary Tree Traversal

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

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

Data Structures. Chapter 8

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

Lecture Notes on Binary Search Trees

Binary Heaps. CSE 373 Data Structures

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

Algorithms and Data Structures

Analysis of Algorithms I: Optimal Binary Search Trees

CS711008Z Algorithm Design and Analysis

Alex. Adam Agnes Allen Arthur

Algorithms and Data Structures

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

M-way Trees and B-Trees

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

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

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

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

Dynamic Programming Problem Set Partial Solution CMPSC 465

Sample Questions Csci 1112 A. Bellaachia

Analysis of Algorithms, I

Rotation Operation for Binary Search Trees Idea:

Big Data and Scripting. Part 4: Memory Hierarchies

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

Binary Search Trees 3/20/14

recursion, O(n), linked lists 6/14

Java Software Structures

Data Structures Using C++

Introduction to Data Structures and Algorithms

Section IV.1: Recursive Algorithms and Recursion Trees

International Journal of Software and Web Sciences (IJSWS)

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Binary Search Trees. Ric Glassey

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

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

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

A Comparison of Dictionary Implementations

AP Computer Science AB Syllabus 1

Eastern Washington University Department of Computer Science. Questionnaire for Prospective Masters in Computer Science Students

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Transcription:

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

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

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

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

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

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

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 7 15 31 63 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