BST

package BinarySearchTree;

import java.util.Stack;

public class BinarySearchTree {
	
	public Node root; //初始化 Node class
	
//	public Node find(int key) { //創建method "find" with data type being Node
//		
//	}
	
	//中序遍歷
	public void inorder_iterator(Node node) {
		if (node.leftchild != null) {  //node.leftchild = null <--node的左節支為空
			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);
	}
	
	//non-recursion preorder_iterator
	public void nonre_preorder_iterator(Node node){
		if(node == null) {   //如果node為空 則return nothing
			return;
		}
		//step 1
		Stack st = new Stack();
		//step 2
		st.push(node);
		
		while (st.empty() != true) { //當stack不是空的時侯進行以下循環
		//step 3
		Node curr = (Node) st.pop(); //cast st.pop() 的數據轉化為 Node type
		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;   
	}
	
	//recursion min
	
	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) {   //當根節點為空時, 新插入的node成為根節點
 			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) {   //如果要插入的node.id大過根點
				if(current.rightchild == null) {
					current.rightchild = node;
					return true;
				}
				current = current.rightchild; //如果current的右節點不為空, 把當前節點的右節點作為節點
			}
			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) {              //如果BST為空
			System.out.println("The tree is empty");
			return false;
		}
		Node targetParent = root;   //初始化記錄父節點和子節點的node
		Node target = root;
		boolean isLeftChild = true;  //boolean來記錄是否左子節點
		while(target.id != key) {	//當要delete的id和current id不同時進行的循環
			if(key > target.id) {		//如key 大於現時的節點id時, 把現時的節點id移動至右子節點
				targetParent = target;
				target = target.rightchild;
				isLeftChild = false;
			}
			else {						//如key 小於現時的節點id時, 把現時的節點id移動至左節點
				targetParent = target;
				target = target.leftchild;
				isLeftChild = true;
			}
			if (target == null)		//此時如果移動至一個null的node時, 中止循環
				break;
		}
		if (target == null)    //找不到該節點
			return false;
		//after while loop, 現時的節點是target, 其父節點為targetParent
		
		//處理第一個情況: 節點沒有左右子節點
		if (target.leftchild==null && target.rightchild==null) {
			if(target.id==root.id) { //如現時的target節點在root節點位置, 將root設定為null
				root=null;
				return true;  //return true 發出delete function已被成功執行訊息
			}
			if (isLeftChild)  //isLeftChild是用來記錄該節點是否左或右子節點
				targetParent.leftchild = null;  //把要被delete節點的parent節點與被delete節點的連接delete 
			
			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) {   //when當時節點為root時
				root = root.leftchild; //root節點的左子點取代root成為新的root節點
				
			}
			if (isLeftChild) {
				targetParent.leftchild = target.leftchild;
			}
			else
				targetParent.rightchild = target.leftchild;
		}
		//被delete節點有左右兩點, 先找到後續節點, 再將後續節點插入被delete節點(target) 的位置
		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) {
		// TODO Auto-generated method stub
		
		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());
		
	}
	
	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值