Podcast Ch18c Title: BST delete operation

Slides:



Advertisements
Similar presentations
BST Search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is.
Advertisements

Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
Binary Search Trees Chapter 7 Objectives
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.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
Tree.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Starting at Binary Trees
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
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.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
Binary Search Trees Chapter 7 Objectives
Data Structures for Java William H. Ford William R. Topp
Podcast Ch17b Title: Iterative Tree Traversal
Chapter 25 Binary Search Trees
Recursive Objects (Part 4)
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Podcast Ch17d Title: Drawing a Binary Tree
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Podcast Ch17a Title: Expression Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Search Trees.
Data Structures and Algorithms
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Data Structures for Java William H. Ford William R. Topp
Node Removal From BST Source:
Search Sorted Array: Binary Search Linked List: Linear Search
Podcast Ch18b Title: STree Class
2-3-4 Trees Red-Black Trees
Podcast Ch22c Title: Deleting from a Heap
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
CMSC 341 Splay Trees.
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Podcast Ch25c Title: Shortest Path Algorithm
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Basic Data Structures - Trees
Yan Shi CS/SE 2630 Lecture Notes
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
CMSC 341 Splay Trees.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Podcast Ch21b Title: Collision Resolution
Data Structures Using C++ 2E
Presentation transcript:

Podcast Ch18c Title: BST delete operation Description: Deleting a leaf; deleting a node with one nonnull child; deleting a node with two nonnull children Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Deleting a Node The private method removeNode() erases a node from the tree by finding a replacement node somewhere else in the tree and using it as a substitute for the deleted node. Choose the node so that, when it takes the place of the deleted node, its value maintains the structure of the tree. Subtrees for the deleted node and the replacement node must be reconnected in such a way that the new tree maintains search tree ordering.

Deleting a Node (continued) Notation: dNode identifies the deleted node D. pNode, identifies the parent P of the deleted node. When pNode is null, we are deleting the root. The removeNode() method sets out to find a replacement node R with reference rNode. The algorithm for finding a replacement node considers two cases that depend on the number of children attached to node D.

Deleted Node has Empty Subtree The other child becomes the replacement node R. If the deleted node is a leaf node, it has two null children and the other node R is null.

Deleted Node has an Empty Subtree (continued)

Deleted Node has Empty Subtree (cont) The algorithm must handle two special cases, one in which the deleted node is a leaf node and the other in which the deleted node is the root.

removeNode() (one null child) private void removeNode(STNode<T> dNode){ if (dNode == null) return; // dNode = reference to node D that is deleted // pNode = reference to parent P of node D // rNode = reference to node R that replaces D STNode<T> pNode, rNode; // assign pNode as a reference to P pNode = dNode.parent; // if D has a null child, the // replacement node is the other child if (dNode.left == null || dNode.right == null){ if (dNode.right == null) rNode = dNode.left; else rNode = dNode.right;

removeNode() (one null child) if (rNode != null) // the parent of R is now the parent of D rNode.parent = pNode; // deleting the root node; assign new root if (pNode == null) root = rNode; // attach R to the correct branch of P else if (((Comparable<T>)dNode.nodeValue). compareTo(pNode.nodeValue) < 0) pNode.left = rNode; else pNode.right = rNode; } ...

Deleted Node has Two Nonnull Children A node with two children has some elements in its subtrees that are less than and some elements that are greater than its value. Find a replacement node that maintains the correct ordering among the items. Rather than removing the deleted node, update its value with that of the replacement node. Splice the replacement node out of the tree.

Deleted Node has Two Nonnull Children (continued) Select R as the node with the smallest value that is greater than the value of the deleted node. R is the leftmost node in the right subtree of D.

Deleted Node has Two Nonnull Children (continued) Move to the right child of D and then far-left to the replacement node R, maintaining a reference, PofR, to the parent of R. Splice out R from the tree

removeNode() (two nonnull children) public void removeNode(STNode<T> dNode) { if (dNode.left == null || dNode.right == null) { . . . } // deleted node has two nonnull children else { // pOfRNode is reference to parent of // replacement node STNode<T> pOfRNode = dNode; // first possible replacement is right // child of D; the reference to PofR, // pOfRNode, is the deleted node rNode = dNode.right; pOfRNode = dNode;

removeNode() (two nonnull children continued) // descend down path of left children, keeping // a record of the current node and its parent; // stop at the replacement node while(rNode.left != null) { pOfRNode = rNode; rNode = rNode.left; } . . .

removeNode() (two nonnull children continued) Complete the process by copying the value of the replacement node to D and then connect the right subtree of R to the tree. If PofR is the deleted node, then connect the right subtree of R as the rightchild of D; otherwise connect the right child of R as the left child of PofR. Assign the parent of a nonnull right subtree of R to be pOfRNode, the parent of R.

removeNode() (two nonnull children concluded) // copy the value in R to D dNode.nodeValue = rNode.nodeValue; if (pOfRNode == dNode) dNode.right = rNode.right; else pOfRNode.left = rNode.right; // the parent of the right child of R // becomes the parent of R if (rNode.right != null) rNode.right.parent = pOfRNode;

The method remove() // if item is in the tree, remove it // and return true; otherwise, return false public boolean remove(Object item) { // search tree for item STNode<T> dNode = findNode(item); if (dNode == null) return false; removeNode(dNode); treeSize--; modCount++; return true; }

Student Question Given the tree Draw the tree after each of the following sequential deletions: 20, 25, 35

Practice Problem Given the tree Draw the tree after each of the following sequential deletions: 26, 33, 65, 40