简单二叉搜索树的JAVA实现。

package dataStructure;
import java.util.*;

public class BinaryTree {
	public BinaryTree()
	{
		root = null;
	}
	public boolean search(int data)
	{
		TreeNode node = root;
		while(node != null)
		{
			if(node.data == data)
				return true;
			else if(node.data < data)
				node = node.rightChild;
			else
				node = node.leftChild;
		}
		return false;
	}
	//默认二叉搜索树中的所有元素都不相同。
	public void insert(int data)
	{
		TreeNode newNode = new TreeNode(data);
		if(root == null)
			root = newNode;
		else
		{
			TreeNode node = root;
			while(node != null)
			{
				if(data < node.data)
					if(node.leftChild == null)
					{
						node.leftChild = newNode;
						break;
					}
					else
						node = node.leftChild;
				else
					if(node.rightChild == null)
					{
						node.rightChild = newNode;
						break;
					}					
					else
						node = node.rightChild;
			}
		}
	}
	public boolean delete(int data)
	{
		if(root == null)
			return false;
		else
		{
			TreeNode current = root;
			TreeNode parent = root;
			boolean isLeftChild = true;//current是parent的左孩子?
			//寻找值为data的节点current。
			while(current.data != data)
			{
				parent = current;
				if(data < current.data)
				{
					current = current.leftChild;
					isLeftChild = true;
				}else
				{
					current = current.rightChild;
					isLeftChild = false;
				}
				if(current == null)
					return false;
			}
			TreeNode cur;//替换current节点。
			if(current.leftChild != null && current.rightChild != null)
			{
				//寻找current前驱节点,并用它代替current。
				TreeNode par = current.leftChild;
				cur = current.leftChild;
				while(cur.rightChild != null)
				{
					par = cur;
					cur = cur.rightChild;
				}
				if(par != cur)//意味着待删除的current节点的左孩子有子节点。
				{
					par.rightChild = cur.leftChild;
					cur.leftChild = current.leftChild;
				}
				cur.rightChild = current.rightChild;
			}
			else if(current.leftChild != null)//current右子树为空
				cur = current.leftChild;
			else//current左子树为空
				cur = current.rightChild;
			if(current == root)//删除的是根节点。
				root = cur;
			else
				if(isLeftChild)
					parent.leftChild = cur;
				else
					parent.rightChild = cur;
			return true;
		}
	}
	//遍历输出树节点。
	public void output()
	{
		display(root);
	}
	public void output2()//未递归。
	{
		display2(root);
	}
	//中序遍历
	private static void display(TreeNode node)
	{
		if(node != null)
		{
			display(node.leftChild);
			System.out.print(node.data+" ");
			display(node.rightChild);
		}
	}
	private static void display2(TreeNode node)
	{
		if(node != null)
		{
			Stack stack = new Stack();
			stack.push(node);
			TreeNode current = node;
			while(!stack.empty())
			{
				//若current的左孩子非空,则进栈。
				while(current.leftChild != null)
				{
					current = current.leftChild;
					stack.push(current);
				}
				System.out.print(((TreeNode)stack.pop()).data+" ");
				//若current没有有孩子,则输出栈元素。
				while(current.rightChild == null)
				{
					if(!stack.empty())
					{
						current = (TreeNode)stack.pop();
						System.out.print(current.data+" ");
					}else
						break;
				}
				//有右孩子,右孩子进栈。
				if(current.rightChild != null)
				{
					current = current.rightChild;
					stack.push(current);
				}
			}
		}
	}
	private TreeNode root;
}
class TreeNode
{
	public TreeNode(int data)
	{
		this.data = data;
		this.leftChild = null;
		this.rightChild = null;
	}
	public int data;
	public TreeNode leftChild;
	public TreeNode rightChild;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值