I. Create a node class:
Public class node {<br/> Public node leftnode; <br/> Public node rightnode; <br/> int data; </P> <p>/** <br/> * @ author hbliu <br/> * @ Param newdata <br/> */<br/> Public node (int newdata) {<br/> This. data = newdata; <br/> This. leftnode = NULL; <br/> This. rightnode = NULL; <br/>}</P> <p>}
Ii. Create a tree class:
Package COM. test. tree; </P> <p> public class binarytree {</P> <p> private node root; </P> <p>/** <br/> * function: Create an empty binary tree. <br/> */<br/> Public binarytree () {<br/> root = NULL; <br/>}</P> <p>/** <br/> * @ author hbliu <br/> * @ Param data value to be inserted <br/> * benefits: insert data to the tree <br/> */<br/> Public void insert (INT data) {<br/> This. root = insert (root, data ); <br/>}</P> <p>/** <br/> * @ Param node: the current node is the root node. <Br/> * @ Param data: the value to be inserted <br/> * @ return <br/> * function: insert the value into the binary tree, when the current node is smaller than or equal to the left value of the current node inserted in the current node, <br/> * If data larger than the current node is inserted in the right corner of the current node, recursive comparison starting from the root node each time <br/> */<br/> private node insert (node, int data) {<br/> If (node = NULL) {<br/> node = new node (data); <br/>} else if (node. DATA> = data) {<br/> node. leftnode = insert (node. leftnode, data); <br/>}else {<br/> node. rightnode = insert (node. rightnode, data); <br/>}</P> <p> return node; <br/>}</P> <p>/** <br/> * @ author hbliu <br/> * @ Param data to be inserted <br/> * benefits: build a binary tree <br/> */<br/> private void buildtree (INT [] arrayddta) {<br/> for (INT I = 0; I <arrayddta. length; I ++) {<br/> insert (arrayddta [I]); <br/>}</P> <p> Public void buildtree () {<br/> int arraydata [] = {10, 3, 1, 2, 4, 7, 12, 15 }; <br/> buildtree (arraydata ); <br/>}</P> <p>/** <br/> * @ author hbliu <br/> */<br/> Public void printtree () {<br/> printtree (this. root); <br/> system. out. println (); <br/>}</P> <p>/** <br/> * @ author hbliu <br/> * @ Param node <br/> */<br/> Public void printtree (node) {</P> <p> If (node = NULL) {<br/> return; <br/>}< br/> printtree (node. leftnode); <br/> system. out. print (node. data + ""); <br/> printtree (node. rightnode); <br/>}</P> <p> Public static void main (string [] Arg) {<br/> binarytree bt = new binarytree (); <br/> BT. buildtree (); <br/> BT. printtree (); <br/>}</P> <p >}< br/>
3. Think carefully about the application of recursion in the tree.