CS 206 Introduction to Computer Science II 09 / 26 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 10 / 2009 Instructor: Michael Eckmann.
Advertisements

CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 19 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 18 / 2009 Instructor: Michael Eckmann.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 29 / 2008 Instructor: Michael Eckmann.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
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.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
B+-Tree Deletion Underflow conditions B+ tree Deletion Algorithm
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Binary Search Trees.
Ch. 12: Binary Search Trees Ming-Te Chi
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Ch. 12: Binary Search Trees Ming-Te Chi
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

CS 206 Introduction to Computer Science II 09 / 26 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Comments? Binary Search trees –operations

Binary Search Trees Let's look at algorithms to do the following – Print keys in ascending order – Search for a key – Find minimum key – Find maximum key – Insert a key – Height of a BST – Delete a key

Print the Keys To print the keys in increasing order we use inorder traversal. Recursive description of inorder traversal In-order traversal of a tree Start with x being the root check if x is not null then 1) In-order traversal of left(x)‏ 2) print key(x)‏ 3) In-order traversal of right(x)‏ What order of running time is this algorithm for a tree that has n nodes?

Search for a key To search in a binary search tree for a key k, start with x being the root. Here's a recursive description of a search tree_search(x, k)‏ { if (x == null || k == x.key)‏ return x; if (k < x.key)‏ return tree_search(x.left, k)‏ else return tree_search(x.right, k)‏ } What's the running time of this?

Search for a key To search in a binary search tree for a key k, start with x being the root. Here's a recursive description of a search tree_search(x, k)‏ { if (x == null || k == x.key)‏ return x; if (k < x.key)‏ return tree_search(x.left, k)‏ else return tree_search(x.right, k)‏ } What's the running time of this? On the order of the height of the tree. What if the binary search tree is complete (or full.)‏

Find Minimum key in a BST How might we devise an algorithm to do “find the minimum”? Where in the tree is the minimum value?

Find Minimum key in a BST How might we devise an algorithm to do find the minimum? Where in the tree is the minimum value? – It is in the leftmost node x = root; // make sure x is not null before doing this... while (x.left != null)‏ x = x.left; return x; – Is it necessarily a leaf?

Find Maximum key in a BST How might we devise an algorithm to do “find the maximum”? Where in the tree is the maximum value?

Find Maximum key in a BST How might we devise an algorithm to do find the maximum? Where in the tree is the maximum value? – It is in the rightmost node x = root; // make sure x is not null before doing this... while (x.right != null)‏ x = x.right; return x; Running times of these?

Insert a key in a BST How might we devise an algorithm to insert a key into the tree? Can the key go anywhere?

Insert a key in a BST How might we devise an algorithm to insert a key into the tree? Can the key go anywhere? No, it has to follow the rules of BST's so the resulting tree after insert must be a BST. Need to keep track of where we are in the tree as we traverse it and the parent of where we are because we might have to go back up the tree. Let's look at the implementation from yesterday's lab.

Height of BST determining the depth (aka height) of the BST –Recall, the depth (height) of any tree is the maximum depth of any of its leaves. The depth of a node n, is the number of “steps” from the root to the node n. –Let's try to figure out an algorithm to determine the height of a tree recursively. –Any ideas?

Height of BST determining the depth (aka height) of the BST –Recall, the depth (height) of any tree is the maximum depth of any of its leaves. The depth of a node n, is the number of “steps” from the root to the node n. –Let's try to figure out an algorithm to determine the height of a tree recursively. –To do recursion we need a base case and a recursive step. –If a tree has 1 node, then it's height = 0. –If a tree has 0 nodes, then it's height should be less than 0, say - 1.

Height of BST determining the depth (aka height) of the BST –Recall, the depth (height) of any tree is the maximum depth of any of its leaves. The depth of a node n, is the number of “steps” from the root to the node n. –Let's try to figure out an algorithm to determine the height of a tree recursively. –To do recursion we need a base case and a recursive step. –If a tree has 1 node, then it's height = 0. –If a tree has 0 nodes, then it's height should be less than 0, say - 1. –Also, given a node n (that is not null), that node n is the root of a subtree, and the height of this subtree is what?

Height of BST determining the depth (aka height) of the BST –Recall, the depth (height) of any tree is the maximum depth of any of its leaves. The depth of a node n, is the number of “steps” from the root to the node n. –Let's try to figure out an algorithm to determine the height of a tree recursively. –To do recursion we need a base case and a recursive step. –If a tree only has 1 node, the root, what's it's height? 0 –If a tree has 2 nodes, a root and a left (or right) node, then it has height 1. –Also, given a node n (that is not null), that node n is the root of a subtree, and the height of this subtree is 1 + Math.max(height(n.left), height(n.right))‏

Height of BST public int height(BSTNode n)‏ { if (n == null)‏ return -1; else return 1 + Math.max(height(n.left), height(n.right)); } // let's verify this will work with an example.

Remove Maximum Key in a BST Let's consider how to do this. This is equivalent to finding the rightmost node and removing it. Consider several cases.

Remove rightmost node in a binary tree To remove the rightmost node in any binary tree –Case 0) if the rightmost node is the root of the tree, do what? –Case 1) If the rightmost node has no left child, then it is a leaf, so you can simply do rightmost.parent.right = null to remove it –Case 2) If the rightmost node has a left child i.e. (rightmost.left != null), then move the whole left subtree to where the current rightmost node is. That is, do rightmost.parent.right = rightmost.left AND set the parent rightmost.left.parent = rightmost.parent We will have to do more than this if we are looking to remove the RM in an arbitrary subtree of a tree.

Remove leftmost node in a binary tree To remove the leftmost node in any binary tree –Case 0) if the leftmost node is the root of the tree, do what? –Case 1) If the leftmost node has no right child, then it is a leaf, so you can simply do leftmost.parent.left = null to remove it –Case 2) If the leftmost node has a right child, then move the whole right subtree to where the current leftmost node is. That is, do leftmost.parent.left = leftmost.right AND set the parent leftmost.right.parent = leftmost.parent; Again, we will have to do more than this if we are looking to remove the LM in an arbitrary subtree of a tree.

Delete a key in a BST First, start with currNode = root Go down the tree until currNode.key == searchkey. Each step down the tree change currNode to be either the left or right child. So, when the above step is done, we have currNode.parent as the parent and currNode as the node to delete. There are several cases to consider –1) if searchkey was not found –2) currNode == root and currNode.left == null –3) currNode != root and currNode.left == null –4) currNode.left != null

Delete a key in a BST Case 1) if searchkey was not found, do nothing --- done. Case 2) currNode == root and currNode.left == null –Set the root to be root.right, AND then root.parent = null, done. Case 3) currNode != root and currNode.left == null –if the currNode is its parent's left child then currNode.parent.left = currNode.right –if the currNode is its parent's right child then currNode.parent.right = currNode.right Case 4) currNode.left != null –This is the hard one. Why?

Delete a key in a BST Case 4) currNode != root and currNode.left != null –We'll look at the left subtree of currNode and find it's rightMost node, store it in lstrmn (left subtree's rightmost node). –Then we can a) set currNode.key = lstrmn.key b) remove the node lstrmn from the tree by calling removeRightmost(currNode)‏