二叉树相关知识总结(二)
二叉树的Java实现
一、分析
一个二叉树节点包含三个部分,分别是,指向左子树的部分,指向右子树的部分,数据部分,如下图所示:
我们是否可以将每个节点都抽象为一个节点对象?
我们下面来尝试下
二、代码实现
package com.example.demo.dataStructure.binaryTree; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class BinaryTree { List<TreeNode> datas = new ArrayList<TreeNode>(); public static void main(String[] args) { BinaryTree tree = new BinaryTree(); Integer array[] = {1,2,3,4,5,6,7,8,9}; TreeNode node = tree.createBinaryTree(array); System.out.println("先根遍历:"); tree.preOrderTraversal(node.getRoot()); System.out.println("中根遍历:"); tree.inOrderTraversal(node.getRoot()); System.out.println("后根遍历:"); tree.postOrderTraversal(node.getRoot()); } /** * 创建二叉树 */ public TreeNode createBinaryTree(Object[] obj) { TreeNode root = new TreeNode(); Arrays.asList(obj).stream().forEach(object -> datas.add(new TreeNode(object))); for (int parentIndex = 0; parentIndex < obj.length / 2; parentIndex++) { // 左孩子 datas.get(parentIndex).setLeftChild(datas.get(parentIndex * 2 + 1)); // 右孩子 if (parentIndex * 2 + 2 < obj.length) { datas.get(parentIndex).setRightChild(datas.get(parentIndex * 2 + 2)); } } root.setRoot(datas.get(0)); return root; } /** * 先根遍历 */ public void preOrderTraversal(TreeNode tree) { if (null == tree) { return; } System.out.print(tree.getData()); preOrderTraversal(tree.getLeftChild()); preOrderTraversal(tree.getRightChild()); } /** * 中根遍历 */ public void inOrderTraversal(TreeNode tree) { if (null == tree) { return; } inOrderTraversal(tree.getLeftChild()); System.out.print(tree.getData()); inOrderTraversal(tree.getRightChild()); } /** * 后根遍历 */ public void postOrderTraversal(TreeNode tree) { if (null == tree) { return; } postOrderTraversal(tree.getLeftChild()); postOrderTraversal(tree.getRightChild()); System.out.print(tree.getData()); } class TreeNode { private TreeNode leftChild; // 左孩子 private TreeNode rightChild; // 右孩子 private Object data; // 数据部分 private TreeNode root; // 标明根节点 public TreeNode() { super(); } // 初始化节点 public TreeNode(TreeNode leftChild, TreeNode rightChild, Object data) { super(); this.leftChild = leftChild; this.rightChild = rightChild; this.data = data; } // 初始化数据域 public TreeNode(Object data) { this(null, null, data); } public TreeNode getLeftChild() { return leftChild; } public void setLeftChild(TreeNode leftChild) { this.leftChild = leftChild; } public TreeNode getRightChild() { return rightChild; } public void setRightChild(TreeNode rightChild) { this.rightChild = rightChild; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public TreeNode getRoot() { return root; } public void setRoot(TreeNode root) { this.root = root; } } }
package com.example.demo.dataStructure.binaryTree; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class BinaryTree { List<TreeNode> datas = new ArrayList<TreeNode>(); public static void main(String[] args) { BinaryTree tree = new BinaryTree(); Integer array[] = {1,2,3,4,5,6,7,8,9}; TreeNode node = tree.createBinaryTree(array); System.out.println("先根遍历:"); tree.preOrderTraversal(node.getRoot()); System.out.println("中根遍历:"); tree.inOrderTraversal(node.getRoot()); System.out.println("后根遍历:"); tree.postOrderTraversal(node.getRoot()); } /** * 创建二叉树 */ public TreeNode createBinaryTree(Object[] obj) { TreeNode root = new TreeNode(); Arrays.asList(obj).stream().forEach(object -> datas.add(new TreeNode(object))); for (int parentIndex = 0; parentIndex < obj.length / 2; parentIndex++) { // 左孩子 datas.get(parentIndex).setLeftChild(datas.get(parentIndex * 2 + 1)); // 右孩子 if (parentIndex * 2 + 2 < obj.length) { datas.get(parentIndex).setRightChild(datas.get(parentIndex * 2 + 2)); } } root.setRoot(datas.get(0)); return root; } /** * 先根遍历 */ public void preOrderTraversal(TreeNode tree) { if (null == tree) { return; } System.out.print(tree.getData()); preOrderTraversal(tree.getLeftChild()); preOrderTraversal(tree.getRightChild()); } /** * 中根遍历 */ public void inOrderTraversal(TreeNode tree) { if (null == tree) { return; } inOrderTraversal(tree.getLeftChild()); System.out.print(tree.getData()); inOrderTraversal(tree.getRightChild()); } /** * 后根遍历 */ public void postOrderTraversal(TreeNode tree) { if (null == tree) { return; } postOrderTraversal(tree.getLeftChild()); postOrderTraversal(tree.getRightChild()); System.out.print(tree.getData()); } class TreeNode { private TreeNode leftChild; // 左孩子 private TreeNode rightChild; // 右孩子 private Object data; // 数据部分 private TreeNode root; // 标明根节点 public TreeNode() { super(); } // 初始化节点 public TreeNode(TreeNode leftChild, TreeNode rightChild, Object data) { super(); this.leftChild = leftChild; this.rightChild = rightChild; this.data = data; } // 初始化数据域 public TreeNode(Object data) { this(null, null, data); } public TreeNode getLeftChild() { return leftChild; } public void setLeftChild(TreeNode leftChild) { this.leftChild = leftChild; } public TreeNode getRightChild() { return rightChild; } public void setRightChild(TreeNode rightChild) { this.rightChild = rightChild; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public TreeNode getRoot() { return root; } public void setRoot(TreeNode root) { this.root = root; } } }
运行结果如下:
下一篇我们研究下,二叉查找树。
=================================================================
Easier said than done.