首页 > 代码库 > 二叉树的构建与输入,前序中序后序

二叉树的构建与输入,前序中序后序

  1 package org.lyk.main;
  2 
  3 import java.util.Scanner;
  4 
  5 
  6 class Test<T>
  7 {
  8     public static <T> void print(T d)
  9     {
 10         System.out.println(d);
 11     }
 12 }
 13 
 14 class Node<T extends Comparable<T> >
 15 {
 16     Node<T> left;
 17     T data;
 18     Node<T> right;
 19 
 20     void add(T data)
 21     {
 22         if (data.compareTo(this.data) < 0)
 23         {
 24             if (null != this.left)
 25                 this.left.add(data);
 26             else
 27             {
 28                 this.left = new Node<T>();
 29                 this.left.data =http://www.mamicode.com/ data;
 30             }
 31         } else
 32         {
 33             if (null != this.right)
 34                 this.right.add(data);
 35             else
 36             {
 37                 this.right = new Node<T>();
 38                 this.right.data =http://www.mamicode.com/ data;
 39             }
 40         }
 41     }
 42     public static <T extends Comparable<T>> Node<T> add_first(Node<T> node,Scanner scanner)
 43     {
 44         String data =http://www.mamicode.com/ input(scanner);
 45         if("*".equals(data))
 46         {
 47             node = null;
 48         }
 49         else
 50         {
 51             node = new Node<T>();
 52             node.data =http://www.mamicode.com/ (T)data;
 53             node.left = Node.add_first(node.left,scanner);
 54             node.right = Node.add_first(node.right,scanner);
 55         } 
 56         return node;
 57     }
 58     
 59     private static String input(Scanner scanner)
 60     {
 61         scanner.useDelimiter("\r\n");
 62         System.out.print("请输入:");  
 63         String data =http://www.mamicode.com/ (String)scanner.next();
 64         return data;
 65     }
 66     
 67     void get_first()
 68     {
 69         System.out.print(this.data);
 70         if(null != this.left )
 71         {
 72             this.left.get_first();
 73         }
 74         if(null != this.right)
 75         {
 76             this.right.get_first();
 77         }
 78     }
 79      
 80     public void get_middle()
 81     {
 82         if(null != this.left)
 83             this.left.get_middle();
 84         System.out.print(this.data);
 85         if(null != this.right)
 86             this.right.get_middle();
 87     } 
 88     
 89     public void get_last()
 90     {
 91         if(null != this.left )
 92             this.left.get_last();
 93         if(null != this.right)
 94             this.right.get_last();
 95         System.out.print(this.data);
 96     }
 97 }
 98 
 99 class Tree<T extends Comparable<T>>
100 {
101     Node<T> root; 
102 
103     public void add_first(Scanner scanner)
104     { 
105         this.root = Node.add_first(this.root,scanner);
106     } 
107     
108     public void get_first()
109     {
110         if(null != root)
111         {
112             root.get_first();
113         }
114     } 
115     public void get_middle()
116     {
117         if(null != this.root)
118             this.root.get_middle();
119     }
120     public void get_last()
121     {
122         if(null != this.root)
123             this.root.get_last();
124     }
125 }
126 
127 public class Main
128 {
129     public static void main(String[] args)
130     {
131         Scanner scanner = new Scanner(System.in);
132         Tree<String> bt = new Tree<>(); 
133         bt.add_first( scanner);
134         System.out.println("先序输出:");
135         bt.get_first();
136         System.out.println("中序输出:");
137         bt.get_middle();
138         System.out.println("后序输出:");
139         bt.get_last();
140         scanner.close();
141         System.out.println("///~ main done"); 
142     } 
143 }

 

二叉树的构建与输入,前序中序后序