二叉树遍历等基本操作(Java实现)
前中后序遍历递归实现+层序遍历:
树的结点类代码:
public class TreeNode<Value extends Comparable<? super Value>> {
private Value value;
private TreeNode left;
private TreeNode right;
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public TreeNode(Value value){
this.value = value;
}
}
接下来对这颗树进行遍历:

遍历类代码:
public class Treetraversal {
/**
* 前序遍历,先根遍历
*
* @param node 树的根
*/
public static void preOrder(TreeNode node) {
if (node == null) return;
System.out.print(node.getValue() + "\t");
preOrder(node.getLeft());
preOrder(node.getRight());
}
/**
* 中序遍历,中根遍历
*
* @param node 树的根
*/
public static void inOrder(TreeNode node) {
if (node == null) return;
inOrder(node.getLeft());
System.out.print(node.getValue() + "\t");
inOrder(node.getRight());
}
/**
* 后序遍历,后根遍历
*
* @param node 树的根
*/
public static void postOrder(TreeNode node) {
if (node == null) return;
postOrder(node.getLeft());
postOrder(node.getRight());
System.out.print(node.getValue() + "\t");
}
/**
* 层序遍历,层次遍历
*
* @param node 树的根
*/
public static void levelOrder(TreeNode node) {
java.util.LinkedList<TreeNode> queue = new java.util.LinkedList<>();
queue.add(node);
while (!queue.isEmpty()) {
TreeNode cur = queue.pop();
System.out.print(cur.getValue() + "\t");
if (cur.getLeft() != null) queue.add(cur.getLeft());
if (cur.getRight() != null) queue.add(cur.getRight());
}
}
public static void main(String[] args) {
//创建一颗树,一个样例
TreeNode<Character> root = new TreeNode<>('A');
root.setLeft(new TreeNode<>('B'));
root.getLeft().setLeft(new TreeNode<>('D'));
root.getLeft().setRight(new TreeNode<>('E'));
root.getLeft().getRight().setLeft(new TreeNode<>('G'));
root.setRight(new TreeNode<>('C'));
root.getRight().setRight(new TreeNode<>('F'));
preOrder(root);//A B D E G C F
System.out.println();
inOrder(root);//D B G E A C F
System.out.println();
postOrder(root);//D G E B F C A
System.out.println();
levelOrder(root);//A B C D E F G
}
}
求树的深度
树结点类的代码和上面一样。测试用的树也和上面一样。
求二叉树深度的代码(递归):
public class TreeDepth {
public static int treeDepth(TreeNode node) {
if (node == null) return 0;
return Math.max(treeDepth(node.getLeft()), treeDepth(node.getRight())) + 1;
}
public static void main(String[] args) {
//创建一颗树,一个样例
TreeNode<Character> root = new TreeNode<>('A');
root.setLeft(new TreeNode<>('B'));
root.getLeft().setLeft(new TreeNode<>('D'));
root.getLeft().setRight(new TreeNode<>('E'));
root.getLeft().getRight().setLeft(new TreeNode<>('G'));
root.setRight(new TreeNode<>('C'));
root.getRight().setRight(new TreeNode<>('F'));
int depth = treeDepth(root);
System.out.println(depth);//4
}
}
求二叉树叶子节点个数:
树结点类的代码和上面一样。测试用的树也和上面一样。
求二叉树叶子结点的代码(递归):
public class LeafCounter {
public static int leafCount(TreeNode node) {
if (node == null) return 0;
if (node.getLeft() == null && node.getRight() == null) return 1;
return leafCount(node.getLeft()) + leafCount(node.getRight());
}
public static void main(String[] args) {
//创建一颗树,一个样例
TreeNode<Character> root = new TreeNode<>('A');
root.setLeft(new TreeNode<>('B'));
root.getLeft().setLeft(new TreeNode<>('D'));
root.getLeft().setRight(new TreeNode<>('E'));
root.getLeft().getRight().setLeft(new TreeNode<>('G'));
root.setRight(new TreeNode<>('C'));
root.getRight().setRight(new TreeNode<>('F'));
int count = leafCount(root);
System.out.println(count);//3
}
}
二叉树遍历等基本操作(Java实现)的更多相关文章
- java 二叉树遍历
package com.lever; import java.util.LinkedList;import java.util.Queue; /** * 二叉树遍历 * @author lckxxy ...
- javascript实现数据结构: 树和二叉树,二叉树的遍历和基本操作
树型结构是一类非常重要的非线性结构.直观地,树型结构是以分支关系定义的层次结构. 树在计算机领域中也有着广泛的应用,例如在编译程序中,用树来表示源程序的语法结构:在数据库系统中,可用树来组织信息:在分 ...
- 二叉树遍历(Java实现)
二叉树遍历(Java实现) 主要是二叉树的遍历,包括递归遍历和非递归遍历 import java.util.ArrayDeque; import java.util.ArrayList; impo ...
- python实现二叉树的遍历以及基本操作
主要内容: 二叉树遍历(先序.中序.后序.宽度优先遍历)的迭代实现和递归实现: 二叉树的深度,二叉树到叶子节点的所有路径: 首先,先定义二叉树类(python3),代码如下: class TreeNo ...
- java数据结构之二叉树遍历的非递归实现
算法概述递归算法简洁明了.可读性好,但与非递归算法相比要消耗更多的时间和存储空间.为提高效率,我们可采用一种非递归的二叉树遍历算法.非递归的实现要借助栈来实现,因为堆栈的先进后出的结构和递归很相似.对 ...
- 二叉树遍历-JAVA实现
二叉树遍历分为前序.中序.后序递归和非递归遍历.还有层序遍历. //二叉树节点 public class BinaryTreeNode { private int data; private Bina ...
- 数据结构之二叉树篇卷三 -- 二叉树非递归遍历(With Java)
Nonrecursive Traversal of Binary Tree First I wanna talk about why we should <code>Stack</c ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- php实现二叉树遍历
php实现二叉树遍历 一.总结 关注输入输出 二.php实现二叉树遍历 题目描述 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的先序遍历字符串 ...
随机推荐
- SpringBoot+gradle+idea实现热部署和热加载
前言 因为之前使用myeclipes的同学就知道,在使用myeclipes的时候,java文件或者jsp文件写完之后会被直接热加载到部署的容器中,从而在开发的时候,不同经常去重启项目,从而达到了增加开 ...
- iOS.Animations.by.Tutorials.v2.0汉化(三)
第2章:Springs 在前一章中,您学习了如何创建UIKit的基本动画,包括如何提供起始值和结束值随着时间的UIKit,自动为你创建一个动画. 到目前为止,你的动画一直是单方向的流体运动.当你激活一 ...
- linux memcached Session共享
memcached memcached是高性能的分布式缓存服务器用来集中缓存数据库查询结果,减少数据库访问次数提高动态web应用的响应速度 传统web架构的问题许多web应用都将数据保存在RDBMS中 ...
- Win7/8出现An error occurred on the server when processing the URL解决办法
使用的是win8系统搭建的本地服务器,win7使用的方法是相同的.如果你的系统是精简版的Win7/8,那么安装IIS7也有可能出现这问题.下面SJY带领大家来解决这个错误. 解决方法 打开控制面板→管 ...
- Java中list.get(index)报错
1.list.get(index)中的index为负值异常 严重:Exception occurred during processing request:-1 java.lang.ArrayInde ...
- Flex和Servlet结合上传文件报错(一)
1.具体错误如下 一个表单域 不是一个表单域 java.io.FileNotFoundException: D:\MyEclipse\workspace\FlexFileUpload\Web\null ...
- Eclipse设置内存大小
Eclipse设置内存大小 1.修改Eclipse的配置文件 (1)打开Eclipse目录 (2)以EditPlus打开eclipse.ini,修改"-Xms40m -Xmx512m&qu ...
- Linux显示一个二进制文件或可执行文件的完整路径
Linux显示一个二进制文件或可执行文件的完整路径 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ which halt /sbin/halt
- unix时间戳转换成标准时间(c#)
//---unix时间戳转换成标准时间(c#)---// /* string timeStamp = "1144821796"; DateTime dtSt ...
- RHEL部署ipa红帽身份验证
1.先下载必须包 yum install -y ipa-server bind bind-dyndb-ldap 2.初始化ipa基本配置 ipa-server-install * Configure ...