• 二叉树(二叉链表实现)JAVA代码


     
    1. publicclassTest{
    2.  
    3.     publicstaticvoid main(String[] args){
    4.  
    5.         char[] ch =newchar[]{'A','B','D','#','#','G','#','#','C','J','#','#','M','#','#'};
    6.         BinaryTree binaryTree =newBinaryTree(ch);
    7.  
    8.         binaryTree.preOrder();
    9.         System.out.println();
    10.  
    11.         binaryTree.inOrder();
    12.         System.out.println();
    13.  
    14.         binaryTree.postOrder();
    15.         System.out.println();
    16.  
    17.         binaryTree.levelOrder();
    18.         System.out.println();
    19.  
    20.     }
    21.  
    22. }
     
     
    1. publicclassBinaryTree{
    2.  
    3.     //二叉树结点
    4.     publicclassBiNode{
    5.         char data;
    6.         BiNode left;
    7.         BiNode right;
    8.  
    9.         BiNode(char data,BiNode left,BiNode right){
    10.             this.data = data;
    11.             this.left = left;
    12.             this.right = right;
    13.         }
    14.  
    15.         int flag =0;//供非递归后序遍历使用
    16.     }
    17.  
    18.     //根结点
    19.     publicBiNode root;
    20.  
    21.     privatestaticint i;
    22.     publicBinaryTree(char[] pre){
    23.         i =0;
    24.         root = create(pre);
    25.     }
    26.  
    27.     //初始化(先序遍历顺序存放数组、'#'表示null)
    28.     privateBiNode create(char[] pre)
    29.     {
    30.         if(i < pre.length)
    31.         {
    32.             if(pre[i]=='#')                             //结点为空
    33.             {
    34.                 i++;
    35.                 return null;
    36.             }
    37.  
    38.             BiNode p =newBiNode(pre[i], null, null);    //结点非空
    39.             i++;
    40.             p.left = create(pre);                         //递归建立左子树
    41.             p.right = create(pre);                       //递归建立右子树
    42.             return p;
    43.         }
    44.         return null;
    45.     }
    46.  
    47.     //先序遍历
    48.     publicvoid preOrder(){
    49.         System.out.print("preOrder traversal with recursion:");
    50.         preOrder(root);
    51.         System.out.println();
    52.  
    53.         System.out.print("preOrder traversal without recursion:");
    54.         preOrder2(root);
    55.         System.out.println();
    56.     }
    57.     //递归
    58.     privatevoid preOrder(BiNode root){
    59.         if(root == null)return;
    60.  
    61.         System.out.print(root.data);       //访问结点
    62.         preOrder(root.left);
    63.         preOrder(root.right);
    64.     }
    65.     //非递归
    66.     privatevoid preOrder2(BiNode head){
    67.         LinkedList<BiNode> s =newLinkedList<BiNode>();
    68.  
    69.         while(head != null ||!s.isEmpty())
    70.         {
    71.             while(head != null)                //访问左子树
    72.             {
    73.                 System.out.print(head.data);    //访问左子树
    74.                 s.push(head);                    //结点入栈(待后面找其右子树使用)= =(“递归”)
    75.                 head = head.left;
    76.             }
    77.  
    78.             if(!s.isEmpty())                   //转向右子树
    79.             {
    80.                 head = s.peek().right;     //转向右子树
    81.                 s.pop();                        //结点出栈(已经找到其右子树)= =(“递归结束”)
    82.             }
    83.         }
    84.     }
    85.  
    86.     //中序遍历
    87.     publicvoid inOrder(){
    88.         System.out.print("inOrder traversal with recursion:");
    89.         inOrder(root);
    90.         System.out.println();
    91.  
    92.         System.out.print("inOrder traversal without recursion:");
    93.         inOrder2(root);
    94.         System.out.println();
    95.     }
    96.     //递归
    97.     privatevoid inOrder(BiNode root){
    98.         if(root == null)return;
    99.  
    100.         inOrder(root.left);
    101.         System.out.print(root.data);      //访问结点
    102.         inOrder(root.right);
    103.     }
    104.     //非递归
    105.     privatevoid inOrder2(BiNode head){
    106.         LinkedList<BiNode> s =newLinkedList<BiNode>();
    107.  
    108.         while(head != null ||!s.isEmpty())
    109.         {
    110.             while(head != null)                //左子树入栈
    111.             {
    112.                 s.push(head);                    //结点入栈(待后面找其右子树使用)= =(“递归”)
    113.                 head = head.left;
    114.             }
    115.  
    116.             System.out.print(s.peek().data);    //访问左子树
    117.  
    118.             if(!s.isEmpty())                   //转向右子树
    119.             {
    120.                 head = s.peek().right;         //转向右子树
    121.                 s.pop();                        //结点出栈(已经找到其右子树)= =(“递归结束”)
    122.             }
    123.         }
    124.     }
    125.  
    126.     //后序遍历
    127.     publicvoid postOrder(){
    128.         System.out.print("postOrder traversal with recursion:");
    129.         postOrder(root);
    130.         System.out.println();
    131.  
    132.         System.out.print("postOrder traversal without recursion:");
    133.         postOrder2(root);
    134.         System.out.println();
    135.     }
    136.     //递归
    137.     privatevoid postOrder(BiNode root){
    138.         if(root == null)return;
    139.  
    140.         postOrder(root.left);
    141.         postOrder(root.right);
    142.         System.out.print(root.data);     //访问结点
    143.     }
    144.     //非递归
    145.     //后序遍历特点:递归左右子树后,还需访问结点:
    146.     //1、左子树入栈
    147.     //2、“两次出栈”(用flag标记模仿):第一次是为了找到左子树相应的右子树结点;第二次是为了访问结点
    148.     privatevoid postOrder2(BiNode head){
    149.         LinkedList<BiNode> s =newLinkedList<BiNode>();
    150.  
    151.         while(head != null ||!s.isEmpty())
    152.         {
    153.             while(head != null)                        //左子树入栈
    154.             {
    155.                 head.flag =1;
    156.                 s.push(head);                            //结点连同flag入栈(待后面找其右子树使用)= =(“递归”)
    157.                 head = head.left;
    158.             }
    159.  
    160.             while(!s.isEmpty()&& s.peek().flag ==2)  //若flag为2(已经找到其右子树出过一次栈),访问结点
    161.             {
    162.                 System.out.print(s.peek().data);       //访问结点元素
    163.                 s.pop();                                //(第二次“结点出栈”)实际结点出栈(已经访问结点元素)= =(“递归结束”)
    164.             }
    165.  
    166.             if(!s.isEmpty())                          //flag为1,转向右子树
    167.             {
    168.                 head = s.peek().right;                //转向右子树
    169.                 s.peek().flag =2;                    //(第一次“flag模拟出栈”)标记为2,但实际结点不出栈(已经找到其右子树)
    170.             }
    171.  
    172.  
    173.         }
    174.     }
    175.  
    176.     //层序遍历
    177.     publicvoid levelOrder(){
    178.         levelOrder(root);
    179.     }
    180.     privatevoid levelOrder(BiNode root){
    181.         LinkedList<BiNode>queue=newLinkedList<BiNode>();  //LinkedList实现了Queue接口
    182.  
    183.         BiNode p = root;
    184.         while(p != null){
    185.             System.out.print(p.data);    //访问结点
    186.  
    187.             if(p.left != null)
    188.                 queue.add(p.left);
    189.             if(p.right != null)
    190.                 queue.add(p.right);
    191.  
    192.             p =queue.poll();           //队头出队并返回为p
    193.         }
    194.     }
    195.  
    196.     //在p结点后插入data
    197.     publicvoid insert(BiNode p,char data, boolean left){
    198.         if(p != null){
    199.             if(left)                //插入位置为左孩子
    200.                 p.left =newBiNode(data,p.left,null);
    201.             else                   //插入位置为右孩子
    202.                 p.right =newBiNode(data,p.right,null);
    203.         }
    204.     }
    205.  
    206.     //删除p的一个子树
    207.     publicvoiddelete(BiNode p, boolean left){
    208.         if(p != null){
    209.             if(left)              //删除目标为左子树
    210.                 p.left = null;
    211.             else                  //删除目标为右子树
    212.                 p.right = null;
    213.         }
    214.     } 
    215.  
    216. }
     
     
     
     
     





  • 相关阅读:
    python split的用法
    大学排名爬虫
    一直在报错:ModuleNotFoundError: No module named 'bs4'.
    微信小程序部署到线上环境
    mybatis关联三张表查询对应字段名
    WPF图像裁剪控件
    git创建标签并推送到远程
    Windows7、10的热键屏蔽
    WPF使用SVG简单整理
    Visual Studio 注册码和下载
  • 原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5535035.html
Copyright © 2020-2023  润新知