数据结构 -- 二叉树的学习(三)
数据结构 -- 二叉树的学习(三)
用链表来实现二叉树
二叉树最多只能有两个子节点,也就是说分支度只能是小于或等于2,。而二叉树的链表存储法,就是利用链表来存储二叉树的,相比较使用数组来存储二叉树,会更加节省空间,尽可能减少空间浪费。
如何创建呢?其实不难,就像是创建单链表一样,先得要创建节点类 TreeNode 再像创建链表LinkList一样创建一个BinaryTree类用于对结点类TreeNode的操作。
节点类:
/** * 二叉树的节点类 */ public class TreeNode { private int value; private TreeNode LeftNode; private TreeNode RightNode; public TreeNode(int value) { this.value = value; this.LeftNode = null; this.RightNode = null; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public TreeNode getLeftNode() { return LeftNode; } public void setLeftNode(TreeNode leftNode) { LeftNode = leftNode; } public TreeNode getRightNode() { return RightNode; } public void setRightNode(TreeNode rightNode) { RightNode = rightNode; } }
BinaryTree类:
/** * 二叉树类 */ public class BinaryTree { private TreeNode rootNode; // 树的根节点 public TreeNode getRootNode() { return rootNode; } /** * 判断树是否为空 */ public boolean isEmpty() { return rootNode == null; } /** * 添加结点,建立二叉树 */ public void createBinaryTree(int treeNumber) { TreeNode newNode = new TreeNode(treeNumber);//新的结点 TreeNode currentNode = rootNode; if (isEmpty()) { rootNode = newNode; return; } while (true) { if (currentNode.getValue() > newNode.getValue()) { // 放在左子树 // 再判断当前结点额左子树是否为空,若为空就放入,反之,就指向结点的指针移动 if (currentNode.getLeftNode() == null) { currentNode.setLeftNode(newNode); return;//每一个结点建立一次,使用while(true)死循环遍历合适的位置,自然放入后就return退出。 } else { currentNode = currentNode.getLeftNode(); } } else { // 放在右子树 // 再判断当前结点额右子树是否为空,若为空就放入,反之,就指向结点的指针移动 if (currentNode.getRightNode() == null) { currentNode.setRightNode(newNode); return; } else { currentNode = currentNode.getRightNode(); } } } } /** * 树的遍历,使用先序遍历 */ public void preOrder(TreeNode rootNode) { if (rootNode != null) { System.out.print(rootNode.getValue() + "\t"); preOrder(rootNode.getLeftNode()); preOrder(rootNode.getRightNode()); } } /** * 树的遍历,使用中序遍历 */ public void inOrder(TreeNode rootNode) { if (rootNode != null) { inOrder(rootNode.getLeftNode()); System.out.print(rootNode.getValue() + "\t"); inOrder(rootNode.getRightNode()); } } /** * 树的遍历,使用后序遍历 */ public void postOrder(TreeNode rootNode) { if (rootNode != null) { postOrder(rootNode.getLeftNode()); postOrder(rootNode.getRightNode()); System.out.print(rootNode.getValue() + "\t"); } } }
测试类:
public class Demone1 { public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); System.out.println(binaryTree.isEmpty()); binaryTree.createBinaryTree(6); binaryTree.createBinaryTree(3); binaryTree.createBinaryTree(5); binaryTree.createBinaryTree(9); binaryTree.createBinaryTree(7); binaryTree.createBinaryTree(8); binaryTree.createBinaryTree(4); binaryTree.createBinaryTree(2); System.out.print("先序遍历:"); binaryTree.preOrder(binaryTree.getRootNode()); System.out.println(); System.out.print("中序遍历:"); binaryTree.inOrder(binaryTree.getRootNode()); System.out.println(); System.out.print("后序遍历:"); binaryTree.postOrder(binaryTree.getRootNode()); System.out.println(); } }
测试结果:
|
true Process finished with exit code 0 |
注意点:使用链表来表示二叉树的好处就是结点的增加与删除操作相当容易,缺点就是很难找到父节点,除非在每一个节点上多加一个属性,指向父节点。

浙公网安备 33010602011771号