Reputation: 12417
I'm working on a binary search tree and try to write a method for creating minimum BST from an array values. However, it's not working successfully. Where I'm making mistake ? It should print values in ascending order using inOrderTraverseTree method. I keep some additional methods and can delete if may feel irrelevant.
I updated the code in question, but, I still need to get the root Node to call the inOrderTraversal (Node root) method.
BinaryTree.java
class Node {
int key;
Node leftChild;
Node rightChild;
Node(int key) {
this.key = key;
}
Node (){}
public String toString() {
return "\n"+key+" ";
}
}
public class BinaryTree {
Node root;
BinaryTree (){
root = null;
}
public void addNode(int key) {
Node newNode = new Node(key);
// If there is no root this becomes root
if (root == null) {
root = newNode;
}
else {
// Set root as the Node we will start
// with as we traverse the tree
Node focusNode = root;
Node parent;
while (true) {
parent = focusNode;
if (key < focusNode.key) {
focusNode = focusNode.leftChild;
if (focusNode == null) {
parent.leftChild = newNode;
return; // All Done
}
} // end of if
else {
focusNode = focusNode.rightChild;
if (focusNode == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
// get the height of binary tree
public int height(Node root) {
if (root == null)
return -1;
Node focusNode = root;
int leftHeight = focusNode.leftChild != null ? height( focusNode.leftChild) : 0;
int rightHeight = focusNode.rightChild != null ? height( focusNode.rightChild) : 0;
return 1 + Math.max(leftHeight, rightHeight);
}
// METHODS FOR THE TREE TRAVERSAL
// inOrderTraverseTree : i) X.left ii) X iii) X.right
public void inOrderTraverseTree(Node focusNode) {
if (focusNode != null) {
inOrderTraverseTree(focusNode.leftChild);
// System.out.println(focusNode);
System.out.print( focusNode );
inOrderTraverseTree(focusNode.rightChild);
}
// System.out.println();
}
// preOrderTraverseTree : i) X ii) X.left iii) X.right
public void preorderTraverseTree(Node focusNode) {
if (focusNode != null) {
System.out.println(focusNode);
preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);
}
}
// postOrderTraverseTree : i) X.left ii) X.right iii) X
public void postOrderTraverseTree(Node focusNode) {
if (focusNode != null) {
preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);
System.out.println(focusNode);
}
}
// END
public Node findNode(int key) {
Node focusNode = root;
while (focusNode.key != key) {
if (key < focusNode.key) {
focusNode = focusNode.leftChild;
} else {
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return null;
}
return focusNode;
}
public boolean remove(int key) {
Node focusNode = root;
Node parent = root;
boolean isItALeftChild = true;
// we will remove the focusNode
while (focusNode.key != key) {
parent = focusNode;
if (key < focusNode.key) {
isItALeftChild = true;
focusNode = focusNode.leftChild;
}
else {
isItALeftChild = false;
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return false;
}
// no child
if (focusNode.leftChild == null && focusNode.rightChild == null) {
if (focusNode == root)
root = null;
else if (isItALeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
}
// one child ( left child )
else if (focusNode.rightChild == null) {
if (focusNode == root)
root = focusNode.leftChild;
else if (isItALeftChild)
parent.leftChild = focusNode.leftChild;
else
parent.rightChild = focusNode.leftChild;
}
else if (focusNode.leftChild == null) {
if (focusNode == root)
root = focusNode.rightChild;
else if (isItALeftChild)
parent.leftChild = focusNode.rightChild;
else
parent.rightChild = focusNode.rightChild;
}
// two children exits
else {
// replacement is the smallest node in the right subtree
// we neeed to delete the focusNode
Node replacement = getReplacementNode(focusNode);
if (focusNode == root)
root = replacement;
else if (isItALeftChild)
parent.leftChild = replacement;
else
parent.rightChild = replacement;
replacement.leftChild = focusNode.leftChild;
}
return true;
}
public Node getReplacementNode(Node replacedNode) {
Node replacementParent = replacedNode;
Node replacement = replacedNode;
Node focusNode = replacedNode.rightChild;
// find the smallest node of the right subtree of the node to be deleted
while (focusNode != null) {
replacementParent = replacement;
replacement = focusNode;
focusNode = focusNode.leftChild;
}
// exit when the focusNode is null
// the replacement is the smallest of the right subtree
if (replacement != replacedNode.rightChild) {
replacementParent.leftChild = replacement.rightChild;
replacement.rightChild = replacedNode.rightChild;
}
return replacement;
}
private void createMinimalBST(int arr[], int start, int end, Node newNode){
if ( end <= start )
return;
int mid = (start + end) / 2;
newNode.key = arr[mid];
if ( root == null ){
root = newNode;
}
System.out.println("new node = "+ newNode );
if (start <= mid-1) {
newNode.leftChild = new Node();
createMinimalBST(arr, start, mid - 1, newNode.leftChild);
}
if (mid+1 <= end) {
newNode.rightChild = new Node();
createMinimalBST(arr, mid + 1, end, newNode.rightChild);
}
// System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}
public void createMinimalBST(int array[]) {
Node n = new Node();
createMinimalBST(array, 0, array.length - 1, n);
}
public static void main(String[] args) {
int[] myArr = { 1,2,3,4,5,6,7,8,9 }; // sortedArrayToBST
BinaryTree myTr = new BinaryTree();
// Node n = BinaryTree.createMinimalBST(myArr);
myTr.createMinimalBST(myArr);
// System.out.println("The root is = "+myTr.root);
// myTr.inOrderTraverseTree(myTr.root);
System.out.println();
myTr.inOrderTraverseTree(myTr.root);
}
}
Upvotes: 1
Views: 439
Reputation: 880
You need to assign the result of createMinimalBST() to a variable.
This method returns a Node:
public Node createMinimalBST(int array[]) {...}
However, in the main, ...
myTr.createMinimalBST(myArr);
you calls to the method but no variable try to hold the result.
Also, You may want to make createMinialBST to be public static and call it like this:
myTr = BinaryTree.createMinimalBST(myArr);
Beside, there are 2 more ways to make this work:
1) move createMinimalBST() into Node, so that the recursion can occur while the key will be setting in-place.
private void createMinimalBST(int arr[], int start, int end){
int mid = (start + end) / 2;
this.key = arr[mid];
System.out.println("new node = "+n);
if (start <= mid-1) {
this.leftChild = new Node();
this.leftChild.createMinimalBST(arr, start, mid - 1);
}
if (mid+1 <= end) {
this.rightChild = new Node();
this.rightChild.createMinimalBST(arr, mid + 1, end);
}
System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}
2) If you want that method stays in BinaryTree, you can consider to pass in the Node as a parameter into createMinimalBST() ilke createMinimalBST(arr, node);
private void createMinimalBST(int arr[], int start, int end, Node newNode){
int mid = (start + end) / 2;
newNode.key = arr[mid];
System.out.println("new node = "+n);
if (start <= mid-1) {
newNode.leftChild = new Node();
createMinimalBST(arr, start, mid - 1, newNode.leftChild);
}
if (mid+1 <= end) {
newNode.rightChild = new Node();
createMinimalBST(arr, mid + 1, end, newNode.leftChild);
}
System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}
Upvotes: 1