The description of a binary sorting tree is also a recursive description, so the structure of the binary sorting tree is also recursive:
Three features of a binary tree:
1: The value of all left children of the current node is smaller than that of the current node;
2: The values of all right children of the current node are greater than those of the current node;
3: The child node meets the preceding requirements.
Package test. sort; </P> <p> public class binarynode {<br/>/** <br/> * Author: sunxing007, <br/> * reprinted please indicate from the http://blog.csdn.net/sunxing007 <br/> **/<br/> private int value; // current value <br/> private binarynode lchild; // left child <br/> private binarynode rchild; // right child </P> <p> Public binarynode (INT value, binarynode L, binarynode R) {<br/> This. value = value; <br/> This. lchild = L; <br/> This. rchild = r; <Br/>}</P> <p> Public binarynode getlchild () {<br/> return lchild; <br/>}< br/> Public void setlchild (binarynode child) {<br/> lchild = Child; <br/>}< br/> Public binarynode getrchild () {<br/> return rchild; <br/>}< br/> Public void setrchild (binarynode child) {<br/> rchild = child; <br/>}< br/> Public int getvalue () {<br/> return value; <br/>}< br/> Public void setvalue (INT value) {<br/> This. value = Value; <br/>}</P> <p> // iterate all node. <br/> Public static void iterate (binarynode root) {<br/> If (root. lchild! = NULL) {<br/> iterate (root. getlchild (); <br/>}< br/> system. out. print (root. getvalue () + ""); <br/> If (root. rchild! = NULL) {<br/> iterate (root. getrchild ()); <br/>}</P> <p>/** <br/> * add child to the current node to construct a tree. <br/> * Time: O (nlog (N) <br/> ***/<br/> Public void addchild (int n) {<br/> If (n <value) {<br/> If (lchild! = NULL) {<br/> lchild. addchild (n); <br/>}< br/> else {<br/> lchild = new binarynode (n, null, null ); <br/>}< br/> else {<br/> If (rchild! = NULL) {<br/> rchild. addchild (n); <br/>}< br/> else {<br/> rchild = new binarynode (n, null, null ); <br/>}</P> <p> // test case. <br/> Public static void main (string [] ARGs) {<br/> system. out. println (); <br/> int [] arr = new int [] {100 }; <br/> binarynode root = new binarynode (ARR [0], null, null); <br/> for (INT I = 1; I <arr. length; I ++) {<br/> root. addchild (ARR [I]); <br/>}< br/> binarynode. iterate (Root); <br/>}< br/>
Reprinted please indicate from http://blog.csdn.net/sunxing007