JAVA PLEASE : Assume the Tree consists of your ID digits, inserted in the fashion to minimize the height of the tree. Remove duplicates if needed. Trace the delete() method above in a similar fashion as the insert was traced, delete 3 last digits of your  ID from the tree in the same order as they are present in you  ID.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

JAVA PLEASE :

Assume the Tree consists of your ID digits, inserted in the fashion to minimize the height of the tree. Remove duplicates if needed. Trace the delete() method above in a similar fashion as the insert was traced, delete 3 last digits of your  ID from the tree in the same order as they are present in you  ID.

 

The two methods of our most interest are insert and delete. The pseudocode of these is below
boolean insert(E e)
if (root == null)
root = createNewNode(e) // Create root
else
// Locate the parent node
TreeNode<E> parent ← null
TreeNode<E> current root
while (current!= null)
if (c.compare(e, current.element) < 0)
parent ← current
current
else if (c.compare(e, current.element) > 0)
current
parent
current current.right
else
end if
end while
current.left
return false // no Duplicates! Skip it!
else
// Create the new node and attach it
if (c.compare(e, parent.element) < 0)
parent.left ← createNewNode(e)
createNewNode(e)
parent.right
end if
end if
size++
return true // Element inserted
end insert
public boolean delete(E e) {
// find the node to delete and its parent
TreeNode<E> parent - null
TreeNode<E> current root
while (current != null)
if (c.compare(e, current.element) < 0)
parent
current ←
else if (c.compare(e, current.element) > 0)
parent
current
current ← current.right
else
break // It is pointed at by current
end if
end while
if (current == null)
return false // Element is not in the tree
end if
// Case 1: current has no left child
if (current.left == null)
// Connect the parent with the right child
of current
if (parent
root
current
current.left
else
else
if (c.compare(e, parent.element) < 0)
parent.left
current.right
end if
end if
null)
current.right
parent.right
current.right
else
// Case 2: The current node has a left child
// Locate the rightmost node in the left
Transcribed Image Text:The two methods of our most interest are insert and delete. The pseudocode of these is below boolean insert(E e) if (root == null) root = createNewNode(e) // Create root else // Locate the parent node TreeNode<E> parent ← null TreeNode<E> current root while (current!= null) if (c.compare(e, current.element) < 0) parent ← current current else if (c.compare(e, current.element) > 0) current parent current current.right else end if end while current.left return false // no Duplicates! Skip it! else // Create the new node and attach it if (c.compare(e, parent.element) < 0) parent.left ← createNewNode(e) createNewNode(e) parent.right end if end if size++ return true // Element inserted end insert public boolean delete(E e) { // find the node to delete and its parent TreeNode<E> parent - null TreeNode<E> current root while (current != null) if (c.compare(e, current.element) < 0) parent current ← else if (c.compare(e, current.element) > 0) parent current current ← current.right else break // It is pointed at by current end if end while if (current == null) return false // Element is not in the tree end if // Case 1: current has no left child if (current.left == null) // Connect the parent with the right child of current if (parent root current current.left else else if (c.compare(e, parent.element) < 0) parent.left current.right end if end if null) current.right parent.right current.right else // Case 2: The current node has a left child // Locate the rightmost node in the left
Let's trace inserting 1,2,3,4 in this order
Step 2: //we have only root with 1, insert 2
if (root == null) false, root = 1
// Locate the parent node
TreeNode<E> parent
TreeNode<E> current
//subfree of the current node and its parent
TreeNode<E> parentOfRightMost ← current
TreeNode<E> rightMost current.left
while (current!= null), true,
Iteration 1: current is 1 is not null
while (rightMost.right != null)
parentOfRight Most - right Most
rightMost rightMost.right // go right
end while
Step 1: //the tree is empty, we need to insert 1
if (root == null) is true, so we createNewNode with value 1 and it is our root // Created root with1
size of Tree is 0+1=1
if (2-1=1) < 0, false
else if (2-1=1) > 0, true
our parent is root with 1
our current is a right child of root
// Replace current by rightMost
current.element ← rightMost.element
// Eliminate rightmost node
if (parent OfRight Most.right == rightMost)
parent Of Right Most.right
else // parentOfRight Most == current
rightMost.left
parent OfRight Most.left ← - rightMost.left
end if
size-- // Reduce the size of the tree
return true // Element deleted
end delete
null, we created empty TreeNode for root
root with value 1, current is 1
Iteration 2: current was just created, it is null, end while
if (2-1=1) < 0, false
else
parent.right createNewNode(2), we created a node with 2 and assigned as a right child of root
Transcribed Image Text:Let's trace inserting 1,2,3,4 in this order Step 2: //we have only root with 1, insert 2 if (root == null) false, root = 1 // Locate the parent node TreeNode<E> parent TreeNode<E> current //subfree of the current node and its parent TreeNode<E> parentOfRightMost ← current TreeNode<E> rightMost current.left while (current!= null), true, Iteration 1: current is 1 is not null while (rightMost.right != null) parentOfRight Most - right Most rightMost rightMost.right // go right end while Step 1: //the tree is empty, we need to insert 1 if (root == null) is true, so we createNewNode with value 1 and it is our root // Created root with1 size of Tree is 0+1=1 if (2-1=1) < 0, false else if (2-1=1) > 0, true our parent is root with 1 our current is a right child of root // Replace current by rightMost current.element ← rightMost.element // Eliminate rightmost node if (parent OfRight Most.right == rightMost) parent Of Right Most.right else // parentOfRight Most == current rightMost.left parent OfRight Most.left ← - rightMost.left end if size-- // Reduce the size of the tree return true // Element deleted end delete null, we created empty TreeNode for root root with value 1, current is 1 Iteration 2: current was just created, it is null, end while if (2-1=1) < 0, false else parent.right createNewNode(2), we created a node with 2 and assigned as a right child of root
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education