• 难点:树的代码


    ADT:

    1. package tree;
    2. public interface TTree<T> {
    3. boolean isEmpty();//判断是否为空
    4. int level(T key);//判断层次
    5. int size();//节点数量
    6. int height();//树的高度
    7. void preorder();//先根次序
    8. void postorder();//后根次序
    9. void levelorder();//层次遍历
    10. TreeNode insertRoot(T x);//当根节点插入
    11. TreeNode insertChild(TreeNode p,T x,int i);//插入x当作p节点的i个孩子
    12. void remove(TreeNode p,int i);//删除第i个子树
    13. void clear();//清空
    14. TreeNode search(T x);//查找
    15. T remove(T x);//删除
    16. boolean contains(T key);//判断是否包含
    17. }

    TreeNode:

    1. package tree;
    2. public class TreeNode<T> {
    3. public T data;//数据
    4. public TreeNode parent,child,sibling;//父母节点,孩子节点,兄弟节点,链表连接
    5. public int level;//节点层次
    6. public TreeNode(T data, int level,TreeNode parent,TreeNode child,TreeNode sibling) {
    7. this.data=data;
    8. this.level=level;
    9. this.parent=parent;
    10. this.child=child;
    11. this.sibling=sibling;//初始化成功
    12. }
    13. public TreeNode(T data,int level) {
    14. this(data,level,null,null,null);//初始化
    15. }//为的是初始化
    16. public String toString() {
    17. return this.data.toString();//节点信息
    18. }
    19. public boolean isLeaf() {
    20. return this.child==null;
    21. }//判断是不是叶子节点
    22. }

    Tree:

    1. package tree;
    2. public class Tree<T> {
    3. public TreeNode <T>root;//根节点
    4. public Tree() {
    5. this.root=null;//初始化
    6. }
    7. public boolean isEmpty() {
    8. return this.root==null;
    9. }//判空
    10. public String toString() {
    11. return "树的显示\n"+toString(root,"");
    12. }
    13. public String toString(TreeNode<T> p,String tab) {
    14. if (p==null) {
    15. return "";
    16. }
    17. return tab+p.data.toString()+"\n"+toString(p.child,tab+"\t")+toString(p.sibling,tab);//递归调用
    18. }
    19. //先序
    20. public void preorder() {
    21. preorder(this.root);
    22. System.out.println();
    23. }
    24. public void preorder(TreeNode<T> p) {
    25. if(p!=null) {
    26. System.out.print(p.data+" ");
    27. preorder(p.child);
    28. preorder(p.sibling);//循环调用
    29. }
    30. }
    31. //后序
    32. public void postorder() {
    33. postorder(this.root);
    34. System.out.println();
    35. }
    36. public void postorder(TreeNode<T> p) {
    37. if(p!=null) {
    38. postorder(p.child);
    39. System.out.print(p.data+" ");
    40. postorder(p.sibling);//循环调用
    41. }
    42. }
    43. //递归提取节点数量
    44. public int size() {
    45. return size(this.root);
    46. }
    47. public int size(TreeNode<T> p) {
    48. if(p==null) {
    49. return 0;
    50. }
    51. return 1+size(p.child)+size(p.sibling);
    52. }
    53. public Tree(Tree<T> tree) {
    54. this.root=copy(tree.root,null);//初始化
    55. }
    56. //深拷贝
    57. public TreeNode<T> copy(TreeNode<T> p,TreeNode<T> parent){
    58. if (p==null) {
    59. return null;
    60. }
    61. TreeNode<T> q=new TreeNode<T>(p.data,p.level,parent,null,null);
    62. q.child=copy(p.child,q);
    63. q.child=copy(p.sibling,q);
    64. return q;//返回节点
    65. }
    66. public void clear() {
    67. this.root=null;
    68. }
    69. public void setLevel(TreeNode<T> p,int level) {
    70. if(p!=null) {
    71. p.level=level;
    72. setLevel(p.child,level+1);
    73. setLevel(p.sibling,level);
    74. }
    75. }
    76. public TreeNode<T> insertRoot(T x){
    77. this.root=new TreeNode<T>(x,1,null,this.root,null);
    78. if(this.root.child!=null) {
    79. this.root.child.parent=this.root;
    80. setLevel(this.root.child,this.root.level+1);//完成插入
    81. }
    82. return this.root;
    83. }
    84. //String prelist[]={"中国","\t北京","\t上海","\t江苏","\t\t南京","\t\t\t江宁","\t\t苏州",
    85. //"\t\t无锡","\t\t\t锡山","\t浙江","\t\t杭州","\t\t宁波","\t\t温州","\t广东","\t\t广州",
    86. //"韩国","\t首尔","法国","意大利","美国","\t华盛顿","\t纽约州","\t\t纽约"};
    87. public static Tree<String> create(String[]prelist){
    88. Tree<String> tree =new Tree<String>();
    89. if (prelist.length==0) {
    90. return tree; //返回空树
    91. }
    92. tree.root=new TreeNode<String>(prelist[0],1);//层次1
    93. TreeNode<String>p=tree.root;//备份根节点
    94. for(int i=1;i<prelist.length;i++) {
    95. int n=0;//多少个tab字符,为的是看层次
    96. while(n<prelist[i].length() && prelist[i].charAt(n)=='\t') {//"\t"的长度是一
    97. n++;
    98. }
    99. String str=prelist[i].substring(n);//去除\t
    100. if(n==p.level) {
    101. p.child=new TreeNode<String>(str,p.level+1,p,null,null);
    102. p=p.child;//循环下一个
    103. }else {
    104. while(n<p.level-1) {//向上寻找位置
    105. p=p.parent;
    106. }
    107. p.sibling=new TreeNode<String>(str,p.level,p.parent,null,null);
    108. p=p.sibling;
    109. }
    110. }
    111. return tree;
    112. }
    113. }

    运行代码:

    1. package tree;
    2. public class TreeRun {
    3. public static void main(String[] args) {
    4. // TODO Auto-generated method stub
    5. String prelist[]={"中国","\t北京","\t上海","\t江苏","\t\t南京","\t\t\t江宁","\t\t苏州",
    6. "\t\t无锡","\t\t\t锡山","\t浙江","\t\t杭州","\t\t宁波","\t\t温州","\t广东","\t\t广州",
    7. "韩国","\t首尔","法国","意大利","美国","\t华盛顿","\t纽约州","\t\t纽约"};
    8. Tree<String>mytree=Tree.create(prelist);
    9. mytree.preorder();
    10. mytree.postorder();
    11. System.out.println(mytree.toString());
    12. }
    13. }

    运行结果:

     

  • 相关阅读:
    Bean的生命周期
    关系代词 - 定义与分类
    【云原生之kubernetes实战】在k8s环境下部署Leanote蚂蚁笔记工具
    【语音识别】高斯混合模型(GMM)说话人识别【含Matlab源码 574期】
    Kafka与Spring Boot等应用框架的集成及消息驱动模型
    1002 A+B for Polynomials
    STM32之HAL开发——CubeMX串行Flash文件系统源码讲解
    秋招第二周面试经验
    Spring Boot 配置静态资源路径
    【LeetCode回溯算法#06】复原IP地址详解(练习如何处理边界条件,判断IP合法性)
  • 原文地址:https://blog.csdn.net/qq_63775204/article/details/127043053