package binary.search.tree;
public class Node {
Integer num;
Node leftNode;
Node rightNode;
public Node(Integer num) {
this.num = num;
}
public Integer getNum() {
return num;
}
public Node getLeftNode() {
return leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void insert(int i) {
if(i < num) {
if(leftNode == null) {
leftNode = new Node(i);
} else {
leftNode.insert(i);
}
} else {
if(rightNode == null) {
rightNode = new Node(i);
} else {
rightNode.insert(i);
}
}
}
}
The Main Class
public class Node {
Integer num;
Node leftNode;
Node rightNode;
public Node(Integer num) {
this.num = num;
}
public Integer getNum() {
return num;
}
public Node getLeftNode() {
return leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void insert(int i) {
if(i < num) {
if(leftNode == null) {
leftNode = new Node(i);
} else {
leftNode.insert(i);
}
} else {
if(rightNode == null) {
rightNode = new Node(i);
} else {
rightNode.insert(i);
}
}
}
}
package binary.search.tree;
public class Main {
public static void main (String[] args) {
Node binarySearchTree = new Node(8);
binarySearchTree.insert(3);
binarySearchTree.insert(10);
binarySearchTree.insert(1);
binarySearchTree.insert(6);
binarySearchTree.insert(14);
binarySearchTree.insert(4);
binarySearchTree.insert(7);
binarySearchTree.insert(13);
BS(binarySearchTree, 7);
BS(binarySearchTree, 14);
BS(binarySearchTree, 8);
BS_Iterative(binarySearchTree, 7);
BS_Iterative(binarySearchTree, 14);
BS_Iterative(binarySearchTree, 8);
}
private static void BS(Node node, int i) {
int nodeNum = node.getNum();
System.out.println("Num:"+nodeNum);
if(i < nodeNum) {
BS(node.getLeftNode(), i);
} else if (i > nodeNum) {
BS(node.getRightNode(), i);
} else {
System.out.println("Found");
}
}
private static void BS_Iterative(Node node, int i) {
int nodeNum = node.getNum();
System.out.println("Num:"+nodeNum);
while(nodeNum != i) {
if(i < nodeNum) {
node = node.getLeftNode();
} else {
node = node.getRightNode();
}
nodeNum = node.getNum();
System.out.println("Num:"+nodeNum);
}
System.out.println("Found");
}
}
public class Main {
public static void main (String[] args) {
Node binarySearchTree = new Node(8);
binarySearchTree.insert(3);
binarySearchTree.insert(10);
binarySearchTree.insert(1);
binarySearchTree.insert(6);
binarySearchTree.insert(14);
binarySearchTree.insert(4);
binarySearchTree.insert(7);
binarySearchTree.insert(13);
BS(binarySearchTree, 7);
BS(binarySearchTree, 14);
BS(binarySearchTree, 8);
BS_Iterative(binarySearchTree, 7);
BS_Iterative(binarySearchTree, 14);
BS_Iterative(binarySearchTree, 8);
}
private static void BS(Node node, int i) {
int nodeNum = node.getNum();
System.out.println("Num:"+nodeNum);
if(i < nodeNum) {
BS(node.getLeftNode(), i);
} else if (i > nodeNum) {
BS(node.getRightNode(), i);
} else {
System.out.println("Found");
}
}
private static void BS_Iterative(Node node, int i) {
int nodeNum = node.getNum();
System.out.println("Num:"+nodeNum);
while(nodeNum != i) {
if(i < nodeNum) {
node = node.getLeftNode();
} else {
node = node.getRightNode();
}
nodeNum = node.getNum();
System.out.println("Num:"+nodeNum);
}
System.out.println("Found");
}
}
The Output is:
Num:8
Num:3
Num:6
Num:7
Found
Num:8
Num:10
Num:14
Found
Num:8
Found
No comments:
Post a Comment