【算法系列-4】二叉树

j结点类;

public class Node {
	public int id;
	public String name;
	
	public Node leftChild;
	public Node rightChild;
	
	public Node(int id, String name){
		this.id = id;
		this.name = name;
	}
	
	public void display(){
		System.out.println("id-->" + id + ", name-->" + name);
	}

}

主类:

public class Tree {
	private Node root;
	public boolean isEmpty(){
		if(null == root)
			return true;
		return false;
	}
	
	public void insert(int id, String name){
		Node node = new Node(id, name);
		boolean isLeftChild = false;
		
		if(null == root){
			root = node;
		}else{
			Node parent = root;
			Node current = root;
			while(null != current){
				parent = current;
				if(current.id > id){
					current = current.leftChild;
					isLeftChild = true;
				}else{
					current = current.rightChild;
					isLeftChild = false;
				}
			}
			if(isLeftChild){
				parent.leftChild = node;
			}else{
				parent.rightChild = node;
			}
		}
	}
	
	public Node find(int id){
		if(isEmpty())
			return null;
		Node current = root;
		while(null != current){
			if(current.id == id){
				return current;
			}else if(current.id > id){
				current = current.leftChild;
			}else{
				current = current.rightChild;
			}
		}
		return null;
	}
	
	public boolean delete(int id){
		if(isEmpty())
			return false;

		Node parent = root;
		Node current = root;
		boolean isLeftChild = true;
		
		while(current.id != id){
			if(null == current)
				return false;
			
			parent = current;
			if(current.id < id){
				current = current.leftChild;
				isLeftChild = true;
			}else{
				current = current.rightChild;
				isLeftChild = false;
			}
			
		}
		/*1. none child*/
		if(null == current.leftChild && null == current.rightChild){
			if(current == root){
				current = null;
			}else if(isLeftChild){
				parent.leftChild = null;
			}else{
				parent.rightChild = null;
			}
		}else if(null == current.leftChild){
			if(root == current){
				root = current.rightChild;
			}else if(isLeftChild){
				parent.leftChild = current.rightChild;
			}else{
				parent.rightChild = current.rightChild;
			}
		}else if(null == current.rightChild){
			if(root == current){
				root = current.leftChild;
			}else if(isLeftChild){
				parent.leftChild = current.leftChild;
			}else{
				parent.rightChild = current.leftChild;
			}
		}else{
			/*待删除节点有两个孩子*/
			Node successor = getSuccessor(current);
			
			if(root == current){
				root = successor;
			}else if(isLeftChild){
				parent.leftChild = successor;
			}else{
				parent.rightChild = successor;
			}
			successor.leftChild = current.leftChild;
		}
		
		return true;
	}
	
	/**
	 * 返回中序遍历的后续节点
	 * @param delNode
	 * @return
	 */
	private Node getSuccessor(Node delNode){
		Node successorParent = delNode;
		Node successor = delNode;
		Node current = delNode.rightChild;
		
		while(null != current){
			successorParent = successor;
			successor = current;
			current = current.leftChild;
		}
		
		if(successor != delNode.rightChild){
			successorParent.leftChild = successor.rightChild;
			successor.rightChild = delNode.rightChild;
		}
		
		return successor;
	}
	
	/*
	 * 中序遍历
	 */
	public void inOrder(Node node){
		if(null == node)
			return;
		inOrder(node.leftChild);
		node.display();
		inOrder(node.rightChild);
	}
	
	public void display(){
		inOrder(root);
	}
	
	public static void main(String[] args){
		Tree tree = new Tree();
		tree.insert(1, "Jordan");
		tree.insert(2, "James");
		tree.insert(3, "kobe");
		tree.insert(5, "wade");
		tree.insert(10, "curry");
		
		tree.display();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值