BINARY TREE FILE package com.binarytree; public class BinaryHeap { private int d
ID: 3693908 • Letter: B
Question
BINARY TREE FILE
package com.binarytree;
public class BinaryHeap {
private int data;
private BinaryHeap left;
private BinaryHeap right;
public BinaryHeap(int x) {
data = x;
left = null;
right = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinaryHeap getLeft() {
return left;
}
public void setLeft(BinaryHeap left) {
this.left = left;
}
public BinaryHeap getRight() {
return right;
}
public void setRight(BinaryHeap right) {
this.right = right;
}
}
BINARY SEARCH TREE FILE
package com.binarytree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BinarySearchTree {
private BinaryHeap root = null;
/*
* insert the new element 'x' into the BinarySearchTree
* create a NEW BinaryHeap with data = 'x', left = NULL and right = NULL
*
* if root is NULL then
* insert the new BinaryHeap into root
* else
* find the location where the NEW BinaryHeap has to be inserted
*
* @Param
* - 'x' - New integer
*/
public void insert(int x) {
BinaryHeap heap = new BinaryHeap(x);
if(root == null) {
root = heap;
} else {
BinaryHeap temp = root;
BinaryHeap temp2 = null;
while(temp != null) {
temp2 = temp;
if(temp.getData() > heap.getData()) {
temp = temp.getLeft();
} else {
temp = temp.getRight();
}
}
if(temp2.getData() > heap.getData())
temp2.setLeft(heap);
else
temp2.setRight(heap);
}
}
MAIN
package com.binarytree;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert(5);
tree.insert(3);
tree.insert(2);
tree.insert(10);
tree.insert(8);
tree.insert(12);
tree.insert(14);
List list = tree.levelOrderTravesal();
iterator(list);
System.out.println(" Delete : 14");
tree.delete(14);
list = tree.levelOrderTravesal();
iterator(list);
System.out.println(" Delete : 3");
tree.delete(3);
list = tree.levelOrderTravesal();
iterator(list);
System.out.println(" Delete : 10");
tree.delete(10);
list = tree.levelOrderTravesal();
iterator(list);
System.out.println(" Delete : 5");
tree.delete(5);
list = tree.levelOrderTravesal();
iterator(list);
}
public static void iterator(List list) {
if(list == null)