码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-11-08 14:14:05      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:color   data   构建   use   com   tree   stat   public   main   

  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 = 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 = data;
 39             }
 40         }
 41     }
 42     public static <T extends Comparable<T>> Node<T> add_first(Node<T> node,Scanner scanner)
 43     {
 44         String data = input(scanner);
 45         if("*".equals(data))
 46         {
 47             node = null;
 48         }
 49         else
 50         {
 51             node = new Node<T>();
 52             node.data = (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 = (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 }

 

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

标签:color   data   构建   use   com   tree   stat   public   main   

原文地址:http://www.cnblogs.com/kuillldan/p/6042511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!