Java object-oriented common class library api--two tree data structure implementation

Source: Internet
Author: User

A binary tree is an ordered tree with a maximum of two subtrees per node. Usually the subtree is called the "left subtree" and "right subtree".

Sorting rules for binary tree algorithms:

1. Select the first element as the root node

2. Then if the element is greater than the root node placed in the right subtree, if the element is small on the root node, place the Zuozi

3. Finally, the output can be sorted by the way of the middle order traversal (left-to-right)

8, 3, 10, 1, 6, 14, 4, 7, 13

1  Public classBinaryTree {2     PrivateNode Root;3      Public voidAddNode (intdata) {4         if( This. root==NULL){5root=NewNode (data);6}Else{7 root.add (data);8         }9     }Ten      Public voidPrintnode () { One         if(root!=NULL){ A root.print (); -         } -     } the      -      -      -      Private classnode{ +          Private intdata; -          PrivateNode left; +          PrivateNode right; A          PublicNode (intdata) { at             Super(); -              This. data =data; -         } -           Public voidAddintdata) { -              if( This.data>data) { -                  if(left==NULL){ inleft=NewNode (data); -}Else{ to left.add (data); +                  } -                  the}Else if( This. data<=data) { *                  if( This. right==NULL){ $                       This. right=NewNode (data);Panax Notoginseng}Else{ -                       This. Right.add (data); the                  } +              } A          } the          //Middle Sequence traversal, rule: Zogen right +           Public voidprint () { -              if( This. left!=NULL){ $                   This. Left.print (); $              } -System.out.print ( This. data+ "-"); -              if( This. right!=NULL){ the                   This. Right.print (); -              }Wuyi          } the      } -}

Java object-oriented common class library api--two tree data structure implementation

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.