2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CS 367 – Introduction to Data Structures
CS 367 Introduction to Data Structures Lecture 7.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
BST Numbers, Memory, Removal & Tree Maps. BST Numbers n = number values contained h = height.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
BST Trees
COSC160: Data Structures Binary Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Binary Search Trees.
Section 8.1 Trees.
Trees.
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Binary Search Trees (I)
COMP 103 Binary Search Trees.
ITEC 2620M Introduction to Data Structures
Chapter 20: Binary Trees.
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Chapter 21: Binary Trees.
Node Removal From BST Source:
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Lecture 12 CS203 1.
2018, Fall Pusan National University Ki-Joune Li
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Podcast Ch18c Title: BST delete operation
Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas Kuehne, VUW COMP 103 Marcus Frean Binary Search Trees IV

2 RECAP-TODAY  RECAP  Binary search trees: binary tree with ordering constraint  pseudocode for add and contains on a BST  TODAY  remove from a BST

3 BST: Remove  how would you remove... 1.a leaf node? 2.a node with one child? 3.a node with two children? O O G G C C K K S S X X U U E E count root A A M M I I T T Q Q V V

4 BST: Remove Two Easy Cases: 1.Node has no children ⇒ just remove it 2.Node has one child ⇒ pull up the child into the node’s place

5 BST: Remove – 0 or 1 child Remove: Anton Lili Des Eve Fred Berta Anton Eve Kim Martha Des Lili Otto Pete Nina Gert Celia Joe Hans count root

6 …OR that it contains the minimum value in the right subtree BST: Remove A good way to think of the “leftmost node“ in the right subtree is that it is the successor in an in-order traversal… O O G G C C K K S S X X U U E E A A M M T T Q Q V V Third, more interesting case: 3.Node has two children: ⇒ Put leftmost node of the right subtree in place of the node simple case: right child has no left child ⇒ the right child is the leftmost! less simple case:right child has a left subtree…

7 BST: Remove – 2 children  a) Remove Martha  In-order successor is Nina  no need for further work since Nina was a leaf  b) Remove Berta  In-order successor is Celia  Celia is easy to replace by “pulling up” her only child Fred Berta Kim Martha Des Lili Otto Pete Nina Gert Celia Joe Hans count root

8 BST: Remove – 2 children  c) Remove Fred  in-order successor is Gert  Gert had a right child…  …but is easy to replace by “pulling up” his only child  Fred happened to be the root  need to update “root”?  definitely if Fred had been the last node Fred Berta Anton Eve Kim Martha Des Lili Otto Pete Nina Gert Celia Joe Hans count root the buck always stops here, i.e., this is the hardest case! Demo: (ie. google “BST visualization galles”) - note they go with the “other side” (they pull up right-most of the left child)

9 Remove: Recursive Function in BSTNode public BSTNode remove (E value) { if value equals this.node return replacementSubtreeFromChildren (this.left, this.right) // could not solve easily, have to defer to children if value smaller than this.item set this.left to result of this.left.remove(value) else // value is greater than this.item set this.right to result of this.right.remove(value) return this } assuming value is in tree, i.e., will be found. (“null” is not enough to encode both “could not be removed” and “result subtree is empty”) only children changed update the respective child returns a reference to a tree that has the element removed

10 helper function for recursive remove (in BSTNode) public BSTNode replacementSubtreeFromChildren(left, right) { if left and right are not both present return the one present (or null) set leftmostNode to the leftmost node within this.right set this.value to be leftmostNode.value // now that leftmostNode’s value is copied, leftmostNode can be removed set this.right to be the result of right.remove(value) return this } easy cases node remains, just value and right child changed copy value returns a reference to a subtree that contains all children from ‘left’ and ‘right’

11 Remove: Iterative Version in BSTSet public boolean remove (E value) { if tree is empty or value is invalidreturn false if value is in root set root to removeRootOf(root), decrement count, return true set currentNode to root; // look for parent of node containing value forever if value is less than currentNode.item if there is no left child return false if value is in left child set left child of currentNode to removeRootOf(left child) decrement count,return true else set currentNode to left child else // value is greater than item in currentNode if right child is nullreturn false if value is in right child set right child of currentNode to removeRootOf(right child) decrement count,return true else set currentNode to right child special case of root continue search

12 the helper for iterative form of remove (in BSTSet) Given a subtree, this returns a new subtree with the root node of the subtree removed: private BSTNode removeRootOf( BSTNode subroot) { if subroot has no children, return null if subroot has only one child, return that child set rightChild to be right child of subroot if rightChild has no left subtree set left child of rightChild to be left child of subroot return rightChild // find parent of leftmost node in right subtree set parent to rightChild while parent has a left-left grandchild { set parent to its left child } // parent’s left child is now the leftmost node in right subtree set leftmostNode to left child of parent set parent’s left node to right child of leftmostNode set right child of leftmostNode to rightChild set left child of leftmostNode to left child of subroot; return leftmostNode; simple case easy cases tricky case