ツリーアルゴリズムは、データを追加してツリー機能を形成し、シーケンスで印刷することを実現します.

3855 ワード

import   java.io.*;     
  import   java.util.Stack;     
      
  public   class   myTest   {     
  private   myTree   tree;     
      
      
  /**    
  *      ,   (   ,  )    
  *    
  **/     
  public   void   insert(int   key,   int   data)     
  {     
  if(tree   ==   null)     
  {     
  tree   =   new   myTree();     
  tree.key   =   key;     
  tree.data   =   data;     
  }     
  else     
  {     
  myTree   newTree   =   new   myTree();     
  newTree.key   =   key;     
  newTree.data   =   data;     
  myTree   parent   =   tree;     
  while(true)     
  {     
  if(   newTree.key   <   parent.key)     
  {     
  if(   parent.leftChild   ==   null)     
  {     
  parent.leftChild   =   newTree;     
  return;     
  }     
  else     
  {     
  parent   =   parent.leftChild;     
  }     
  }     
  else   if(   newTree.key   >   parent.key)     
  {     
  if(parent.rightChild   ==   null)     
  {     
  parent.rightChild   =   newTree;     
  return;     
  }     
  else     
  {     
  parent   =   parent.rightChild;     
  }     
  }     
  }     
      
  }     
  }     
      
  /**    
    *         ,   (   ),       myTree         
    *      
    *   **/     
  public   myTree   find(int   key)     
  {     
  if(   tree   ==   null   )   return   null;     
  myTree   curr   =   new   myTree();     
  curr.key   =   key;     
  myTree   parent   =   tree;     
  while(true)     
  {     
  if(   parent   ==   null)     
  {     
  return   null;     
  }     
  else   if(   curr.key   ==   parent.key)     
  {     
  return   parent;     
  }     
  else   if(   curr.key   >   parent.key)     
  {     
  parent   =   parent.rightChild;     
  }     
  else   if(   curr.key   <   parent.key)     
  {     
  parent   =   parent.leftChild;     
  }     
  }     
  }     
      
      
      
  /*    
    *      
    *                 
    *      
    *      
    */     
  private   static   void   midOrder(myTree   tree)     
  {     
  if(tree   !=   null   )     
  {     
  midOrder(tree.leftChild);     
  System.out.println(tree+","+tree.key+","+tree.data);     
  midOrder(tree.rightChild);     
  }     
  }     
      
                        
  /*    
    *             
    */     
  private   static   void   frontOrder(myTree   tree)     
  {     
  if(tree   !=   null)     
  {     
  System.out.println(""+tree.key+"   ,   "+tree.data);     
  frontOrder(tree.leftChild);     
  frontOrder(tree.rightChild);     
  }     
  }     
      
      
  public   static   void   main(String[]   args)       
  {     
  System.out.println("Tree   view   Begin");     
  myTest   t1   =   new   myTest();     
  t1.insert(8,25);     
  t1.insert(5,9);     
  t1.insert(58,87);     
  t1.insert(13,82);     
  t1.insert(4,8);     
  t1.insert(12,54);     
  t1.insert(53,123);     
  t1.insert(56,47);     
  t1.insert(2,75);     
  t1.insert(34,5);     
  t1.insert(6,23);     
  System.out.println("      :");     
  midOrder2(t1.tree);     
  midOrder(t1.tree);     
  }     
  }