栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

树与二叉树——二叉树的深度遍历

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

树与二叉树——二叉树的深度遍历

这一篇主要用来总结一下学习到的关于二叉树的基本知识。这一篇也要开始使用英文注释了。因为中文有可能会乱码,所以所有的注释最好都用英文吧!

首先,用Java先生成一个二叉树:节点值、左孩子、右孩子。(这里节点值用的是char类型)

public class BinaryCharTree {

	
	char value;

	
	BinaryCharTree leftChild;

	
	BinaryCharTree rightChild;

	
	public BinaryCharTree(char paraName) {
		value = paraName;
		leftChild = null;
		rightChild = null;
	}

	public static BinaryCharTree manualConstructTree() {
		// Step 1.Contrast a tree with only one node.
		BinaryCharTree resultTree = new BinaryCharTree('a');

		// Step 2.Contrast all nodes.The first node is the root.

		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');

		// Step 3.link all nodes.
		resultTree.leftChild = tempTreeB;
		resultTree.rightChild = tempTreeC;
		tempTreeB.rightChild = tempTreeD;
		tempTreeC.leftChild = tempTreeE;
		tempTreeD.leftChild = tempTreeF;
		tempTreeD.rightChild = tempTreeG;

		return resultTree;
	}

1、前序遍历:根 左 右

public void preOrderVisit() {
		System.out.print("" + value + " ");

		if (leftChild != null) {
			leftChild.preOrderVisit();
		} // Of if

		if (rightChild != null) {
			rightChild.preOrderVisit();
		} // Of if
	}// Of preOrderVisit

2、中序遍历:左 根 右

public void inOrderVisit() {
		if (leftChild != null) {
			leftChild.inOrderVisit();
		} // Of if

		System.out.print("" + value + " ");

		if (rightChild != null) {
			rightChild.inOrderVisit();
		} // Of if
	}

3、后序遍历:左 右 根

public void postOrderVisit() {
		if (leftChild != null) {
			leftChild.postOrderVisit();
		} // Of if

		if (rightChild != null) {
			rightChild.postOrderVisit();
		} // Of if

		System.out.print("" + value + " ");
	}

4、树的深度:需要先判断根节点,如果一个节点没有左右子树,则为根节点,根节点的深度设定为1;接着用递归的思想去遍历左右子树。

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) {
			tempLeftDepth = rightChild.getDepth();
		} // Of if

		// The depth should increment by 1.
		if (tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth + 1;
		} else {
			return tempRightDepth + 1;
		}
	}// Of getDepth

5、树的节点个数:我认为思想是与树的深度很相似的,都需要先判断根节点,接着是用递归的思想。

递归:关于树,我认为用处最大的就是递归,三种遍历用到了递归,树的深度、节点个数也需要用到递归。

public int getNumNode() {
		// It is a leaf.
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		} // Of if.

		// The number of nodes of the left child.
		int tempLeftNodes = 0;
		if (leftChild != null) {
			tempLeftNodes = leftChild.getNumNode();
		}

		// The number of nodes of the left child.
		int tempRightNodes = 0;
		if (rightChild != null) {
			tempRightNodes = rightChild.getNumNode();
		}

		// The total number of nodes.
		return tempLeftNodes + tempRightNodes + 1;
	}// Of getNumNodes.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/694974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号