java中二叉树的建立方法,及基本操作

//创建节点
package com.txt.c;

public class TreeNode {
	//结点的权
	int value;
	//左儿子
	TreeNode LeftNode;
	//右儿子
	TreeNode RightNode;
	public TreeNode(int value) {
		this.value = value;
	}
	public TreeNode(int value, TreeNode LeftNode, TreeNode RightNode) {
		this.value = value;
		this.LeftNode = LeftNode;
		this.RightNode = RightNode;
	}
	//设置左儿子
	public void setLeftNode(TreeNode LeftNode) {
		this.LeftNode=LeftNode;
	}
	//设置右儿子
	public void setRightNode(TreeNode RightNode) {
		this.RightNode=RightNode;
	}
	//前序遍历(递归)
	public void frontShow() {
		//遍历当前内容
		System.out.print("\t"+value);
		//左节点
		if(LeftNode!=null) {
			LeftNode.frontShow();
		}
		//右节点
		if(RightNode!=null) {
			RightNode.frontShow();
		}
	}
	//中序遍历
	public void midShow()
	{
				//左节点
				if(LeftNode!=null) {
					LeftNode.midShow();;
				}
				//遍历当前内容
				System.out.print("\t"+value);
				//右节点
				if(RightNode!=null) {
					RightNode.midShow();
				}
	}
	public void afterShow() {
		//左节点
		if(LeftNode!=null) {
			LeftNode.midShow();;
		}
		//右节点
		if(RightNode!=null) {
			RightNode.midShow();
		}
		//遍历当前内容
		System.out.print("\t"+value);
		
	}
	
	//前序查找,(中后序和遍历同理)
	public TreeNode frontSearch(int i) {
		TreeNode target=null;		
		//对比当前节点
		if(this.value==i)
			return this;
		else {
			//查找左儿子
			if(LeftNode!=null) {
				target=LeftNode.frontSearch(i);
			}
			//非空说明在左儿子
			if(target!=null) {
				return target;
			}
			//所找元素没在左儿子中,看右儿子,如果没有就是真的没有了
			if(RightNode!=null) {
				target=RightNode.frontSearch(i);
			}
		}
		return target;
	}
	
	

}

//创建树
package com.txt.c;

public class BinaryTree {
	TreeNode root;
	
	public BinaryTree(TreeNode root) {
		this.root = root;
	}

	public BinaryTree() {
	}
//获取根节点
	public TreeNode getRoot() {
		return root;
	}
//设置根节点
	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public void frontShow() {
		if(root!=null)//注意:没有节点无法调用节点方法
		    root.frontShow();
		
	}
	public void midShow() {
		if(root!=null)
		   root.midShow();
		
	}

	public void afterShow() {
		if(root!=null)
		     root.afterShow();
		
	}

	public TreeNode frontSearch(int i) {
		return root.frontSearch(i);
	}

}

//实例化二叉树
package com.txt.c;

public class TestBinaryTree {
	public static void main(String[] args) {
		//创建一棵树
		BinaryTree binTree=new BinaryTree();
		//创建一个根节点
		TreeNode root= new TreeNode(1);
		//把根节点赋给树
		binTree.setRoot(root);
		//创建左节点
		TreeNode rootL=new TreeNode(2);
		//把新建的节点设置为根的左子节点
		root.setLeftNode(rootL);
		//创建右节点
		TreeNode rootR=new TreeNode(3);
		//把新建的节点设置为根的右子节点
		root.setRightNode(rootR);
		//为第二层层的左节点创建两个新节点
		rootL.setLeftNode(new TreeNode(4));
		rootL.setRightNode(new TreeNode(5));
		//为第二层的右节点创建两个新节点
		rootR.setLeftNode(new TreeNode(6));
		rootR.setRightNode(new TreeNode(7));
		
		//前序遍历树遍历
		System.out.println("前序遍历:");
	    binTree.frontShow();
	    System.out.println("\n中序遍历:");
	    binTree.midShow();
	    System.out.println("\n后序遍历:");
	    binTree.afterShow();
	    
	    //前序查找
	    TreeNode result=binTree.frontSearch(5);
	    System.out.println("\n"+result);
	    
	    /*删除节点,删除节点为根节点或者为子类节点两种情况
         *只需将节点置空
         */	
	    
	}
	

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值