1 Binary Search Trees III Delete Chapter 6. 2 Objectives You will be able to write code to delete a node from a Binary Search Tree.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)
1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
Discussion 2: PA1 Siarhei Vishniakou CSE 100 Summer 2014.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Binary Search Trees Chapter 6.
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.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Binary Trees – Part I CS 367 – Introduction to Data Structures.
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.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
BSTImp: Insert, Delete. Inserting an Element into a BST Search for the position in the tree where the element would be found Insert the element in the.
1 AVL Trees II Implementation. 2 Download: 2011_03_28_AVL_Tree/ File AVL_Tree_Demo.zip
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.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
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 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
CSC 205 Java Programming II Lecture 28 BST Implementation.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
CSCS-200 Data Structure and Algorithms Lecture
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
Lecture 9 Binary Trees Trees General Definition Terminology
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
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
Binary Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1.
Binary Search Trees Chapter 7 Objectives
Unit 7 Trees, Tree Traversals and Binary Search Trees
ITEC324 Principle of CS III
Binary search tree. Removing a node
UNIT III TREES.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Trees.
Binary Search Tree Chapter 10.
Analysis of Algorithms
Section 8.1 Trees.
Trees.
Chapter 16 Tree Implementations
Revised based on textbook author’s notes.
Binary Search Trees Why this is a useful data structure. Terminology
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.
Binary Search Trees (BSTs)
Chapter 16 Tree Implementations
Binary Search Trees (BSTs)
Chapter 20: Binary Trees.
Trees.
CSC 205 – Java Programming II
Binary Search Tree.
Binary Search Trees (BSTs)
Presentation transcript:

1 Binary Search Trees III Delete Chapter 6

2 Objectives You will be able to write code to delete a node from a Binary Search Tree.

3 Issues in Deletion from a BST Deleting a node from a BST is more complex than adding a node. We have to ensure that the BST is still a binary tree after we delete a node. And that the BST constraints are still met. Everything to the left of any node is <=. Everything to the right of any node is >=.

4 Deletion from a BST Three possible cases for deleting a node, x, from a BST: 1. The node, x, is a leaf. 2. The node, x, has one child. 3. The node, x, has two children.

5 Deleting a Leaf Case 1. The node, x, is a leaf.

6 Deleting a Node with One Child Case 2. The node, x, has one child.

7 Deleting a Node with Two Children Case 3: The node, x, has two children. Deletion by copying. (Hibbard & Knuth) Replace contents of x with the contents of its inorder successor K Note that inorder successor will have no left child. Why?

8 Delete the former inorder successor node as described for cases 1 and 2 Deleting a Node with Two Children K The node that was the inorder successor will either be a leaf or have one child (a right child.) In this example it has a right child.

9 Implementing Deletion Download most recent version of BST _03_21_Delete_Node/ _03_21_Delete_Node/ BST_Vertical_Display Expand. Build and run. Rename folder BST_with_Delete

10 Downloaded Program Running

11 Add Delete Node Method In public section of BST class definition: void delete_node(const T&); In protected section of BST class definition: void delete_leaf(BSTNode *p, BSTNode *prev); void delete_node_with_one_child(BSTNode *p, BSTNode *prev) ; void delete_node_with_two_children(BSTNode *p) ;

12 Implementation Copy: _03_21_Delete_Node/ File BST_Delete_Node.cpp.txt _03_21_Delete_Node/

13 Below Class Defintion template void BST ::delete_node(const T& el) { BSTNode *p = root; BSTNode *prev = 0; // Find node to be deleted. while (p != 0 && !(p->key == el)) { prev = p; if (p->key < el) { p = p->right; } else { p = p->left; }

14 Delete Node Method (continued) if (p == 0) { // Declare success! cout << el << " is not in the tree\n"; return; } // p points to the node to be deleted. if ((p->left != 0) && (p->right != 0)) { // Delete node with two children abort(); // Not yet implemented } if ((p->left != 0) || (p->right != 0)) { // Delete node with one child. abort(); // Not yet implemented }

15 Delete Node Method (continued) // Delete leaf; delete_leaf(p, prev); }

16 delete_leaf() template void BST ::delete_leaf(BSTNode *p, BSTNode *prev) { cout key << endl; if (prev->left == p) { prev->left = 0; } else { prev->right = 0; } delete(p); }

17 In main.cpp Delete traversals. Add at end: cout << endl << "Deleting 20\n"; my_BST.delete_node(20); cout << endl << "Updated tree:\n"; my_BST.display_v(cout); cin.get(); return 0; }

18 Deleting Leaf Node

19 Delete Nonexistent Node Add at end of main: cout << "\n\nDeleting nonexistent node, 15\n"; my_BST.delete_node(15); my_BST.display_v(cout); cout << endl << endl;

20 Deleting Nonexistent Node

21 Delete Node with One Child In function delete_node: if ((p->left != 0) || (p->right != 0)) { // Delete node with one child. delete_node_with_one_child(p, prev); return; } Implementation: _03_21_Delete_Node/ _03_21_Delete_Node/ File delete_node_with_one_child.cpp.txt

22 Delete Node with One Child template void BST ::delete_node_with_one_child(BSTNode *p, BSTNode *prev) { cout key << endl; BSTNode * child; if (p->left != 0) { child = p->left; } else { child = p->right; } if (prev->right == p) { prev->right = child; } else { prev->left = child; } delete(p); }

23 main.cpp cout << "\nDeleting node with one child, 31\n"; my_BST.delete_node(31); cout << "\nUpdated tree:\n"; my_BST.display_v(cout); cout << endl << endl; cin.get(); return 0; }

24 Deleting Node with One Child

25 Delete Node with Two Children In Function delete_node(): if ((p->left != 0) && (p->right != 0)) { // Delete node with two children delete_node_with_two_children(p); return; } Copy implementation from: _03_21_Delete_Node/ File delete_node_with_two_children.cpp.txt

26 Delete Node with Two Children template void BST ::delete_node_with_two_children(BSTNode *p) { cout key << endl; // Find inorder successor to node to be deleted. BSTNode * suc = p->right; BSTNode * prev = p; while (suc->left != 0) { prev = suc; suc = suc->left; }

27 Delete Node with Two Children // Copy data from inorder successor as data for the // node to be deleted. p->key = suc->key; // Delete the node that was the inorder successor. if (suc->right != 0) { // Delete node with one child. delete_node_with_one_child(suc, prev); } else { delete_leaf(suc, prev); }

28 In main.cpp cout << "\nDeleting node with two children, 13\n"; my_BST.delete_node(13); cout << "\nUpdated tree:\n"; my_BST.display_v(cout); cout << endl << endl; cin.get(); return 0; }

29 Deleting Node with Two Children