SlideShare a Scribd company logo
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding a reference to a node’s
parent, as shown below:
BST.TreeNode
#element
#left : TreeNode
#right: TreeNode
#parent: TreeNode
Implement the insert and delete methods in BST class to update the parent for each node in the
tree.
Add the following new method in BST:
/** Return the node for the specific element.
* Return null if the element is not in the tree. */
private TreeNode getNode(E element)
/** Returns true if the node for the element is a lead */
private boolean isLeaf(E element)
/** Returns the path of the element from the specific element to the root in an array list */
public ArrayList getPath(E e)
Write a test program that prompts the user to enter 10 integers, adds them to the tree, deletes the
first integer from the tree, and displays the paths for all leaf nodes.
Sample run
Enter 10 integers :
45 54 67 56 50 45 23 59 23 67
[50, 54, 23]
[59, 56, 67, 54, 23]
Solution
import java.util.NoSuchElementException; public class TreeSet> implements Set {
private TreeNode root; private int size; // Constructs an empty set. public
TreeSet() { root = null; size = 0; } // Adds the given value to this
set, if it was not already in the set. // If the element is already a member of this set, adding it
again has no effect. public void add(E value) { root = add(root, value); }
// private recursive helper for add // uses the "x = change(x)" pattern: // pass in
current state of root // return out desired new state of root private TreeNode
add(TreeNode node, E value) { if (node == null) { node = new
TreeNode(value); // reached dead end; put new node here size++; }
else if (value.compareTo(node.data) > 0) { node.right = add(node.right, value);
} else if (value.compareTo(node.data) < 0) { node.left = add(node.left,
value); } // else a duplicate; do nothing return node; } // Returns
true if this set contains the given element value. public boolean contains(E value) {
return contains(root, value); } // private recursive helper for contains private
boolean contains(TreeNode node, E value) { if (node == null) { return
false; } else if (value.compareTo(node.data) > 0) { return
contains(node.right, value); } else if (value.compareTo(node.data) < 0) {
return contains(node.left, value); } else { // value.equals(root.data);
found it! return true; } } // Returns the minimum value in this
set. // Throws a NoSuchElementException if this set is empty. public E getMin() {
if (root == null) { throw new NoSuchElementException("empty tree");
} return getMin(root); } // private recursive helper for getMin
private E getMin(TreeNode node) { if (node.left == null) { return
node.data; } else { return getMin(node.left); } }
// Returns true if this set does not contain any elements. public boolean isEmpty() {
return size == 0; } // Prints all elements of this tree in a left-to-right order (in-order)
traversal. public void print() { print(root); } // private recursive helper
for print private void print(TreeNode node) { if (node != null) {
print(node.left); System.out.print(node.data + " ");
print(node.right); } // implicit base case: else do nothing } // Removes
the given value from this set, if it is found in this set. // If the element is not found, calling
this method has no effect. public void remove(E value) { root = remove(root,
value); } // private recursive helper for remove private TreeNode
remove(TreeNode node, E value) { if (node == null) { // nothing here;
do nothing? } else if (value.compareTo(node.data) < 0) { node.left =
remove(node.left, value); } else if (value.compareTo(node.data) > 0) {
node.right = remove(node.right, value); } else { // (value == root.data) //
found it! remove now. size--; if (node.left == null && node.right
== null) { // leaf node = null; } else if
(node.right == null) { // only left child node = node.left;
} else if (node.left == null) { // only right child
node = node.right; } else { // hard case: two children;
replace me with min from my right E rightMin = getMin(node.right);
node.right = remove(node.right, rightMin); node.data = rightMin;
} } return node; } // Returns the number of
elements in this set. public int size() { return size; } // Each
TreeNode object stores a single node of a binary tree. // The class has just public data fields
and constructors. // This version of our code places the node as an inner class. private
class TreeNode { public E data; // data stored at this node public
TreeNode left; // reference to left subtree public TreeNode right; // reference to right
subtree // Constructs a leaf node with the given data. public TreeNode(E
data) { this(data, null, null); } // Constructs a leaf or branch
node with the given data and links. public TreeNode(E data, TreeNode left, TreeNode
right) { this.data = data; this.left = left; this.right =
right; } } }

More Related Content

Similar to Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf

Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
illyasraja7
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdf
ajaycosmeticslg
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
sauravmanwanicp
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
xlynettalampleyxc
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docx
JakeT2gGrayp
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
flashfashioncasualwe
 
There is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdfThere is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdf
Dhanrajsolanki2091
 
include ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdfinclude ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdf
adisainternational
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
pratikradia365
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
petercoiffeur18
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
archgeetsenterprises
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
C programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdfC programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdf
info309708
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
adityastores21
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
sivakumar19831
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdf
amarndsons
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
deepakangel
 

Similar to Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf (20)

Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdf
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docx
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
 
There is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdfThere is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdf
 
include ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdfinclude ltiostreamgt include ltfstreamgt in.pdf
include ltiostreamgt include ltfstreamgt in.pdf
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
C programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdfC programming. Answer question only in C code Ninth Deletion with B.pdf
C programming. Answer question only in C code Ninth Deletion with B.pdf
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdf
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
 

More from Footageetoffe16

Why did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdfWhy did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdf
Footageetoffe16
 
Why was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdfWhy was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdf
Footageetoffe16
 
What is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdfWhat is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdf
Footageetoffe16
 
What were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdfWhat were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdf
Footageetoffe16
 
What is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdfWhat is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdf
Footageetoffe16
 
What is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdfWhat is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdf
Footageetoffe16
 
What are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdfWhat are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdf
Footageetoffe16
 
What are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdfWhat are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdf
Footageetoffe16
 
The owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdfThe owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdf
Footageetoffe16
 
The government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdfThe government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdf
Footageetoffe16
 
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdfSyntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Footageetoffe16
 
scene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdfscene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdf
Footageetoffe16
 
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdfQUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
Footageetoffe16
 
Problems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdfProblems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdf
Footageetoffe16
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdf
Footageetoffe16
 
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdfPLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
Footageetoffe16
 
List the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdfList the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdf
Footageetoffe16
 
Information is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdfInformation is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdf
Footageetoffe16
 
I need to create two binary search trees in Ruby. One using recursio.pdf
I need to create two binary search trees in Ruby. One using recursio.pdfI need to create two binary search trees in Ruby. One using recursio.pdf
I need to create two binary search trees in Ruby. One using recursio.pdf
Footageetoffe16
 
I am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdfI am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdf
Footageetoffe16
 

More from Footageetoffe16 (20)

Why did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdfWhy did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdf
 
Why was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdfWhy was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdf
 
What is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdfWhat is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdf
 
What were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdfWhat were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdf
 
What is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdfWhat is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdf
 
What is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdfWhat is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdf
 
What are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdfWhat are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdf
 
What are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdfWhat are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdf
 
The owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdfThe owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdf
 
The government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdfThe government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdf
 
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdfSyntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
 
scene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdfscene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdf
 
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdfQUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
 
Problems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdfProblems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdf
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdf
 
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdfPLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
 
List the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdfList the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdf
 
Information is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdfInformation is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdf
 
I need to create two binary search trees in Ruby. One using recursio.pdf
I need to create two binary search trees in Ruby. One using recursio.pdfI need to create two binary search trees in Ruby. One using recursio.pdf
I need to create two binary search trees in Ruby. One using recursio.pdf
 
I am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdfI am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdf
 

Recently uploaded

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 

Recently uploaded (20)

Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 

Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf

  • 1. Assignment 9 (Parent reference for BST) Redefine TreeNode by adding a reference to a node’s parent, as shown below: BST.TreeNode #element #left : TreeNode #right: TreeNode #parent: TreeNode Implement the insert and delete methods in BST class to update the parent for each node in the tree. Add the following new method in BST: /** Return the node for the specific element. * Return null if the element is not in the tree. */ private TreeNode getNode(E element) /** Returns true if the node for the element is a lead */ private boolean isLeaf(E element) /** Returns the path of the element from the specific element to the root in an array list */ public ArrayList getPath(E e) Write a test program that prompts the user to enter 10 integers, adds them to the tree, deletes the first integer from the tree, and displays the paths for all leaf nodes. Sample run Enter 10 integers : 45 54 67 56 50 45 23 59 23 67 [50, 54, 23] [59, 56, 67, 54, 23] Solution import java.util.NoSuchElementException; public class TreeSet> implements Set { private TreeNode root; private int size; // Constructs an empty set. public TreeSet() { root = null; size = 0; } // Adds the given value to this set, if it was not already in the set. // If the element is already a member of this set, adding it again has no effect. public void add(E value) { root = add(root, value); } // private recursive helper for add // uses the "x = change(x)" pattern: // pass in current state of root // return out desired new state of root private TreeNode add(TreeNode node, E value) { if (node == null) { node = new TreeNode(value); // reached dead end; put new node here size++; }
  • 2. else if (value.compareTo(node.data) > 0) { node.right = add(node.right, value); } else if (value.compareTo(node.data) < 0) { node.left = add(node.left, value); } // else a duplicate; do nothing return node; } // Returns true if this set contains the given element value. public boolean contains(E value) { return contains(root, value); } // private recursive helper for contains private boolean contains(TreeNode node, E value) { if (node == null) { return false; } else if (value.compareTo(node.data) > 0) { return contains(node.right, value); } else if (value.compareTo(node.data) < 0) { return contains(node.left, value); } else { // value.equals(root.data); found it! return true; } } // Returns the minimum value in this set. // Throws a NoSuchElementException if this set is empty. public E getMin() { if (root == null) { throw new NoSuchElementException("empty tree"); } return getMin(root); } // private recursive helper for getMin private E getMin(TreeNode node) { if (node.left == null) { return node.data; } else { return getMin(node.left); } } // Returns true if this set does not contain any elements. public boolean isEmpty() { return size == 0; } // Prints all elements of this tree in a left-to-right order (in-order) traversal. public void print() { print(root); } // private recursive helper for print private void print(TreeNode node) { if (node != null) { print(node.left); System.out.print(node.data + " "); print(node.right); } // implicit base case: else do nothing } // Removes the given value from this set, if it is found in this set. // If the element is not found, calling this method has no effect. public void remove(E value) { root = remove(root, value); } // private recursive helper for remove private TreeNode remove(TreeNode node, E value) { if (node == null) { // nothing here; do nothing? } else if (value.compareTo(node.data) < 0) { node.left = remove(node.left, value); } else if (value.compareTo(node.data) > 0) { node.right = remove(node.right, value); } else { // (value == root.data) // found it! remove now. size--; if (node.left == null && node.right == null) { // leaf node = null; } else if (node.right == null) { // only left child node = node.left; } else if (node.left == null) { // only right child node = node.right; } else { // hard case: two children; replace me with min from my right E rightMin = getMin(node.right); node.right = remove(node.right, rightMin); node.data = rightMin; } } return node; } // Returns the number of
  • 3. elements in this set. public int size() { return size; } // Each TreeNode object stores a single node of a binary tree. // The class has just public data fields and constructors. // This version of our code places the node as an inner class. private class TreeNode { public E data; // data stored at this node public TreeNode left; // reference to left subtree public TreeNode right; // reference to right subtree // Constructs a leaf node with the given data. public TreeNode(E data) { this(data, null, null); } // Constructs a leaf or branch node with the given data and links. public TreeNode(E data, TreeNode left, TreeNode right) { this.data = data; this.left = left; this.right = right; } } }