//Dipen Patel
//CSC-236
//LAB6-PART A
public class TreeNode {
private int value, pos;
private TreeNode left, right;
public TreeNode()
this(0,0);
public TreeNode(int value)
this(value,0);
public TreeNode(int value, int pos)
[Link] = value;
[Link] = pos;
[Link] = null;
[Link] = null;
public int getValue()
return value;
}
public void setValue(int value)
[Link] = value;
public TreeNode getLeft()
return left;
public void setLeft(TreeNode left)
[Link] = left;
public TreeNode getRight()
return right;
public int getPos()
return [Link];
public void setPos(int pos)
[Link] = pos;
}
public void setRight(TreeNode right)
[Link] = right;
public String toString()
return value + " ";
}
//Dipen Patel
//CSC-236
//LAB6-PART A
public interface BTInterface {
public abstract TreeNode getRoot();
//gives the root of the tree
public abstract void setRoot(TreeNode root);
//used to set the root
public abstract int getNumOfSingleParent();
//Returns the number of single parents in the tree
public abstract void insert(int value);
//inserts the int value in accordance to BST rules
public abstract void doSwap();
//initialize swapping
public abstract void doPreOrder();
//prints the tree in preorder
public abstract void doInOrder();
//prints the tree in inorder
public abstract void doPostOrder();
//prints the tree in postorder
}
//Dipen Patel
//CSC-236
//LAB6-PART A
public class BinaryTree extends TreeNode implements BTInterface {
private TreeNode root;
private int numOfSingleParent;
public BinaryTree() {
[Link] = null;
[Link] = 0;
public BinaryTree(int value) {
[Link] = new TreeNode(value);
[Link] = 0;
public TreeNode getRoot() {
return root;
public void setRoot(TreeNode root) {
[Link] = root;
public int getNumOfSingleParent() {
[Link] = 0;
singleParent([Link]);
return [Link];
public void insert(int value) {
TreeNode x = new TreeNode(value);
if ([Link] == null) {
[Link] = x;
} else {
TreeNode curr = [Link];
TreeNode prev = curr;
while (curr != null) {
prev = curr;
if (value < [Link]()) {
curr = [Link]();
} else {
curr = [Link]();
if (value < [Link]()) {
[Link](x);
} else {
[Link](x);
}
public void doSwap() {
swap([Link]);
private void swap(TreeNode curr) {
if ([Link]() != null) {
swap([Link]());
if ([Link]() != null) {
swap([Link]());
if ([Link]() != null || [Link]() != null) {
TreeNode temp = [Link]();
[Link]([Link]());
[Link](temp);
private void singleParent(TreeNode curr) {
if ([Link]() != null) {
singleParent([Link]());
if ([Link]() != null) {
singleParent([Link]());
if (([Link]() != null && [Link]() == null) ||
([Link]() == null && [Link]() != null)) {
[Link]++;
public void doPreOrder() {
preOrder([Link]);
private void preOrder(TreeNode curr) {
if (curr != null) {
[Link](curr);
preOrder([Link]());
preOrder([Link]());
public void doInOrder() {
inOrder([Link]);
private void inOrder(TreeNode curr) {
if (curr != null) {
inOrder([Link]());
[Link](curr);
inOrder([Link]());
public void doPostOrder() {
postOrder([Link]);
private void postOrder(TreeNode curr) {
if (curr != null) {
postOrder([Link]());
postOrder([Link]());
[Link](curr);
}
//Dipen Patel
//CSC-236
//LAB6-PART A
public class BinarySearchTree extends BinaryTree{
private TreeNode root;
private int lastPos;
private int numOfSingleParent;
public BinarySearchTree()
[Link] = null;
[Link] = -1;
public BinarySearchTree(int value)
[Link] = new TreeNode(value);
[Link] = 0;
public TreeNode getRoot()
return root;
public void setRoot(TreeNode root)
[Link] = root;
}
public int getLastPos()
return [Link];
public void setLastPos(int lastPos)
[Link] = lastPos;
public int getNumOfSingleParent()
[Link] = 0;
singleParent([Link]);
return [Link];
public TreeNode find(int pos)
if(pos < 0)
return null;
else if(pos == 0)
return [Link];
else
TreeNode curr = [Link];
pos++;
while(pos > 1)
if(pos % 2 == 0)
curr = [Link]();
else
curr = [Link]();
pos = pos / 2;
return curr;
}//else pos > 0 ends
public void insert(int value)
TreeNode temp = new TreeNode(value, lastPos + 1);
if([Link] == null)
[Link] = temp;
else
if(lastPos % 2 == 0)
find(lastPos / 2).setLeft(temp);
else
find(lastPos / 2).setRight(temp);
}//else root != null, ends
lastPos++;
public void doSwap()
swap([Link]);
private void swap(TreeNode curr)
if([Link]() != null)
swap([Link]());
if([Link]() != null)
swap([Link]());
if([Link]() != null || [Link]() != null)
TreeNode temp = [Link]();
[Link]([Link]());
[Link](temp);
private void singleParent(TreeNode curr)
{
if([Link]() != null)
singleParent([Link]());
if([Link]() != null)
singleParent([Link]());
if(([Link]() != null && [Link]() == null) ||
([Link]() == null && [Link]() != null))
[Link]++;
public void doPreOrder()
preOrder([Link]);
private void preOrder(TreeNode curr)
if(curr != null)
[Link](curr);
preOrder([Link]());
preOrder([Link]());
}
public void doInOrder()
inOrder([Link]);
private void inOrder(TreeNode curr)
if(curr != null)
inOrder([Link]());
[Link](curr);
inOrder([Link]());
public void doPostOrder()
postOrder([Link]);
private void postOrder(TreeNode curr)
if(curr != null)
postOrder([Link]());
postOrder([Link]());
[Link](curr);
}
//Dipen Patel
//CSC-236
//LAB6-PART A
public class TreeDemo {
public static void main(String[] args)
//Tree 1 -- Binary Complete Tree
BinarySearchTree compTreeObj = new BinarySearchTree();
for(int i = 1; i < 7; i++)
[Link](i);
[Link]("\t\tA regular Binary Tree");
[Link]("---------------------------------"
+ "-----------------------------");
[Link]("Inorder traversal: ");
[Link]();
[Link]("\nPostorder traversal: ");
[Link]();
[Link]("\nPreorder traversal: ");
[Link]();
[Link]("\n*******************************"
+ "*******************************");
[Link]("\n---------------------------------"
+ "-----------------------------");
[Link]();
[Link]("\t\tAfter Swapping...");
[Link]("---------------------------------"
+ "-----------------------------");
[Link]("Inorder traversal: ");
[Link]();
[Link]("\nPostorder traversal: ");
[Link]();
[Link]("\nPreorder traversal: ");
[Link]();
[Link]("\n*******************************"
+ "*******************************");
[Link]("\nThe total number of single parent/s is: "
+ [Link]());
[Link]("\n---------------------------------"
+ "-----------------------------");
//Tree 2 -- Binary Search Tree
BinaryTree bSTreeObj = new BinaryTree();
[Link](14);
[Link](15);
[Link](4);
[Link](3);
[Link](9);
[Link](7);
[Link](5);
[Link](18);
[Link](16);
[Link](20);
[Link](17);
[Link]("\t\tA regular Binary Search Tree");
[Link]("---------------------------------"
+ "-----------------------------");
[Link]("Inorder traversal: ");
[Link]();
[Link]("\nPostorder traversal: ");
[Link]();
[Link]("\nPreorder traversal: ");
[Link]();
[Link]("\n*******************************"
+ "*******************************");
[Link]();
[Link]("\n---------------------------------"
+ "-----------------------------");
[Link]("\t\tAfter Swapping.");
[Link]("---------------------------------"
+ "-----------------------------");
[Link]("Inorder traversal: ");
[Link]();
[Link]("\nPostorder traversal: ");
[Link]();
[Link]("\nPreorder traversal: ");
[Link]();
[Link]("\n*******************************"
+ "*******************************");
[Link]("\nThe total number of single parent/s is: "
+ [Link]());
[Link]("---------------------------------"
+ "-----------------------------");
}
OUTPUT:-
//Dipen Patel
//CSC-236
//LAB6-PART B
public interface HeapPriorityQueue
public abstract HeapTreeNode getRoot();
//gives the root of the tree
public abstract void setRoot(HeapTreeNode root);
//used to set the root
public abstract void insert(int value);
//inserts the int value in accordance to Heap tree rules
public abstract void remove();
//removes the root from a non-empty heap
public abstract void doPreOrder();
//prints the tree in preorder
public abstract void doInOrder();
//prints the tree in inorder
public abstract void doPostOrder();
//prints the tree in postorder
}
/Dipen Patel
//CSC-236
//LAB6-PART B
public class HPTree implements HeapPriorityQueue{
private HeapTreeNode root;
private int lastPos;
public HPTree()
[Link] = null;
[Link] = -1;
public HPTree(int value)
[Link] = new HeapTreeNode(value);
[Link] = 0;
public HeapTreeNode getRoot()
return root;
public void setRoot(HeapTreeNode root)
[Link] = root;
}
public int getLastPos()
return [Link];
public void setLastPos(int lastPos)
[Link] = lastPos;
public HeapTreeNode find(int pos)
if(pos < 0)
return null;
else if(pos == 0)
return [Link];
else
HeapTreeNode curr = [Link];
pos++;
int i = -1;
int [] storeDir = new int[10];
while(pos > 1)
if(pos % 2 == 0)
storeDir[++i] = 0;
else
storeDir[++i] = 1;
pos = pos / 2;
while(i >= 0)
if(storeDir[i] == 0)
curr = [Link]();
else
curr = [Link]();
i--;
return curr;
}//else pos > 0 ends
public void insert(int value)
HeapTreeNode temp = new HeapTreeNode(value, lastPos + 1);
if([Link] == null)
[Link] = temp;
else
{
if(lastPos % 2 == 0)
find(lastPos / 2).setLeft(temp);
else
find(lastPos / 2).setRight(temp);
}//else root != null, ends
swapInsert(temp);
lastPos++;
private void swapInsert(HeapTreeNode curr)
if(curr != null)
HeapTreeNode parent = find(([Link]() - 1) / 2);
//if the value inside the curr node is greater than its parent
//then, the values of the parent and the curr will interchange.
if([Link]() > [Link]())
int temp = [Link]();
[Link]([Link]());
[Link](temp);
swapInsert(parent);
}
public void remove()
if([Link] > 0)
//parent of the last node entered
HeapTreeNode lastParent = find((lastPos - 1) / 2);
//sets the last node's value into the root's value
//and setting the parent's pointer
//pointing to the last node to null
if(lastPos % 2 == 0)
//if the last node was a right child
[Link]([Link]().getValue());
[Link](null);
else
//if the last node was a left child
[Link]([Link]().getValue());
[Link](null);
swapRemove([Link]);
else
{ //if the lastPos == 0, or the tree has just the root node
[Link] = null;
[Link]--;
}
private void swapRemove(HeapTreeNode curr)
//if the left node is null,
//curr doesn't have even the right child
if([Link]() != null)
HeapTreeNode left = [Link]();
if([Link]() != null)
HeapTreeNode right = [Link]();
//if the left has the largest value
if([Link]() > [Link]()
&&
[Link]() > [Link]())
//swap the values and
//recurse using the left as a new parent/root
int temp = [Link]();
[Link]([Link]());
[Link](temp);
swapRemove(left);
//if the right has the largest value
else if([Link]() > [Link]()
&&
[Link]() > [Link]())
//swap the values and
//recurse using the right as a new parent/root
int temp = [Link]();
[Link]([Link]());
[Link](temp);
swapRemove(right);
}//if(right != null), ends
//else if(..), right == null but left != null
else if([Link]() > [Link]())
//swap left's and curr(parent)'s value
int temp = [Link]();
[Link]([Link]());
[Link](temp);
}//if(left != null), ends
//if(left == null), no need for swapping
public void doPreOrder()
preOrder([Link]);
private void preOrder(HeapTreeNode curr)
{
if(curr != null)
[Link](curr);
preOrder([Link]());
preOrder([Link]());
public void doInOrder()
inOrder([Link]);
private void inOrder(HeapTreeNode curr)
if(curr != null)
inOrder([Link]());
[Link](curr);
inOrder([Link]());
public void doPostOrder()
postOrder([Link]);
}
private void postOrder(HeapTreeNode curr)
if(curr != null)
postOrder([Link]());
postOrder([Link]());
[Link](curr);
}
//Dipen Patel
//CSC-236
//LAB6-PART B
public class HeapTreeNode {
private int value, pos;
private HeapTreeNode left, right;
public HeapTreeNode()
this(0,0);
public HeapTreeNode(int value)
this(value, 0);
public HeapTreeNode(int value, int pos)
[Link] = value;
[Link] = pos;
[Link] = null;
[Link] = null;
public int getValue()
return value;
}
public void setValue(int value)
[Link] = value;
public HeapTreeNode getLeft()
return left;
public void setLeft(HeapTreeNode left)
[Link] = left;
public HeapTreeNode getRight()
return right;
public void setRight(HeapTreeNode right)
[Link] = right;
public int getPos()
{
return [Link];
public void setPos(int pos)
[Link] = pos;
public String toString()
return value + " ";
}
//Dipen Patel
//CSC-236
//LAB6-PART B
public class HeapDemo
public static void main(String[] args)
HPTree tree1 = new HPTree();
[Link]("\t\tADDING 1-10 INTO A HEAP\n"
+ "---------------------------------"
+ "----------------------------");
for (int i = 1; i < 11; i++)
[Link](i);
[Link]("Inorder traversal: ");
[Link]();
[Link]("\nPostorder traversal: ");
[Link]();
[Link]("\nPreorder traversal: ");
[Link]();
[Link]("\n*******************************"
+ "*******************************");
[Link](); [Link](); [Link]();
[Link]("\n\t\tREMOVING 3 ITEMS FROM THE HEAP\n"
+ "---------------------------------"
+ "----------------------------");
[Link]("Inorder traversal: ");
[Link]();
[Link]("\nPostorder traversal: ");
[Link]();
[Link]("\nPreorder traversal: ");
[Link]();
[Link]("\n*******************************"
+ "*******************************");
}
OUTPUT:-