Removing Nodes from a Binary Tree in Java
This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node. However, if the left child is null, then a non-null right child may take its place. If both children are null, then a null node should take the place of the removed node.
Node.Java
public class Node<K extends Comparable<K>, V> {
private K key;
private V value;
private Node<K, V> left, right;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
public Node(K key, V value, Node<K, V> left, Node<K, V> right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}
public boolean equals(Node<K, V> other) {
if (other == null) return false;
boolean left, right;
if (this.left == null) {
left = other.left == null;
}
else {
left = this.left.equals(other.left);
}
if (this.right == null) {
right = other.right == null;
}
else {
right = this.right.equals(other.right);
}
return left && right && this.key.equals(other.key) && this.value.equals(other.value);
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public Node<K, V> getLeft() {
return left;
}
public void setLeft(Node<K, V> left) {
this.left = left;
}
public Node<K, V> getRight() {
return right;
}
public void setRight(Node<K, V> right) {
this.right = right;
}
}
BinaryTree.Java
public class BinaryTree<K extends Comparable<K>, V> {
private Node<K, V> root;
public BinaryTree(Node root) {
this.root = root;
}
public Node<K, V> getRoot() {
return this.root;
}
public void remove(K key) {
//COMPLETE THIS FUNCTION
}
// Node.java
public class Node<K extends Comparable<K>, V>
{
private K key;
private V value;
private Node<K, V> left, right;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
public Node(K key, V value, Node<K, V> left, Node<K,
V> right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}
public boolean equals(Node<K, V> other)
{
if (other == null) return false;
boolean left, right;
if (this.left == null) {
left = other.left == null;
}
else {
left = this.left.equals(other.left);
}
if (this.right == null) {
right = other.right == null;
}
else {
right = this.right.equals(other.right);
}
return left && right && this.key.equals(other.key)
&& this.value.equals(other.value);
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public Node<K, V> getLeft() {
return left;
}
public void setLeft(Node<K, V> left) {
this.left = left;
}
public Node<K, V> getRight() {
return right;
}
public void setRight(Node<K, V> right) {
this.right = right;
}
}
//end of Node.java
// BinaryTree.java
public class BinaryTree<K extends Comparable<K>, V>
{
private Node<K, V> root;
public BinaryTree(Node root) {
this.root = root;
}
public Node<K, V> getRoot() {
return this.root;
}
public void remove(K key) {
Node<K, V> parent = null; //
set parent to null
Node<K, V> current = root; //
set current to root
// loop to get the node with
key
while(current != null)
{
// node found,
exit the loop
if(current.getKey().compareTo(key) == 0)
{
break;
}
else
if(current.getKey().compareTo(key) < 0) // if current's key <
key, then key if present must be in right subtree since it is a
BST
{
parent = current;
current = current.getRight();
}
else
// if current's key > key, then key if
present must be in left subtree since it is a BST
{
parent = current;
current = current.getLeft();
}
}
// key found in tree
if (current != null)
{
// current is a
leaf node i.e no children
if(current.getLeft() == null && current.getRight() ==
null)
{
if(parent == null) // if root node, set root to
null
{
root = null;
}
else // not a root node
{
// if current is left child
of parent, set left child of parent to null
if(parent.getLeft() ==
current)
parent.setLeft(null);
else // if
current is right child of parent, set right child of parent to
null
parent.setRight(null);
}
}
else
if(current.getLeft() == null) // current has no left child, replace
the node with right child
{
// root node, set root to its right child
if(parent == null)
{
root = root.getRight();
}
else // not a root node
{
// if current is left child
of parent, set left child of parent to right child of current
if(parent.getLeft() ==
current)
parent.setLeft(current.getRight());
else
// if current is right child of parent, set
right child of parent to right child of current
parent.setRight(current.getRight());
}
}
else
{
// has left child
// set rightMost to the left child of
current
Node<K, V> rightMost =
current.getLeft();
// loop to get the right most node of the left
child of current
while(rightMost.getRight() != null)
rightMost =
rightMost.getRight();
// set the right child of right-most node to
right child of current
rightMost.setRight(current.getRight());
if(parent == null) // root node, set
root to its left child
root = root.getLeft();
else // not a root node
{
// if current is left child
of parent, set left child of parent to left child of current
if(parent.getLeft() ==
current)
parent.setLeft(current.getLeft());
else // if
current is right child of parent, set right child of parent to left
child of current
parent.setRight(current.getLeft());
}
}
}
}
}
//end of BinaryTree.java
Removing Nodes from a Binary Tree in Java This section requires you to complete the following...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...
Have to write the tree into a text file?
JAVA CODE
Binary search tree
This is the tree
public class Buildbst {
private int data;
private Buildbst left;
private Buildbst right;
//Set the binary search tree
public Buildbst(int data)
{
this.data = data;
this.left = null;
this.right =null;
}
public int getData() {
return data;
}
public void
setData(int data) {
this.data = data;
}
public Buildbst getLeft() {
return left;
}
public void setLeft(Buildbst
left) {
this.left = left;...
I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list //constructor - create an empty binary tree public BinaryTree() { root = null; } //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); } //deleteTree() - remove all items from tree public void deleteList() { root =...
Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET { private Node root; private static class Node { public final int key; public Node left, right; public Node(int key) { this.key = key; } } public int sizeAboveDepth(int...
Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys. (Both low key and high key do not have to be a key on the list). BST.java import java.lang.Comparable; /** Binary Search Tree implementation for Dictionary ADT */ class BST<Key extends Comparable<? super Key>, E> implements Dictionary<Key, E> { private BSTNode<Key,E> root; // Root of the BST int nodecount; //...
Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...
Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap (a complete binary tree whose entries satisfy the heap-order property). For this problem, complete the included HeapPriorityQueue class by using the LinkedBinaryTree class to represent a heap. Hint: the most challenging part of this problem is identifying the last Position in the heap and the next available Position in the heap. It is suggested that you review the array-based heap to better understand how...
Need help for three BinaryTree class public class BinaryTree { //Implements a Binary Tree of Strings private class Node { private Node left; private String data; private Node right; private Node parent; // reference to the parent node // the parent is null for the root node private Node(Node L, String d, Node r, Node p) { left = L; data...
Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...
Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree { class Node{ int key; Node left,right; public Node(int item) { key = item; left = right = null; } } Node root; public void BinaryTree(){ root = null; } void...