[Java] Java通过递归进行二叉树遍历 →→→→→进入此内容的聊天室

来自 , 2020-03-30, 写在 Java, 查看 97 次.
URL http://www.code666.cn/view/342b5fe6
  1. package com.wzs;
  2.  
  3. // 测试二叉树遍历,递归算法
  4. public class TestBinaryTree {
  5.         public static void main(String[] args) {
  6.                 Node<String> g = new Node<String>("G", null, null);
  7.                 Node<String> e = new Node<String>("E", null, null);
  8.                 Node<String> f = new Node<String>("F", null, null);
  9.                 Node<String> d = new Node<String>("D", null, g);
  10.                 Node<String> b = new Node<String>("B", d, e);
  11.                 Node<String> c = new Node<String>("C", null, f);
  12.                 Node<String> a = new Node<String>("A", b, c);
  13.  
  14.                 System.out.println("生成的二叉树:");
  15.                 System.out.println("            A");
  16.                 System.out.println("            |     ");
  17.                 System.out.println("       |---------|");
  18.                 System.out.println("       B         C");
  19.                 System.out.println("       |         |");
  20.                 System.out.println("  |---------|     -----|");
  21.                 System.out.println("  D         E          F");
  22.                 System.out.println("  |");
  23.                 System.out.println("   ----|");
  24.                 System.out.println("       G");
  25.  
  26.                 System.out.println("二叉树深度:" + BinaryTree.getDepth(a));
  27.  
  28.                 System.out.print("前序遍历:");
  29.                 BinaryTree.priorderTraversal(a);
  30.                 System.out.println();
  31.  
  32.                 System.out.print("中序遍历:");
  33.                 BinaryTree.inorderTraversal(a);
  34.                 System.out.println();
  35.  
  36.                 System.out.print("后序遍历:");
  37.                 BinaryTree.postorderTraversal(a);
  38.                 System.out.println();
  39.         }
  40. }
  41.  
  42. // 二叉树
  43. class BinaryTree {
  44.         // 前序遍历
  45.         static <T> void priorderTraversal(Node<T> node) {
  46.                 if (node != null) {
  47.                         visitNode(node);
  48.                         priorderTraversal(node.getLeftChild());
  49.                         priorderTraversal(node.getRightChild());
  50.                 }
  51.         }
  52.  
  53.         // 中序遍历
  54.         static <T> void inorderTraversal(Node<T> node) {
  55.                 if (node != null) {
  56.                         inorderTraversal(node.getLeftChild());
  57.                         visitNode(node);
  58.                         inorderTraversal(node.getRightChild());
  59.                 }
  60.         }
  61.  
  62.         // 后序遍历
  63.         static <T> void postorderTraversal(Node<T> node) {
  64.                 if (node != null) {
  65.                         postorderTraversal(node.getLeftChild());
  66.                         postorderTraversal(node.getRightChild());
  67.                         visitNode(node);
  68.                 }
  69.         }
  70.  
  71.         // 二叉树深度
  72.         static <T> int getDepth(Node<T> node) {
  73.                 if (node == null) {
  74.                         return 0;
  75.                 }
  76.                 int leftDepth = 0;
  77.                 int rightDepth = 0;
  78.                 leftDepth = getDepth(node.getLeftChild());
  79.                 rightDepth = getDepth(node.getRightChild());
  80.                 return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
  81.         }
  82.  
  83.         // 访问节点
  84.         static <T> void visitNode(Node<T> node) {
  85.                 System.out.print(node.getKey() + " ");
  86.         }
  87. }
  88.  
  89. // 节点
  90. class Node<T> {
  91.         private T key;
  92.         private Node<T> leftChild;
  93.         private Node<T> rightChild;
  94.  
  95.         public Node() {
  96.  
  97.         }
  98.  
  99.         public Node(T key, Node<T> leftChild, Node<T> rightChild) {
  100.                 super();
  101.                 this.key = key;
  102.                 this.leftChild = leftChild;
  103.                 this.rightChild = rightChild;
  104.         }
  105.  
  106.         public T getKey() {
  107.                 return key;
  108.         }
  109.  
  110.         public void setKey(T key) {
  111.                 this.key = key;
  112.         }
  113.  
  114.         public Node<T> getLeftChild() {
  115.                 return leftChild;
  116.         }
  117.  
  118.         public void setLeftChild(Node<T> leftChild) {
  119.                 this.leftChild = leftChild;
  120.         }
  121.  
  122.         public Node<T> getRightChild() {
  123.                 return rightChild;
  124.         }
  125.  
  126.         public void setRightChild(Node<T> rightChild) {
  127.                 this.rightChild = rightChild;
  128.         }
  129.  
  130. }
  131.  
  132. //java/6693

回复 "Java通过递归进行二叉树遍历"

这儿你可以回复上面这条便签

captcha