Java implementation two fork tree

Source: Internet
Author: User

Original link:

Http://www.cnblogs.com/lzq198754/p/5857597.html

1  Public classTreeNode {2     PrivateTreeNode Leftnode;3     PrivateTreeNode Rightnode;4     Private intvalue;5     Private BooleanIsdeleteornot;6 7      PublicTreeNode Getleftnode () {8         returnLeftnode;9     }Ten  One      Public voidSetleftnode (TreeNode leftnode) { A          This. Leftnode =Leftnode; -     } -  the      PublicTreeNode Getrightnode () { -         returnRightnode; -     } -  +      PublicTreeNode () { -         Super(); +     } A  at      Public voidSetrightnode (TreeNode rightnode) { -          This. Rightnode =Rightnode; -     } -  -      Public intGetValue () { -         returnvalue; in     } -  to      Public voidSetValue (intvalue) { +          This. Value =value; -     } the  *      Public BooleanIsisdeleteornot () { $         returnIsdeleteornot;Panax Notoginseng     } -  the      Public voidSetisdeleteornot (Booleanisdeleteornot) { +          This. Isdeleteornot =Isdeleteornot; A     } the  +      PublicTreeNode (intvalue) { -          This(NULL,NULL, Value,false); $     } $  -      PublicTreeNode (TreeNode leftnode, TreeNode Rightnode,intValueBooleanisdeleteornot) { -         Super(); the          This. Leftnode =Leftnode; -          This. Rightnode =Rightnode;Wuyi          This. Value =value; the          This. Isdeleteornot =Isdeleteornot; -     } Wu  -}

1 /**2 * Java Implementation of binary tree3  * 4  */5  Public classBintree {6     PrivateTreeNode RootNode;7 8      PublicTreeNode Getrootnode () {9         returnRootNode;Ten     } One  A     /** - * Binary Tree insert operation -      */ the      Public voidInsertvalue (intvalue) { -TreeNode NewNode =NewTreeNode (value); -         //If the root node is empty, the new node is the root node -         if(RootNode = =NULL) { +RootNode =NewNode; -Rootnode.setleftnode (NULL); +Rootnode.setrightnode (NULL); A}Else { atTreeNode Currennode =RootNode; - TreeNode Parennode; -              while(true) { -Parennode =Currennode; -                 //put it on the right . -                 if(Newnode.getvalue () >Currennode.getvalue ()) { inCurrennode =Currennode.getrightnode (); -                     if(Currennode = =NULL) { to Parennode.setrightnode (newNode); +                         return; -                     } the}Else { *                     //put it on the left . $Currennode =Currennode.getleftnode ();Panax Notoginseng                     if(Currennode = =NULL) { - Parennode.setleftnode (newNode); the                         return; +                     } A  the                 } +             } -         } $     } $  -     /** - * Ascending traversal of binary tree the      */ -      Public voidOrderbyasc (TreeNode TreeNode) {Wuyi         if(TreeNode! =NULL&& treenode.isisdeleteornot () = =false) { the Orderbyasc (Treenode.getleftnode ()); - System.out.println (Treenode.getvalue ()); Wu Orderbyasc (Treenode.getrightnode ()); -         } About     } $  -     /** - * Descending traversal of binary tree -      */ A      Public voidOrderbydesc (TreeNode TreeNode) { +         if(TreeNode! =NULL&& treenode.isisdeleteornot () = =false) { the Orderbydesc (Treenode.getrightnode ()); - System.out.println (Treenode.getvalue ()); $ Orderbydesc (Treenode.getleftnode ()); the         } the     } the  the     /** - * Binary Tree Search in      */ the      PublicTreeNode FindKey (intkey) { theTreeNode Currennode =RootNode; About         if(Currennode! =NULL) { the              while(Currennode.getvalue ()! =key) { the                 if(Currennode.getvalue () >key) { theCurrennode =Currennode.getleftnode (); +}Else { -Currennode =Currennode.getrightnode (); the                 }Bayi                 if(Currennode = =NULL) { the                     return NULL; the                 } -             } -             if(Currennode.isisdeleteornot ()) { the                 return NULL; the}Else { the                 returnCurrennode; the             } -}Else { the             return NULL; the         } the     }94  the      Public Static voidMain (string[] args) { theBintree Bintree =NewBintree (); theBintree.insertvalue (10);98Bintree.insertvalue (12); AboutBintree.insertvalue (13); - Bintree.orderbyasc (Bintree.getrootnode ());101System.out.println ("-------------------");102 Bintree.orderbydesc (Bintree.getrootnode ());103System.out.println ("-------------------");104System.out.println (Bintree.findkey (10)); the     }106 107}

Java implementation two fork tree

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.