Binary tree construction and input, pre-order sequencing

Source: Internet
Author: User
Tags comparable

1  PackageOrg.lyk.main;2 3 ImportJava.util.Scanner;4 5 6 classTest<t>7 {8      Public Static<T>voidprint (T d)9     {Ten System.out.println (d); One     } A } -  - classNode<textendscomparable<t> > the { -Node<t>Left ; - T data; -Node<t>Right ; +  -     voidAdd (T data) +     { A         if(Data.compareto ( This. Data) < 0) at         { -             if(NULL!= This. Left) -                  This. Left.add (data); -             Else -             { -                  This. left =NewNode<t>(); in                  This. Left.data =data; -             } to}Else +         { -             if(NULL!= This. Right) the                  This. Right.add (data); *             Else $             {Panax Notoginseng                  This. right =NewNode<t>(); -                  This. Right.data =data; the             } +         } A     } the      Public Static<textendsComparable<t>> node<t> Add_first (node<t>Node,scanner Scanner) +     { -String data =input (scanner); $         if("*". Equals (data)) $         { -node =NULL; -         } the         Else -         {Wuyinode =NewNode<t>(); theNode.data =(T) data; -Node.left =Node.add_first (node.left,scanner); WuNode.right =Node.add_first (node.right,scanner); -         }  About         returnnode; $     } -      -     Private StaticString input (Scanner Scanner) -     { AScanner.usedelimiter ("\ r \ n"); +System.out.print ("Please Enter:");  theString data =(String) Scanner.next (); -         returndata; $     } the      the     voidGet_first () the     { theSystem.out.print ( This. data); -         if(NULL!= This. Left) in         { the              This. Left.get_first (); the         } About         if(NULL!= This. Right) the         { the              This. Right.get_first (); the         } +     } -       the      Public voidGet_middle ()Bayi     { the         if(NULL!= This. Left) the              This. Left.get_middle (); -System.out.print ( This. data); -         if(NULL!= This. Right) the              This. Right.get_middle (); the     }  the      the      Public voidget_last () -     { the         if(NULL!= This. Left) the              This. Left.get_last (); the         if(NULL!= This. Right)94              This. Right.get_last (); theSystem.out.print ( This. data); the     } the }98  About classTree<textendsComparable<t>> - {101Node<t>Root;102 103      Public voidAdd_first (Scanner Scanner)104     {  the          This. root = Node.add_first ( This. Root,scanner);106     } 107     108      Public voidGet_first ()109     { the         if(NULL!=root)111         { the Root.get_first ();113         } the     }  the      Public voidGet_middle () the     {117         if(NULL!= This. Root)118              This. Root.get_middle ();119     } -      Public voidget_last ()121     {122         if(NULL!= This. Root)123              This. Root.get_last ();124     } the }126 127  Public classMain - {129      Public Static voidMain (string[] args) the     {131Scanner Scanner =NewScanner (system.in); theTree<string> BT =NewTree<>(); 133 Bt.add_first (scanner);134System.out.println ("First Order output:");135 Bt.get_first ();136System.out.println ("middle order output:");137 Bt.get_middle ();138System.out.println ("Post output:");139 bt.get_last (); $ scanner.close ();141SYSTEM.OUT.PRINTLN ("///~ main Done"); 142     } 143}

Binary tree construction and input, pre-order sequencing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.