二叉树,特殊的图,非线性结构,递归的
总览
package com.collection;
public class BTreeNode {
public static class TreeNode<E>{
public E data;
public TreeNode<E> left,right;
public TreeNode(E data) {
this.data = data;
this.left = this.right = null;
}
}
}
测试
package com.collection;
public class Main {
public static void main(String[] args) {
BTreeNode.TreeNode<Character> a = new BTreeNode.TreeNode<>('A');
BTreeNode.TreeNode<Character> b = new BTreeNode.TreeNode<>('B');
BTreeNode.TreeNode<Character> c = new BTreeNode.TreeNode<>('C');
BTreeNode.TreeNode<Character> d = new BTreeNode.TreeNode<>('D');
BTreeNode.TreeNode<Character> e = new BTreeNode.TreeNode<>('E');
a.left = b;
a.right = c;
b.left = d;
b.right = e;
System.out.println(a.left.left.data);
}
}
二叉树遍历
前序遍历
public static void preOrder(TreeNode<Character> root){
if (root != null) {
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}
中序遍历
public static void inOrder(TreeNode<Character> root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
后序遍历
public static void postOrder(TreeNode<Character> root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
}
层序遍历,使用队列,稍微复杂一点
public static void levelOrder(TreeNode<Character> root) {
if (root == null) {
return;
}
Queue<TreeNode<Character>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode<Character> node = queue.poll();
System.out.print(node.data + " ");
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
System.out.println();
}
总览
package com.collection;
import java.util.LinkedList;
import java.util.Queue;
public class BTreeNode {
public static class TreeNode<E>{
public E data;
public TreeNode<E> left,right;
public TreeNode(E data) {
this.data = data;
this.left = this.right = null;
}
}
public static void preOrder(TreeNode<Character> root){
if (root != null) {
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}
public static void inOrder(TreeNode<Character> root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
public static void postOrder(TreeNode<Character> root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
}
public static void levelOrder(TreeNode<Character> root) {
if (root == null) {
return;
}
Queue<TreeNode<Character>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode<Character> node = queue.poll();
System.out.print(node.data + " ");
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
System.out.println();
}
}
测试
package com.collection;
public class Main {
public static void main(String[] args) {
BTreeNode.TreeNode<Character> a = new BTreeNode.TreeNode<>('A');
BTreeNode.TreeNode<Character> b = new BTreeNode.TreeNode<>('B');
BTreeNode.TreeNode<Character> c = new BTreeNode.TreeNode<>('C');
BTreeNode.TreeNode<Character> d = new BTreeNode.TreeNode<>('D');
BTreeNode.TreeNode<Character> e = new BTreeNode.TreeNode<>('E');
BTreeNode.TreeNode<Character> f = new BTreeNode.TreeNode<>('F');
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.right = f;
System.out.println(a.left.left.data);
BTreeNode.preOrder(a);
System.out.println();
BTreeNode.inOrder(a);
System.out.println();
BTreeNode.postOrder(a);
System.out.println();
BTreeNode.levelOrder(a);
}
}