java实现链式二叉树

//树的节点
class TreeNode{	
	
	int value;
	TreeNode LeftNode;
	TreeNode RightNode;
	
	public TreeNode(int value) {
		this.value = value;
	}
	
	//设置左儿子
	public void setLeftNode(TreeNode LeftNode) {
		this.LeftNode = LeftNode;
	}
	
	//设置右儿子  
	public void setRightNode(TreeNode RightNode) {
		this.RightNode = RightNode;
	}
	
	//前序遍历
	public void frontShow() {
		System.out.print(value);
		if (LeftNode != null) {
			LeftNode.frontShow();
		}
		if (RightNode != null) {
			RightNode.frontShow();
		}
	}
	
	//中序遍历
	public void midShow() {
		if (LeftNode != null) {
			LeftNode.midShow();
		}
		System.out.print(value);
		if (RightNode != null) {
			RightNode.midShow();
		}
	}
	
	//后序遍历
	public void afterShow() {
		if (LeftNode != null) {
			LeftNode.afterShow();
		}
		if (RightNode != null) {
			RightNode.afterShow();
		}
		System.out.print(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;
	}
	
	//删除一颗子树
	public void delete(int i) {
		 TreeNode parent = this;
		 if (parent.LeftNode != null && parent.LeftNode.value == i) {
			 parent.LeftNode = null;
			 return;
		 }
		 if (parent.RightNode != null && parent.RightNode.value == i) {
			 parent.RightNode = null;
			 return;
		 }
		 parent = LeftNode;
		 if (parent != null) {
			 parent.delete(i);
		 }
		 parent = RightNode;
		 if (parent != null) {
			 parent.delete(i);
		 }
	}	
}

public class BinaryTree {
	
	TreeNode root;
	
	//设置根节点
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	
	//返回根节点
	public TreeNode getRoot() {
		return 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);
	}
	
	//删除一颗子树
	public void delete(int i) {
		if (root.value == i) {
			root = null;
		} else {
			root.delete(i);
		}
	}	
}

前序遍历
1.先输出根节点
2.在看左子树递归输出
3.再看右子树递归输出

前序查找
1.如果查找的节点是根节点那么直接返回
2.左子树不为空查左子树
3.左子树没有且右子树不为空查右子数

删除子树
1.正好是根节点直接设为null
2.将这个节点设置成parent
3.左节点不为空的情况下恰好是i那么设为null
4.右节点不为空的情况下恰好是i那么设为null
5.在分别递归查找进行删除

测试

public static void main(String args[]) {
	BinaryTree binTree = new BinaryTree();
	TreeNode root = new TreeNode(1);
	binTree.setRoot(root);
	TreeNode rootL1 = new TreeNode(2);
	TreeNode rootR1 = new TreeNode(3);
	root.setLeftNode(rootL1);
	root.setRightNode(rootR1);
	TreeNode rootL2 = new TreeNode(4);
	TreeNode rootR2 = new TreeNode(5);
	TreeNode rootL3 = new TreeNode(6);
	TreeNode rootR3 = new TreeNode(7);
	rootL1.setLeftNode(rootL2);
	rootL1.setRightNode(rootR2);
	rootR1.setLeftNode(rootL3);
	rootR1.setRightNode(rootR3);
	System.out.print("前序遍历:");
	binTree.frontShow();
	System.out.println(" ");
	System.out.print("中序遍历:");
	binTree.midShow();
	System.out.println("  ");
	System.out.print("后序遍历:");
	binTree.afterShow();
	System.out.println("  ");
	System.out.print("前序查找:");
	TreeNode result = binTree.frontSearch(6);
	System.out.println(result);
	binTree.delete(3);
	System.out.print("删除3之后的前序遍历:");
	binTree.frontShow();
}

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 基于链表实现树结构 */ package dsa; public class TreeLinkedList implements Tree { private Object element;//树根节点 private TreeLinkedList parent, firstChild, nextSibling;//父亲、长子及最大的弟弟 //(单节点树)构造方法 public TreeLinkedList() { this(null, null, null, null); } //构造方法 public TreeLinkedList(Object e, TreeLinkedList p, TreeLinkedList c, TreeLinkedList s) { element = e; parent = p; firstChild = c; nextSibling = s; } /*---------- Tree接口中各方法的实现 ----------*/ //返回当前节点中存放的对象 public Object getElem() { return element; } //将对象obj存入当前节点,并返回此前的内容 public Object setElem(Object obj) { Object bak = element; element = obj; return bak; } //返回当前节点的父节点;对于根节点,返回null public TreeLinkedList getParent() { return parent; } //返回当前节点的长子;若没有孩子,则返回null public TreeLinkedList getFirstChild() { return firstChild; } //返回当前节点的最大弟弟;若没有弟弟,则返回null public TreeLinkedList getNextSibling() { return nextSibling; } //返回当前节点后代元素的数目,即以当前节点为根的子树的规模 public int getSize() { int size = 1;//当前节点也是自己的后代 TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 size += subtree.getSize();//累加 subtree = subtree.getNextSibling();//所有孩子的后代数目 } return size;//即可得到当前节点的后代总数 } //返回当前节点的高度 public int getHeight() { int height = -1; TreeLinkedList subtree = firstChild;//从长子开始 while (null != subtree) {//依次 height = Math.max(height, subtree.getHeight());//在所有孩子中取最大高度 subtree = subtree.getNextSibling(); } return height+1;//即可得到当前节点的高度 } //返回当前节点的深度 public int getDepth() { int depth = 0; TreeLinkedList p = parent;//从父亲开始 while (null != p) {//依次 depth++; p = p.getParent();//访问各个真祖先 } return depth;//真祖先的数目,即为当前节点的深度 } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值