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