概述
二叉树的遍历
二叉树基本概念:
在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构。二叉树是每个结点最多有两个子树的有序树。子树通常被称为 “左子树” 和 “右子树”,二叉树常被用于实现二叉查找树和二叉堆。
二叉树的遍历:
二叉树的遍历大致分为先根遍历,中根遍历,后根遍历,层次遍历。按照具体的算法又可分为递归和非递归的,
代码如下:
import java.util.LinkedList;
import java.util.Stack;
public class BinaryTree {
/*
* 测试,打印结果
*/
public static void main(String[] args){
BinaryTreeMade tree = maketree();
System.out.print("先序遍历(非递归):");
nonrecursion_preorder(tree.root);
System.out.println();
System.out.print("先序遍历(递 归):");
preorder(tree.root);
System.out.println();
System.out.print("中序遍历(非递归):");
nonrecursion_inorder(tree.root);
System.out.println();
System.out.print("中序遍历(递 归):");
inorder(tree.root);
System.out.println();
System.out.print("后序遍历(非递归):");
nonrecursion_postorder(tree.root);
System.out.println();
System.out.print("后序遍历(递 归):");
postorder(tree.root);
System.out.println();
System.out.print("层次遍历:");
level_order(tree.root);
}
/*
* 构造二叉树,根节点是 a
* 根节点的左右节点是 b c
* b,c的左右节点分别是 d e f g
*/
public static BinaryTreeMade maketree(){
BinaryNode a7 = new BinaryNode('g',null,null);
BinaryNode a6 = new BinaryNode('f',null,null);
BinaryNode a5 = new BinaryNode('e',null,null);
BinaryNode a4 = new BinaryNode('d',null,null);
BinaryNode a3 = new BinaryNode('c',a6,a7);
BinaryNode a2 = new BinaryNode('b',a4,a5);
BinaryNode a1 = new BinaryNode('a',a2,a3);
BinaryTreeMade tree = new BinaryTreeMade(a1);
return tree;
}
/*
* 递归的先根遍历
*/
public static void preorder(BinaryNode node){
if (node!=null) {
System.out.print(node.element+" ");
preorder(node.lefttree);
preorder(node.righttree);
}
}
/*
* 递归的中根遍历
*/
public static void inorder(BinaryNode node){
if (node!=null) {
inorder(node.lefttree);
System.out.print(node.element+" ");
inorder(node.righttree);
}
}
/*
* 递归的后根遍历
*/
public static void postorder(BinaryNode node){
if (node!=null) {
postorder(node.lefttree);
postorder(node.righttree);
System.out.print(node.element+" ");
}
}
/*
* 非递归的先根遍历
*/
public static void nonrecursion_preorder(BinaryNode node){
Stack<BinaryNode> stack = new Stack<BinaryNode>();
BinaryNode b = node;
while(b!=null||!stack.isEmpty()){
while (b!=null) {
System.out.print(b.element+ " ");
stack.push(b);
b = b.lefttree;
}
if (!stack.isEmpty()) {
b = stack.pop();
b = b.righttree;
}
else {
return;
}
}
}
/*
*非递归的中跟遍历
*/
public static void nonrecursion_inorder(BinaryNode node){
Stack<BinaryNode> stack = new Stack<BinaryNode>();
BinaryNode b = node;
for ( ; ; ) {
while (b!=null) {
stack.push(b);
b = b.lefttree;
}
if (!stack.isEmpty()) {
b = stack.pop();
System.out.print(b.element+" ");
b = b.righttree;
}
else {
return;
}
}
}
/*
* 非递归的后跟遍历
*/
public static void nonrecursion_postorder(BinaryNode node){
Stack<BinaryNode> stack = new Stack<BinaryNode>();
BinaryNode b = node;
BinaryNode tmp = null;
while(b!=null||!stack.isEmpty()){
while(b!=null){
stack.push(b);
b = b.lefttree;
}
if (!stack.isEmpty()) {
tmp = stack.pop();
if (tmp.isfirst == true) {
tmp.isfirst = false;
stack.push(tmp);
b = tmp.righttree;
}else {
System.out.print(tmp.element+" ");
}
}
}
}
/*
* 层次遍历
*/
public static void level_order(BinaryNode node){
LinkedList<BinaryNode> linkedList = new LinkedList<BinaryNode>();
BinaryNode b = node;
while (b!=null) {
System.out.print(b.element+" ");
if (b.lefttree!=null) {
linkedList.add(b.lefttree);
}
if (b.righttree!=null) {
linkedList.add(b.righttree);
}
b = linkedList.get(0);
linkedList.remove(0);
if (linkedList.size()==0) {
System.out.print(b.element+" ");
break;
}
}
}
}
/*
* 二叉树的节点类
*/
class BinaryNode{
Object element;
BinaryNode lefttree;
BinaryNode righttree;
boolean isfirst;
public BinaryNode(Object e, BinaryNode l, BinaryNode r){
element = e;
lefttree = l;
righttree = r;
isfirst = true;
}
}
/*
* 二叉树的构造类
*/
class BinaryTreeMade{
BinaryNode root;
public BinaryTreeMade(BinaryNode root){
this.root = root;
}
}
最后
以上就是微笑毛豆为你收集整理的【数据结构和算法分析】二叉树的遍历二叉树的遍历的全部内容,希望文章能够帮你解决【数据结构和算法分析】二叉树的遍历二叉树的遍历所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复