当前位置:   article > 正文

链式二叉树的实现(Java)_java创建链式二叉树

java创建链式二叉树

定义树节点:

  1. package 链式二叉树;
  2. public class TreeNode {
  3. private Object data;
  4. private TreeNode left;
  5. private TreeNode right;
  6. public TreeNode() {
  7. this.data = null;
  8. this.left = null;
  9. this.right = null;
  10. }
  11. public TreeNode(Object data, TreeNode left, TreeNode right) {
  12. this.data = data;
  13. this.left = left;
  14. this.right = right;
  15. }
  16. public Object getData() {
  17. return this.data;
  18. }
  19. public void setData(Object data) {
  20. this.data = data;
  21. }
  22. public TreeNode getLeft() {
  23. return this.left;
  24. }
  25. public void setLeft(TreeNode left) {
  26. this.left = left;
  27. }
  28. public TreeNode getRight() {
  29. return this.right;
  30. }
  31. public void setRight(TreeNode right) {
  32. this.right = right;
  33. }
  34. }

二叉树实现:

  1. package 链式二叉树;
  2. public class BinaryTree {
  3. public TreeNode root;
  4. public BinaryTree(TreeNode root) {
  5. this.root = root;
  6. }
  7. public BinaryTree() {
  8. this.root = null;
  9. }
  10. public TreeNode getRoot() {
  11. return this.root;
  12. }
  13. public void setRoot(TreeNode root) {
  14. this.root = root;
  15. }
  16. //前序遍历
  17. public void preorder(TreeNode root) {
  18. if(root != null) {
  19. System.out.println(root.getData());
  20. preorder(root.getLeft());
  21. preorder(root.getRight());
  22. }
  23. }
  24. //中序遍历
  25. public void middleorder(TreeNode root) {
  26. if(root != null) {
  27. middleorder(root.getLeft());
  28. System.out.println(root.getData());
  29. middleorder(root.getRight());
  30. }
  31. }
  32. //后序遍历
  33. public void postorder(TreeNode root) {
  34. if(root != null) {
  35. postorder(root.getLeft());
  36. postorder(root.getRight());
  37. System.out.println(root.getData());
  38. }
  39. }
  40. //获得父节点
  41. public TreeNode getParent(TreeNode ele) {
  42. return (root == null || ele == ele) ? null : parent(root, ele);
  43. }
  44. private TreeNode parent(TreeNode root, TreeNode e) {
  45. if(root == null) return null;
  46. if(root.getLeft() == e || root.getRight() == e) return root;
  47. TreeNode t;
  48. return ((t = parent(root.getLeft(), e)) != null) ? t : parent(root.getRight(), e);
  49. }
  50. //获得节点个数
  51. public int getSize() {
  52. return length(root);
  53. }
  54. private int length(TreeNode root) {
  55. return root == null ? 0 : (length(root.getLeft()) + length(root.getRight()) + 1);
  56. }
  57. //获得树的深度
  58. public int getDepth() {
  59. return depth(root);
  60. }
  61. private int depth(TreeNode root) {
  62. if(root == null) return 0;
  63. int m = depth(root.getLeft());
  64. int n = depth(root.getRight());
  65. return m > n ? m+1 : n+1;
  66. }
  67. public static void main(String[] args) {
  68. TreeNode l12 = new TreeNode("left12", null, null);
  69. TreeNode r12 = new TreeNode("right12", null, null);
  70. TreeNode l22 = new TreeNode("left22", null, null);
  71. TreeNode r22 = new TreeNode("right22", null, null);
  72. TreeNode l1 = new TreeNode("left1", l12, r12);// 根节点左子树
  73. TreeNode r1 = new TreeNode("right1", l22, r22);// 根节点右子树
  74. TreeNode root = new TreeNode("root", l1, r1);// 创建根节点
  75. BinaryTree bt = new BinaryTree(root);
  76. // System.out.println("=======先序遍历======");
  77. // bt.preorder(bt.getRoot());
  78. // System.out.println("=======中序遍历======");
  79. // bt.middleorder(bt.getRoot());
  80. System.out.println("深度:"+ bt.getDepth());
  81. }
  82. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/233080
推荐阅读
相关标签
  

闽ICP备14008679号