package p30;
public class Hq_P30 {
public TreeNode root;
public Hq_P30(TreeNode root) {
this.root = root;
}
public Hq_P30() {
}
public TreeNode put(int data) {
TreeNode node = null;
TreeNode parent = null;
if (root == null) {
node = new TreeNode(0, data);
root = node;
return node;
}
node = root;
while (node != null) {
parent = node;
if (data > node.data) {
node = node.RightChild;
} else if (data < node.data) {
node = node.LeftChild;
} else {
return node;
}
}
node = new TreeNode(0, data);
if (data < parent.data) {
parent.LeftChild = node;
node.parent = node;
} else{
parent.RightChild = node;
node.parent = parent;
}
return node;
}
public TreeNode del(TreeNode node, int key) {
TreeNode sNode = searchNode(node, key);
if (sNode == null) {
return node;
}else if (sNode.LeftChild == null && sNode.RightChild == null) {
if (sNode.parent.LeftChild.data == key) {
sNode.parent.LeftChild = null;
} else {
sNode.parent.RightChild = null;
}
return node;
} else if (sNode.LeftChild != null || sNode.RightChild != null) {
if (sNode.LeftChild != null && sNode.RightChild == null) {
if (sNode.parent.LeftChild.data == key) {
sNode.parent.LeftChild = sNode.LeftChild;
sNode.LeftChild.parent = sNode.parent;
} else {
sNode.parent.RightChild = sNode.LeftChild;
sNode.LeftChild.parent = sNode.parent;
}
return node;
} else if (sNode.LeftChild == null && sNode.RightChild != null) {
if (sNode.parent.LeftChild.data == key) {
sNode.parent.LeftChild = sNode.RightChild;
sNode.RightChild.parent = sNode.parent;
} else {
sNode.parent.RightChild = sNode.RightChild;
sNode.RightChild.parent = sNode.parent;
}
return node;
} else {
TreeNode tempNode = sNode.LeftChild;
while (tempNode != null) {
tempNode = tempNode.RightChild;
}
if (sNode.parent.LeftChild.data == key) {
tempNode.parent.RightChild = null;
sNode.parent.LeftChild = tempNode;
tempNode.LeftChild = sNode.LeftChild;
tempNode.parent = sNode.parent;
} else {
tempNode.parent.RightChild = null;
sNode.parent.RightChild = tempNode;
tempNode.LeftChild = sNode.LeftChild;
tempNode.parent = sNode.parent;
}
return node;
}
}
return node;
}
public TreeNode searchNode(TreeNode node, int key) {
if (node == null) {
return null;
}
while (node != null) {
if (key > node.data) {
node = node.RightChild;
} else if (key < node.data) {
node = node.LeftChild;
}else {
return node;
}
}
return null;
}
public void minOrder(TreeNode node) {
if (node == null) {
return;
} else {
minOrder(node.LeftChild);
System.out.print(node.data + " ");
minOrder(node.RightChild);
}
}
class TreeNode {
private int index;
private int data;
private TreeNode LeftChild;
private TreeNode RightChild;
private TreeNode parent;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public TreeNode getLeftChild() {
return LeftChild;
}
public void setLeftChild(TreeNode leftChild) {
LeftChild = leftChild;
}
public TreeNode getRightChild() {
return RightChild;
}
public void setRightChild(TreeNode rightChild) {
RightChild = rightChild;
}
public TreeNode getParent() {
return parent;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public TreeNode(int index, int data) {
super();
this.index = index;
this.data = data;
this.LeftChild = null;
this.parent = null;
this.RightChild = null;
}
}
}