链式存储二叉树(创建,遍历,删除)

创建二叉树

链式存储二叉树
二叉数的五种形态
在这里插入图片描述

package BinaryTree;

public class 创建二叉树 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建一个树(空树),具体请看二叉树的五种形态
		BinaryTree binTree=new BinaryTree();
		//创建一个根节点
		TreeNode root=new TreeNode(1);
		//把根节点赋值给树
		binTree.setRoot(root);
		//创建两个节点
		TreeNode rootL=new TreeNode(2);
		//把新创建的节点设置为根节点的子节点
		root.setlNode(rootL);
		//创建两个节点
		TreeNode rootR=new TreeNode(3);
		//把新创建的节点设置为根节点的子节点
		root.setrNode(rootR);
		
	}

}


package BinaryTree;

public class TreeNode {
	int value;//节点的权
	TreeNode lNode;//左儿子
	TreeNode rNode;//右儿子
	public TreeNode(int value){
		this.value=value;
	}
	//设置左儿子
	public void setlNode(TreeNode lNode){
		this.lNode=lNode;
	}
	//设置右儿子
	public void setrNode(TreeNode rNode){
		this.rNode=rNode;
	}	
}

package BinaryTree;

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

}

遍历二叉树

有点错误,但不知道在哪了

package BinaryTree;

public class 创建二叉树 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建一个树(空树),具体请看二叉树的五种形态
		BinaryTree binTree=new BinaryTree();
		//创建一个根节点
		TreeNode root=new TreeNode(1);
		//把根节点赋值给树
		binTree.setRoot(root);
		//创建两个节点
		TreeNode rootL=new TreeNode(2);
		//把新创建的节点设置为根节点的子节点
		root.setlNode(rootL);
		//创建两个节点
		TreeNode rootR=new TreeNode(3);
		//把新创建的节点设置为根节点的子节点
		root.setrNode(rootR);
		//为第二层的左节点创建两个子节点
		rootL.setlNode(new TreeNode(4));
		rootL.setrNode(new TreeNode(5));
		//为第二层的右节点创建两个子节点
		rootR.setlNode(new TreeNode(6));
		rootR.setrNode(new TreeNode(7));
		//前序遍历
		binTree.fronShow();
		System.out.println();
		//中序遍历
		binTree.midShow();
		System.out.println();
		//后续遍历
		binTree.afterShow();
		System.out.println();
		//前序查找
		TreeNode result=binTree.frontSearch(3);
		System.out.println(result==rootR);
	}

}

```java
package BinaryTree;

public class TreeNode {
	int value;//节点的权
	TreeNode lNode;//左儿子
	TreeNode rNode;//右儿子
	public TreeNode(int value){
		this.value=value;
	}
	//设置左儿子
	public void setlNode(TreeNode lNode){
		this.lNode=lNode;
	}
	//设置右儿子
	public void setrNode(TreeNode rNode){
		this.rNode=rNode;
	}
	
	//前序遍历
	public void frontShow() {
		// TODO Auto-generated method stub
		//先遍历当前节点的内容
		System.out.print(value+" ");
		//左节点
		if(lNode!=null){
		lNode.frontShow();
		}
		//右节点
		if(rNode!=null){
			rNode.frontShow();
		}
	}
	public void midShow() {
		// TODO Auto-generated method stub
		//左节点
		if(lNode!=null){
			lNode.midShow();
		}
		System.out.print(value+" ");
		if(rNode!=null){
			rNode.midShow();
		}		
	}
	public void afterShow() {
		// TODO Auto-generated method stub
		if(lNode!=null){
			lNode.afterShow();
		}
		if(rNode!=null){
			rNode.afterShow();
		}
		System.out.print(value+" ");
	}	
	//前序查找
	public TreeNode frontSearch(int i){
		TreeNode target=null;
		//对比当前节点的值
		if(this.value==1){
			return this;
		//当前节点的值不是要查找的节点
		}else{
			//查找左儿子
			if(lNode!=null){
				//可能查找,也可能找不到,查不到,target还是一个null
				target=lNode.frontSearch(i);
			}
			//如果不为空,说明在左儿子中已经找到
			if(target!=null){
				return target;
			}
			//查找右儿子
			if(rNode!=null){
				target=rNode.frontSearch(i);
			}
		}
		//都找不到就返回
		return null;
	}
}

package BinaryTree;

public class BinaryTree {
	TreeNode root;
	//设置根节点
	public TreeNode getRoot() {
		return root;
	}
	//获取根节点
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public void fronShow() {
		// TODO Auto-generated method stub
		root.frontShow();
	}
	public void midShow() {
		// TODO Auto-generated method stub
		root.midShow();
	}
	public void afterShow() {
		// TODO Auto-generated method stub
		root.afterShow();
	}
	public TreeNode frontSearch(int i) {
		// TODO Auto-generated method stub
		return root.frontSearch(i);
	}
	

}


## 删除节点

```java
package BinaryTree;

public class 创建二叉树 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建一个树(空树),具体请看二叉树的五种形态
		BinaryTree binTree=new BinaryTree();
		//创建一个根节点
		TreeNode root=new TreeNode(1);
		//把根节点赋值给树
		binTree.setRoot(root);
		//创建两个节点
		TreeNode rootL=new TreeNode(2);
		//把新创建的节点设置为根节点的子节点
		root.setlNode(rootL);
		//创建两个节点
		TreeNode rootR=new TreeNode(3);
		//把新创建的节点设置为根节点的子节点
		root.setrNode(rootR);
		//为第二层的左节点创建两个子节点
		rootL.setlNode(new TreeNode(4));
		rootL.setrNode(new TreeNode(5));
		//为第二层的右节点创建两个子节点
		rootR.setlNode(new TreeNode(6));
		rootR.setrNode(new TreeNode(7));
		//前序遍历
		binTree.frontShow();
		System.out.println();
		//中序遍历
		binTree.midShow();
		System.out.println();
		//后续遍历
		binTree.afterShow();
		System.out.println();
		//前序查找
		TreeNode result=binTree.frontSearch(3);
		System.out.println(result==rootR);
		//删除一个子树
		binTree.delete(4);
		binTree.frontShow();
	}

}


package BinaryTree;

public class TreeNode {
	int value;//节点的权
	TreeNode lNode;//左儿子
	TreeNode rNode;//右儿子
	public TreeNode(int value){
		this.value=value;
	}
	//设置左儿子
	public void setlNode(TreeNode lNode){
		this.lNode=lNode;
	}
	//设置右儿子
	public void setrNode(TreeNode rNode){
		this.rNode=rNode;
	}
	
	//前序遍历
	public void frontShow() {
		// TODO Auto-generated method stub
		//先遍历当前节点的内容
		System.out.print(value+" ");
		//左节点
		if(lNode!=null){
		lNode.frontShow();
		}
		//右节点
		if(rNode!=null){
			rNode.frontShow();
		}
	}
	public void midShow() {
		// TODO Auto-generated method stub
		//左节点
		if(lNode!=null){
			lNode.midShow();
		}
		System.out.print(value+" ");
		if(rNode!=null){
			rNode.midShow();
		}		
	}
	public void afterShow() {
		// TODO Auto-generated method stub
		if(lNode!=null){
			lNode.afterShow();
		}
		if(rNode!=null){
			rNode.afterShow();
		}
		System.out.print(value+" ");
	}	
	//前序查找
	public TreeNode frontSearch(int i){
		TreeNode target=null;
		//对比当前节点的值
		if(this.value==1){
			return this;
		//当前节点的值不是要查找的节点
		}else{
			//查找左儿子
			if(lNode!=null){
				//可能查找,也可能找不到,查不到,target还是一个null
				target=lNode.frontSearch(i);
			}
			//如果不为空,说明在左儿子中已经找到
			if(target!=null){
				return target;
			}
			//查找右儿子
			if(rNode!=null){
				target=rNode.frontSearch(i);
			}
		}
		//都找不到就返回
		return null;
	}
	//删除一棵子树
	public void delete(int i) {
		// TODO Auto-generated method stub
		TreeNode parent=this;
		//判断左儿子
		if(parent.lNode!=null&&parent.lNode.value==i){
			parent.lNode=null;
			return;
		}
		//判断右儿子
		if(parent.rNode!=null&&parent.rNode.value==i){
			parent.rNode=null;
			return;
		}
		//如果都不是,递归检查并删除左儿子
		parent=lNode;
		if(parent!=null){
			parent.delete(i);
		}
		//递归检查并删除右儿子
		parent=rNode;
		if(parent!=null){
			parent.delete(i);
		}
	}
}

package BinaryTree;

public class BinaryTree {
	TreeNode root;
	//设置根节点
	public TreeNode getRoot() {
		return root;
	}
	//获取根节点
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public void frontShow() {
		// TODO Auto-generated method stub
		if(root!=null){
		root.frontShow();
		}
	}
	public void midShow() {
		// TODO Auto-generated method stub
		if(root!=null){
		root.midShow();
		}
	}
	public void afterShow() {
		// TODO Auto-generated method stub
		if(root!=null){
		root.afterShow();
		}
	}
	public TreeNode frontSearch(int i) {
		// TODO Auto-generated method stub
		return root.frontSearch(i);
	}
	public void delete(int i) {
		// TODO Auto-generated method stub
		if(root.value==i){
			root=null;
		}else{
			root.delete(i);
		}
	}
	

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向上Claire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值