Lab 6
Lab 6
//CSC-236
//LAB6-PART A
public TreeNode()
this(0,0);
this(value,0);
this.value = value;
this.pos = pos;
this.right = null;
this.left = null;
return value;
}
public void setValue(int value)
this.value = value;
return left;
this.left = left;
return right;
return this.pos;
this.pos = pos;
}
this.right = right;
}
//Dipen Patel
//CSC-236
//LAB6-PART A
//initialize swapping
}
//Dipen Patel
//CSC-236
//LAB6-PART A
public BinaryTree() {
this.root = null;
this.numOfSingleParent = 0;
this.numOfSingleParent = 0;
return root;
this.root = root;
this.numOfSingleParent = 0;
singleParent(this.root);
return this.numOfSingleParent;
if (this.root == null) {
this.root = x;
} else {
prev = curr;
curr = curr.getLeft();
} else {
curr = curr.getRight();
prev.setLeft(x);
} else {
prev.setRight(x);
}
public void doSwap() {
swap(this.root);
if (curr.getLeft() != null) {
swap(curr.getLeft());
if (curr.getRight() != null) {
swap(curr.getRight());
curr.setLeft(curr.getRight());
curr.setRight(temp);
if (curr.getLeft() != null) {
singleParent(curr.getLeft());
if (curr.getRight() != null) {
singleParent(curr.getRight());
preOrder(this.root);
if (curr != null) {
System.out.print(curr);
preOrder(curr.getLeft());
preOrder(curr.getRight());
inOrder(this.root);
if (curr != null) {
inOrder(curr.getLeft());
System.out.print(curr);
inOrder(curr.getRight());
if (curr != null) {
postOrder(curr.getLeft());
postOrder(curr.getRight());
System.out.print(curr);
}
//Dipen Patel
//CSC-236
//LAB6-PART A
public BinarySearchTree()
this.root = null;
this.lastPos = -1;
this.lastPos = 0;
return root;
this.root = root;
}
public int getLastPos()
return this.lastPos;
this.lastPos = lastPos;
this.numOfSingleParent = 0;
singleParent(this.root);
return this.numOfSingleParent;
if(pos < 0)
return null;
else if(pos == 0)
return this.root;
else
pos++;
while(pos > 1)
if(pos % 2 == 0)
curr = curr.getLeft();
else
curr = curr.getRight();
pos = pos / 2;
return curr;
if(this.root == null)
this.root = temp;
else
if(lastPos % 2 == 0)
find(lastPos / 2).setLeft(temp);
else
find(lastPos / 2).setRight(temp);
swap(this.root);
if(curr.getLeft() != null)
swap(curr.getLeft());
if(curr.getRight() != null)
swap(curr.getRight());
curr.setLeft(curr.getRight());
curr.setRight(temp);
{
if(curr.getLeft() != null)
singleParent(curr.getLeft());
if(curr.getRight() != null)
singleParent(curr.getRight());
this.numOfSingleParent++;
preOrder(this.root);
if(curr != null)
System.out.print(curr);
preOrder(curr.getLeft());
preOrder(curr.getRight());
}
public void doInOrder()
inOrder(this.root);
if(curr != null)
inOrder(curr.getLeft());
System.out.print(curr);
inOrder(curr.getRight());
postOrder(this.root);
if(curr != null)
postOrder(curr.getLeft());
postOrder(curr.getRight());
System.out.print(curr);
}
//Dipen Patel
//CSC-236
//LAB6-PART A
compTreeObj.insert(i);
System.out.println("---------------------------------"
+ "-----------------------------");
compTreeObj.doInOrder();
compTreeObj.doPostOrder();
compTreeObj.doPreOrder();
System.out.println("\n*******************************"
+ "*******************************");
System.out.println("\n---------------------------------"
+ "-----------------------------");
compTreeObj.doSwap();
System.out.println("\t\tAfter Swapping...");
System.out.println("---------------------------------"
+ "-----------------------------");
compTreeObj.doInOrder();
compTreeObj.doPostOrder();
compTreeObj.doPreOrder();
System.out.println("\n*******************************"
+ "*******************************");
+ compTreeObj.getNumOfSingleParent());
System.out.println("\n---------------------------------"
+ "-----------------------------");
bSTreeObj.insert(14);
bSTreeObj.insert(15);
bSTreeObj.insert(4);
bSTreeObj.insert(3);
bSTreeObj.insert(9);
bSTreeObj.insert(7);
bSTreeObj.insert(5);
bSTreeObj.insert(18);
bSTreeObj.insert(16);
bSTreeObj.insert(20);
bSTreeObj.insert(17);
System.out.println("---------------------------------"
+ "-----------------------------");
bSTreeObj.doInOrder();
bSTreeObj.doPostOrder();
bSTreeObj.doPreOrder();
System.out.println("\n*******************************"
+ "*******************************");
bSTreeObj.doSwap();
System.out.println("\n---------------------------------"
+ "-----------------------------");
System.out.println("\t\tAfter Swapping.");
System.out.println("---------------------------------"
+ "-----------------------------");
bSTreeObj.doInOrder();
bSTreeObj.doPostOrder();
bSTreeObj.doPreOrder();
System.out.println("\n*******************************"
+ "*******************************");
+ bSTreeObj.getNumOfSingleParent());
System.out.println("---------------------------------"
+ "-----------------------------");
}
OUTPUT:-
//Dipen Patel
//CSC-236
//LAB6-PART B
}
/Dipen Patel
//CSC-236
//LAB6-PART B
public HPTree()
this.root = null;
this.lastPos = -1;
this.lastPos = 0;
return root;
this.root = root;
}
return this.lastPos;
this.lastPos = lastPos;
if(pos < 0)
return null;
else if(pos == 0)
return this.root;
else
pos++;
int i = -1;
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 = curr.getLeft();
else
curr = curr.getRight();
i--;
return curr;
if(this.root == null)
this.root = temp;
else
{
if(lastPos % 2 == 0)
find(lastPos / 2).setLeft(temp);
else
find(lastPos / 2).setRight(temp);
swapInsert(temp);
lastPos++;
if(curr != null)
//if the value inside the curr node is greater than its parent
//then, the values of the parent and the curr will interchange.
curr.setValue(parent.getValue());
parent.setValue(temp);
swapInsert(parent);
}
public void remove()
if(this.lastPos > 0)
if(lastPos % 2 == 0)
this.root.setValue(lastParent.getRight().getValue());
lastParent.setRight(null);
else
this.root.setValue(lastParent.getLeft().getValue());
lastParent.setLeft(null);
swapRemove(this.root);
else
{ //if the lastPos == 0, or the tree has just the root node
this.root = null;
this.lastPos--;
}
if(curr.getLeft() != null)
if(curr.getRight() != null)
&&
curr.setValue(left.getValue());
left.setValue(temp);
swapRemove(left);
&&
right.getValue() > curr.getValue())
curr.setValue(right.getValue());
right.setValue(temp);
swapRemove(right);
curr.setValue(left.getValue());
left.setValue(temp);
preOrder(this.root);
if(curr != null)
System.out.print(curr);
preOrder(curr.getLeft());
preOrder(curr.getRight());
inOrder(this.root);
if(curr != null)
inOrder(curr.getLeft());
System.out.print(curr);
inOrder(curr.getRight());
postOrder(this.root);
}
private void postOrder(HeapTreeNode curr)
if(curr != null)
postOrder(curr.getLeft());
postOrder(curr.getRight());
System.out.print(curr);
}
//Dipen Patel
//CSC-236
//LAB6-PART B
public HeapTreeNode()
this(0,0);
this(value, 0);
this.value = value;
this.pos = pos;
this.right = null;
this.left = null;
return value;
}
this.value = value;
return left;
this.left = left;
return right;
this.right = right;
{
return this.pos;
this.pos = pos;
}
//Dipen Patel
//CSC-236
//LAB6-PART B
+ "---------------------------------"
+ "----------------------------");
tree1.insert(i);
tree1.doInOrder();
tree1.doPostOrder();
tree1.doPreOrder();
System.out.println("\n*******************************"
+ "*******************************");
tree1.remove(); tree1.remove(); tree1.remove();
+ "---------------------------------"
+ "----------------------------");
tree1.doInOrder();
tree1.doPostOrder();
tree1.doPreOrder();
System.out.println("\n*******************************"
+ "*******************************");
}
OUTPUT:-