查找二叉树删除节点的操作

二叉查找树中,最复杂的操作就是删除操作。对于叶子节点,直接删除即可。对于一颗子树的节点,用子树取代原节点即可。对于拥有两颗子树的节点,首先用右子树最小的节点取代源节点,再递归删除此最小节点。

具体代码如下所示:

package com.Algorithm.Tree;
import java.util.*;
import java.io.*;
/*
 * author:Tammy Pi
 * function:二叉查找树
 */
public class BiSearchTree {

	public TreeNode root=null;
	private Scanner scanner=null;
	
	public BiSearchTree()
	{
		scanner=new Scanner(System.in);
	}
	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public Scanner getScanner() {
		return scanner;
	}
	public void setScanner(Scanner scanner) {
		this.scanner = scanner;
	}
	
    public void createTree()
    {
    	this.root=createTree(this.root);
    }
    public void midTraverse()
    {
    	midTraverse(this.root);
    }
    //中序遍历,相当于排序
    public void midTraverse(TreeNode root)
    {
    	if(root!=null)
    	{
    		midTraverse(root.getLchild());
    		System.out.print(root.getData()+" ");
    		midTraverse(root.getRchild());
    	}
    }
	//建立二叉查找树
	public TreeNode createTree(TreeNode root)
	{
		String str=scanner.next();
		int n=Integer.parseInt(str);
		
		for(int i=0;i<n;i++)
		{
			String data=scanner.next();
			if(root==null)
			{
				root=new TreeNode(data);
			}
			else 
			{
				TreeNode node=new TreeNode(data);
				TreeNode x=root,y=root;
				
				while(x!=null)
				{
					if(x.getData().compareTo(node.getData())>0)
					{
						y=x;
						x=x.getLchild();
					}
					else 
					{
						y=x;
						x=x.getRchild();
					}
				}
				if(y.getData().compareTo(node.getData())>0)
				{
					y.setLchild(node);
				}
				else
				{
					y.setRchild(node);
				}
			}
		}//for i
		
		return root;
	}//createTree
	//查找最小的节点
	public TreeNode findMin(TreeNode node)
	{
		TreeNode x=node;
		while(node!=null)
		{
			x=node;
			node=node.getLchild();
		}
		return x;
	}
	public void testRemove()
	{
		root.setRchild(remove(root.getRchild(),root.getRchild()));
		System.out.print("删除节点后,中序遍历结果为:");
	    midTraverse();
	}
	//删除节点
	//node为待删除节点
	public TreeNode remove(TreeNode node,TreeNode subTree)
	{
		if(subTree==null)
		{
			return null;
		}
		
		int state=node.getData().compareTo(subTree.getData());
		if(state<0)
		{
			node.setLchild(remove(node,subTree.getLchild()));
		}
		else if(state>0)
		{
			node.setRchild(remove(node,subTree.getRchild()));
		}
		else if(node.getLchild()!=null&&node.getRchild()!=null)
		{
			TreeNode temp=findMin(node.getRchild());
			node.setData(temp.getData());
			node.setRchild(remove(temp,node.getRchild()));
		}
		else
		{
			node=(node.getLchild()!=null?node.getLchild():node.getRchild());
		}
		return node;
	}
	//用于测试的主函数
	public static void main(String[] args)
	{
		BiSearchTree biTree=new BiSearchTree();
		biTree.createTree();
		System.out.println("查找二叉树建立完成!");
		System.out.print("中序遍历结果为:");
		biTree.midTraverse();
		System.out.println();
		biTree.testRemove();
	}
}
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/rongyongfeikai2/article/details/7474430
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞