public class BinaryTree {
Node head;
Node root;
Node senital = new Node('&');
Node candidate = null;
public BinaryTree() {
root = null;
// root.leftChild=null;
// root.rightChild=null;
}
public void insert(int data) {
if (root == null) {
root = new Node(data);
return;
}
insert(root, data);
}
private void insert(Node node, int data) {
if (node == null) {
node = new Node(data);
// node.leftChild = senital;
//node.rightChild = senital;
// System.out.println(data);
return;
}
if (data < node.data) {
if (node.leftChild == null) {
node.leftChild = new Node(data);
node.leftChild.parent = node;
} else {
insert(node.leftChild, data);
}
} else {
if (node.rightChild == null) {
node.rightChild = new Node(data);
node.rightChild.parent = node;
} else {
insert(node.rightChild, data);
}
}
}
}
No comments:
Post a Comment