java学习第21天,二叉树

二叉树的先序,中序和后序遍历,这里要多看看数据结构。

package java21to30;

public class D21_BinaryCharTree {
	char value;
	D21_BinaryCharTree leftChild;
	D21_BinaryCharTree rightChild;

	public static void main(String[] args) {
		D21_BinaryCharTree tree = manualConstructTree();
		System.out.println("先序遍历:");
		tree.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		tree.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		tree.postOrderVisit();

		System.out.println("\r\n二叉树深度: " + tree.getDepth());
		System.out.println("节点个数: " + tree.getNumNodes());
	}

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

	public static D21_BinaryCharTree manualConstructTree() {
		D21_BinaryCharTree result = new D21_BinaryCharTree('a');
		D21_BinaryCharTree TreeB = new D21_BinaryCharTree('b');
		D21_BinaryCharTree TreeC = new D21_BinaryCharTree('c');
		D21_BinaryCharTree TreeD = new D21_BinaryCharTree('d');
		D21_BinaryCharTree TreeE = new D21_BinaryCharTree('e');
		D21_BinaryCharTree TreeF = new D21_BinaryCharTree('f');
		D21_BinaryCharTree TreeG = new D21_BinaryCharTree('g');
		D21_BinaryCharTree TreeH = new D21_BinaryCharTree('h');
		D21_BinaryCharTree TreeI = new D21_BinaryCharTree('i');
		result.leftChild = TreeB;
		result.rightChild = TreeC;
		TreeB.rightChild = TreeD;
		TreeC.leftChild = TreeE;
		TreeD.leftChild = TreeF;
		TreeD.rightChild = TreeG;
		TreeG.leftChild = TreeH;
		TreeB.leftChild = TreeI;
		return result;
	}

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

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

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

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

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

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

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

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

	public int getDepth() {
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		}
		int tempLeftDepth = 0;
		if (leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		}
		int tempRightDepth = 0;
		if (rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		}
		if (tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth + 1;
		} else {
			return tempRightDepth + 1;
		}
	}

	public int getNumNodes() {
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		}
		int tempLeftNodes = 0;
		if (leftChild != null) {
			tempLeftNodes = leftChild.getNumNodes();
		}
		int tempRightNodes = 0;
		if (rightChild != null) {
			tempRightNodes = rightChild.getNumNodes();
		}
		return tempLeftNodes + tempRightNodes + 1;
	}
}

输出结果:

先序遍历:
a b i d f g h c e 
中序遍历:
i b f d h g a e c 
后序遍历:
i f h g d b e c a 
二叉树深度: 5
节点个数: 9
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是二十道Java算法题目: 1. 编写一个程序,将字符串中的字符按照出现次数进行排序。 2. 给定一个整数数组,找到两个数之和等于目标值的数对。 3. 实现一个函数来计算一个字符串中含有的不重复字符的个数。 4. 给定一个字符串,编写一个函数来判断它是否是回文字符串。 5. 实现一个函数,把一个字符串中的空格替换成%20。 6. 给定一个二叉树,编写一个函数来获取其最大深度。 7. 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。 8. 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 9. 给定一个无序数组,找出其中的最长连续序列。 10. 给定一个二叉树,确定它是否是一个有效的二叉搜索树。 11. 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 12. 编写一个函数来验证输入的字符串是否是有效的 IPv4 地址或 IPv6 地址。 13. 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 14. 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 15. 编写一个程序,找到两个单链表相交的起始节点。 16. 给定一个二叉树,返回所有从根节点到叶子节点的路径。 17. 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。 18. 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度。 19. 给定一个字符串 s,找到 s 中最长的回文子串。 20. 给定一个二叉树,找到它的最小深度。 希望这些题目能够对您有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值