java二叉树的遍历算法代码_java中二叉树遍历(递归) 程序代码

本文章给大家介绍java中二叉树遍历(递归) 程序代码,有常用的递归遍历也有其它更高级的算法来遍历二叉树,有需要了解的朋友可参考本文章。

测试二叉树遍历,递归算法

 代码如下复制代码

public class TestBinaryTree {

public static void main(String[] args) {

Node g = new Node("G", null, null);

Node e = new Node("E", null, null);

Node f = new Node("F", null, null);

Node d = new Node("D", null, g);

Node b = new Node("B", d, e);

Node c = new Node("C", null, f);

Node a = new Node("A", b, c);

System.out.println("生成的二叉树:");

System.out.println("            A");

System.out.println("            |     ");

System.out.println("       |---------|");

System.out.println("       B         C");

System.out.println("       |         |");

System.out.println("  |---------|     -----|");

System.out.println("  D         E          F");

System.out.println("  |");

System.out.println("   ----|");

System.out.println("       G");

System.out.println("二叉树深度:" BinaryTree.getDepth(a));

System.out.print("前序遍历:");

BinaryTree.priorderTraversal(a);

System.out.println();

System.out.print("中序遍历:");

BinaryTree.inorderTraversal(a);

System.out.println();

System.out.print("后序遍历:");

BinaryTree.postorderTraversal(a);

System.out.println();

}

}

// 二叉树

class BinaryTree {

// 前序遍历

static void priorderTraversal(Node node) {

if (node != null) {

visitNode(node);

priorderTraversal(node.getLeftChild());

priorderTraversal(node.getRightChild());

}

}

// 中序遍历

static void inorderTraversal(Node node) {

if (node != null) {

inorderTraversal(node.getLeftChild());

visitNode(node);

inorderTraversal(node.getRightChild());

}

}

// 后序遍历

static void postorderTraversal(Node node) {

if (node != null) {

postorderTraversal(node.getLeftChild());

postorderTraversal(node.getRightChild());

visitNode(node);

}

}

// 二叉树深度

static int getDepth(Node node) {

if (node == null) {

return 0;

}

int leftDepth = 0;

int rightDepth = 0;

leftDepth = getDepth(node.getLeftChild());

rightDepth = getDepth(node.getRightChild());

return (leftDepth > rightDepth ? leftDepth : rightDepth) 1;

}

// 访问节点

static void visitNode(Node node) {

System.out.print(node.getKey() " ");

}

}

// 节点

class Node {

private T key;

private Node leftChild;

private Node rightChild;

public Node() {

}

public Node(T key, Node leftChild, Node rightChild) {

super();  www.2cto.com

this.key = key;

this.leftChild = leftChild;

this.rightChild = rightChild;

}

public T getKey() {

return key;

}

public void setKey(T key) {

this.key = key;

}

public Node getLeftChild() {

return leftChild;

}

public void setLeftChild(Node leftChild) {

this.leftChild = leftChild;

}

public Node getRightChild() {

return rightChild;

}

public void setRightChild(Node rightChild) {

this.rightChild = rightChild;

}

}

非递归算法

当建立好二叉树类后可以创建二叉树实例,并实现二叉树的先根遍历,中根遍历,后根遍历,代码如下:

 代码如下复制代码

package package2;

public class BinaryTreePreorder {

public static void preOrder(BinaryTree root){  //先根遍历

if(root!=null){

System.out.print(root.data "-");

preOrder(root.left);

preOrder(root.right);

}

}

public static void inOrder(BinaryTree root){     //中根遍历

if(root!=null){

inOrder(root.left);

System.out.print(root.data "--");

inOrder(root.right);

}

}

public static void postOrder(BinaryTree root){    //后根遍历

if(root!=null){

postOrder(root.left);

postOrder(root.right);

System.out.print(root.data "---");

}

}

public static void main(String[] str){

int[] array = {12,76,35,22,16,48,90,46,9,40};

BinaryTree root = new BinaryTree(array[0]);   //创建二叉树

for(int i=1;i

root.insert(root, array[i]);       //向二叉树中插入数据

}

System.out.println("先根遍历:");

preOrder(root);

System.out.println();

System.out.println("中根遍历:");

inOrder(root);

System.out.println();

System.out.println("后根遍历:");

postOrder(root);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值