package BinarySearchTree;
import java.util.Stack;
public class BinarySearchTree {
public Node root;
public void inorder_iterator(Node node) {
if (node.leftchild != null) {
this.inorder_iterator(node.leftchild);
}
System.out.println(node.id + " ");
if (node.rightchild != null) {
this.inorder_iterator(node.rightchild);
}
}
public void preorder_iterator(Node node ) {
System.out.println(node.id + " ");
if(node.leftchild != null) {
this.preorder_iterator(node.leftchild);
}
if(node.rightchild != null) {
this.preorder_iterator(node.rightchild);
}
}
public void postorder_iterator(Node node) {
if (node.leftchild != null) {
postorder_iterator(node.leftchild);
}
if (node.rightchild != null) {
postorder_iterator(node.rightchild);
}
System.out.println(node.id);
}
public void nonre_preorder_iterator(Node node){
if(node == null) {
return;
}
Stack st = new Stack();
st.push(node);
while (st.empty() != true) {
Node curr = (Node) st.pop();
System.out.println(curr.id);
if (curr.rightchild != null) {
st.push(curr.rightchild);
}
if (curr.leftchild != null) {
st.push(curr.leftchild);
}
}
}
public int min_node() {
if (root == null) {
System.out.println("root not exists");
return 0;
}
if (root.leftchild == null) {
return root.id;
}
Node curr = root.leftchild;
while (curr.leftchild != null) {
curr = curr.leftchild;
}
return curr.id;
}
public int min(Node node) {
Node min_code = min_node(node);
return min_code.id;
}
private Node min_node(Node node) {
if (node == null) {
return null;
}
if (node.leftchild == null) {
return node;
}
if (node.leftchild != null) {
return min_node(node.leftchild);
}
return node;
}
public int max_node() {
if(root==null) {
return 0;
}
if(root.rightchild==null) {
return root.id;
}
Node cur = root.rightchild;
while(cur.rightchild!=null) {
cur = cur.rightchild;
}
return cur.id;
}
public Node find(int key) {
if (root == null) {
System.out.println("The tree is empty");
return null;
}
Node current = root;
while (current.id != key) {
if (key > current.id)
current = current.rightchild;
else
current = current.leftchild;
if (current == null)
return null;
}
return current;
}
public boolean insert (Node node) {
if(root == null) {
root = node;
return true;
}
if(this.find(node.id) != null) {
System.out.println(node.id + " has already existed!");
return false;
}
Node current = root;
while(current != null) {
if(node.id > current.id) {
if(current.rightchild == null) {
current.rightchild = node;
return true;
}
current = current.rightchild;
}
if(node.id < current.id) {
if(current.leftchild == null) {
current.leftchild = node;
return true;
}
current = current.leftchild;
}
}
return false;
}
public void delete_min(Node node) {
if (find(node.id)==null) {
System.out.println("Root node not existing!");
}
Node cur = node;
while (cur.leftchild!= null) {
cur = cur.leftchild;
}
}
public boolean delete(int key) {
if (root ==null) {
System.out.println("The tree is empty");
return false;
}
Node targetParent = root;
Node target = root;
boolean isLeftChild = true;
while(target.id != key) {
if(key > target.id) {
targetParent = target;
target = target.rightchild;
isLeftChild = false;
}
else {
targetParent = target;
target = target.leftchild;
isLeftChild = true;
}
if (target == null)
break;
}
if (target == null)
return false;
if (target.leftchild==null && target.rightchild==null) {
if(target.id==root.id) {
root=null;
return true;
}
if (isLeftChild)
targetParent.leftchild = null;
else
targetParent.rightchild = null;
}
else if(target.leftchild == null && target.rightchild != null) {
if (key == root.id) {
root = root.rightchild;
}
if (isLeftChild) {
targetParent.leftchild = target.rightchild;
}
else
targetParent.rightchild = target.rightchild;
}
else if(target.leftchild != null && target.rightchild == null) {
if (key == root.id) {
root = root.leftchild;
}
if (isLeftChild) {
targetParent.leftchild = target.leftchild;
}
else
targetParent.rightchild = target.leftchild;
}
else {
Node followingNode = this.getFollowingNode(target);
if (target.id == root.id) {
root = followingNode;
}
else if (isLeftChild) {
targetParent.leftchild = followingNode;
}
else {
targetParent.rightchild = followingNode;
followingNode.leftchild = targetParent.leftchild;
followingNode.rightchild = targetParent.rightchild;
}
}
return true;
}
private Node getFollowingNode(Node node2Del){
Node nodeParent = node2Del;
Node node = node2Del.rightchild;
while (node.leftchild != null) {
nodeParent = node;
node = node.leftchild;
}
if(node.id != node2Del.rightchild.id)
nodeParent.leftchild = node.rightchild;
else
nodeParent.rightchild = node.rightchild;
return node;
}
public static void main(String[] args) {
BinarySearchTree BST = new BinarySearchTree();
Node t1 = new Node (20, "first node");
Node t2 = new Node (10, "second node");
Node t3 = new Node (30, "third node");
Node t4 = new Node (40, "forth node");
BST.insert(t1);
BST.insert(t2);
BST.insert(t3);
BST.insert(t4);
BST.inorder_iterator(BST.root);
System.out.println("以下是先序遍歷");
BST.preorder_iterator(BST.root);
System.out.println("以下是後序遍歷");
BST.postorder_iterator(BST.root);
System.out.println("以下是non-recursion iterator");
BST.nonre_preorder_iterator(BST.root);
System.out.println("以下是minNode");
System.out.println(BST.min_node());
System.out.println(BST.max_node());
System.out.println(BST.min(BST.root));
BST.delete(10);
BST.delete(20);
System.out.println(BST.min_node());
}
}