Java二叉树的实现

结点:

首先我们以这个二叉树图为例讲解:

 

我们主要来看看二叉树的遍历,求树的高度,结点总数。这三个方法实现很简单,就是递归。

思想:

二叉树的遍历:先序遍历,中序遍历,后序遍历。框架一样,只不过输出的位置有差异

	//先序遍历
	public void preOrderVisit() {
		System.out.print("" + value + " ");
		if(leftChild != null) {
			leftChild.preOrderVisit();
		}// Of if
		if(rightChild != null) {
			rightChild.preOrderVisit();
		}//Of if
	}// Of preOrderVisit
	
	//中序遍历
	public void inOrderVisit() {
		if(leftChild != null) {
			leftChild.inOrderVisit();
		}// Of if
		
		System.out.print("" + value + " ");
		
		if(rightChild != null) {
			rightChild.inOrderVisit();
		}// Of if
	}// Of inOrderVisit
	
	//后序遍历
	public void postOrderVisit() {
		if(leftChild != null) {
			leftChild.postOrderVisit();
		}// Of if
		
		if(rightChild != null) {
			rightChild.postOrderVisit();
		}// Of if
		System.out.print("" + value + " ");
	}// Of postOrderVisit

树的高度:无非就是比较左右孩子高度,返回高的哪个

public int getDepth() {
		// It is a leaf
		if((leftChild == null)&&(rightChild == null)) {
			return 1;
		}// Of if
		
		// The depth of the left child.
		int tempLeftDepth = 0;
		if(leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		}// Of if
		
		// The depth of the right child.
		int tempRightDepth = 0;
		if(rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		}// Of if
		
		// 树的高度选取最高的那边+1
		if(tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth+1;
		}
		else {
			return tempRightDepth+1;
		}// Of if
	}// Of getDepth

结点总数:顾名思义,就是左右孩子数之和嘛。

	//获得结点数目
	public int getNumNodes() {
		// If it is a leaf.
		if(leftChild == null && rightChild == null) {
			return 1;
		}// Of if
		
		//左孩子的数目
		int templeftNodes = 0;
		if(leftChild != null) {
			templeftNodes = leftChild.getNumNodes();
		}// Of if
		
		//友孩子的数目
		int tempRightNodes = 0;
		if(rightChild != null)
		{
			tempRightNodes = rightChild.getNumNodes();
		}// Of if
		
		// 总的节点数 = 左孩子数目 + 右孩子数目
		return templeftNodes+tempRightNodes + 1;
	}// Of getNumNodes

总的代码:

/**
 * 
 */
package datastructure.tree;
import java.util.Arrays;
	 /**
	 ****************************
 	 * TODO
 	 * @author Chen Fan
 	 * @version 1.0
 	 * time 2021年12月30日
 	 ****************************
 	 */
public class BinaryCharTree {
	char value;
	BinaryCharTree leftChild;
	BinaryCharTree rightChild;
	
	// 第一个构造函数
	public BinaryCharTree(char paraName) {
		value = paraName;
		leftChild = null;
		rightChild = null;		
	}// Of the constructor
	
	// 
	public static BinaryCharTree manualConstructTree() {
		//第一步构造一个根节点
		BinaryCharTree resultTree = new BinaryCharTree('a');
		
		//第二步构造所有结点。
		BinaryCharTree tempTreeB = new BinaryCharTree('b');
		BinaryCharTree tempTreeC = new BinaryCharTree('c');
		BinaryCharTree tempTreeD = new BinaryCharTree('d');
		BinaryCharTree tempTreeE = new BinaryCharTree('e');
		BinaryCharTree tempTreeF = new BinaryCharTree('f');
		BinaryCharTree tempTreeG = new BinaryCharTree('g');
		
		//第三步,连接所有节点
		resultTree.leftChild = tempTreeB;
		resultTree.rightChild = tempTreeC;
		tempTreeB.leftChild = tempTreeD;
		tempTreeC.rightChild = tempTreeE;
		tempTreeD.leftChild = tempTreeF;
		tempTreeD.rightChild = tempTreeG;
		return resultTree;
	}// Of manualConstructTree
	
	//先序遍历
	public void preOrderVisit() {
		System.out.print("" + value + " ");
		if(leftChild != null) {
			leftChild.preOrderVisit();
		}// Of if
		if(rightChild != null) {
			rightChild.preOrderVisit();
		}//Of if
	}// Of preOrderVisit
	
	//中序遍历
	public void inOrderVisit() {
		if(leftChild != null) {
			leftChild.inOrderVisit();
		}// Of if
		
		System.out.print("" + value + " ");
		
		if(rightChild != null) {
			rightChild.inOrderVisit();
		}// Of if
	}// Of inOrderVisit
	
	//后序遍历
	public void postOrderVisit() {
		if(leftChild != null) {
			leftChild.postOrderVisit();
		}// Of if
		
		if(rightChild != null) {
			rightChild.postOrderVisit();
		}// Of if
		System.out.print("" + value + " ");
	}// Of postOrderVisit
	
	//得到树的高度
	public int getDepth() {
		// It is a leaf
		if((leftChild == null)&&(rightChild == null)) {
			return 1;
		}// Of if
		
		// The depth of the left child.
		int tempLeftDepth = 0;
		if(leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		}// Of if
		
		// The depth of the right child.
		int tempRightDepth = 0;
		if(rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		}// Of if
		
		// 树的高度选取最高的那边+1
		if(tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth+1;
		}
		else {
			return tempRightDepth+1;
		}// Of if
	}// Of getDepth
	
	//获得结点数目
	public int getNumNodes() {
		// If it is a leaf.
		if(leftChild == null && rightChild == null) {
			return 1;
		}// Of if
		
		//左孩子的数目
		int templeftNodes = 0;
		if(leftChild != null) {
			templeftNodes = leftChild.getNumNodes();
		}// Of if
		
		//友孩子的数目
		int tempRightNodes = 0;
		if(rightChild != null)
		{
			tempRightNodes = rightChild.getNumNodes();
		}// Of if
		
		// 总的节点数 = 左孩子数目 + 右孩子数目
		return templeftNodes+tempRightNodes + 1;
	}// Of getNumNodes
	
	// 主函数
	public static void main(String args[]) {
		BinaryCharTree tempTree = manualConstructTree();
		System.out.println("\r\nPreorder visit:");
		tempTree.preOrderVisit();
		System.out.println("\r\nIn-order visit:");
		tempTree.inOrderVisit();
		System.out.println("\r\nPost-order visit:");
		tempTree.postOrderVisit();

		System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
		System.out.println("The number of nodes is: " + tempTree.getNumNodes());
	}// Of main
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值