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

Slides:



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

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
CS 171: Introduction to Computer Science II
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.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
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.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
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 / 19 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 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
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
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.
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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
CS 206 Introduction to Computer Science II 09 / 26 / 2008 Instructor: Michael Eckmann.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
B+-Tree Deletion Underflow conditions B+ tree Deletion Algorithm
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Instructor: Lilian de Greef Quarter: Summer 2017
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Recursive Objects (Part 4)
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Binary Search Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Objective: Understand Concepts related to trees.
Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Search Sorted Array: Binary Search Linked List: Linear Search
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Trees.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Comments? Binary Search trees –Height – Remove rightmost – Remove leftmost – Delete a key

Binary Search Trees Let's look at algorithms to do the following – Height of a BST – Remove rightmost – Remove leftmost – Delete a key

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

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.

Delete a key in a BST There are several cases to consider –1) if searchkey was not found –2) currNode == root and currNode.left == null –3) currNode == root and currNode.right == null –4) currNode != root and currNode.left == null –5) currNode != root and currNode.right == null –6) currNode == root and both right and left are not null –7) currNode != root and both right and left are not null

Delete a key in a BST There are several cases to consider –1) if searchkey was not found –done –2) currNode == root and currNode.left == null –make root.right be the root and new root's parent be null –3) currNode == root and currNode.right == null –make root.left be the root and new root's parent be null

Delete a key in a BST There are several cases to consider –4) currNode != root and currNode.left == null –Make currNode's parent now point to currNode.right Change parent's left or right depending on whether currNode was the left or right child –5) currNode != root and currNode.right == null –Make currNode's parent now point to currNode.left Change parent's left or right depending on whether currNode was the left or right child

Delete a key in a BST There are several cases to consider –6) currNode == root and both right and left are not null –7) currNode != root and both right and left are not null –These are the hard ones because the node to delete has two children. We need to maintain the properties of the BST after deletion. –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)‏ –Which means to remove the RM node in the subtree that has currNode as the root Let's fix removeRightmost to handle arbitrary subtrees