Java Data Structure Tree

Source: Internet
Author: User

 

 

<Br/> public class testbinarytree1 <br/> {<br/> testnode1 root; <br/> testbinarytree1 left; <br/> testbinarytree1 right; <br/> static int I; <br/> static int left1; <br/> static int right1; <br/> // final static int I1 = I ++; <br/> Public void adddata (int n, string name, int CC, int data1) <br/>{< br/> system. out. println ("input: =" + n); <br/> If (root = NULL) <br/>{< br/> root = new testnode1 (); <br/> root. setdata (N ); <Br/> system. out. println ("root =" + root. getdata () + "" + (++ I) + "times"); <br/> system. out. println ("-------------------------"); <br/> If (name! = NULL & CC! = 0 & data1! = 0) <br/> system. out. println (name + "lower" + (CC + 1) + "layer" + "the upper layer is:" + data1); <br/> system. out. println ("-------------------------"); <br/>}< br/> else <br/>{< br/> int DATA = root. getdata (); <br/> system. out. println ("root DATA =" + data); <br/> If (n <= data) <br/>{< br/> If (this. left = NULL) <br/> This. left = new testbinarytree1 (); <br/> system. out. println ("Left left because it is smaller than"); <br/> This. left. adddata (n, "Left left", ++ left1, data); <Br/>}< br/> else <br/>{< br/> If (this. right = NULL) <br/> This. right = new testbinarytree1 (); <br/> system. out. println ("right because it is greater than"); <br/> This. right. adddata (n, "Right", ++ right1, data); <br/>}< br/> Public void sort1 () <br/>{< br/> If (this. left! = NULL) <br/> This. Left. sort1 (); <br/> If (this. Right! = NULL) <br/> This. Right. sort1 (); <br/> If (this. Root! = NULL) <br/> system. out. print (root. getdata () + ","); <br/>}</P> <p> Public void sort2 () <br/>{< br/> If (this. root! = NULL) <br/> system. Out. Print (root. getdata () + ","); <br/> If (this. Left! = NULL) <br/> This. Left. sort2 (); <br/> If (this. Right! = NULL) <br/> This. right. sort2 (); <br/>}< br/> Public void sort3 () <br/>{< br/> If (this. left! = NULL) <br/> This. Left. sort3 (); <br/> If (this. Root! = NULL) <br/> system. Out. Print (root. getdata () + ","); <br/> If (this. Right! = NULL) <br/> This. right. sort3 (); <br/>}< br/> Public void sort4 () <br/>{< br/> If (this. right! = NULL) <br/> This. Right. sort4 (); <br/> If (this. Root! = NULL) <br/> system. Out. Print (root. getdata () + ","); <br/> If (this. Left! = NULL) <br/> This. left. sort4 (); <br/>}< br/> Public static void main (string [] ARGs) <br/>{< br/> testbinarytree1 bt = new testbinarytree1 (); <br/> start1 (BT); <br/> start2 (BT); <br/>}< br/> Private Static void clear1 () <br/>{< br/> right1 = 0; left1 = 0; <br/>}< br/> Private Static void start1 (testbinarytree1 BT) <br/>{< br/> BT. adddata (1, null, 0, 0); clear1 (); <br/> BT. adddata (3, null, 0, 0); clear1 (); <br/> BT. adddata (2, null, 0, 0); clear1 (); <br/> BT. adddata (1, null, 0, 0); clear1 (); <br/> BT. adddata (4, null, 0, 0); clear1 (); <br/> BT. adddata (9, null, 0, 0); clear1 (); <br/> BT. adddata (-1, null, 0, 0); clear1 (); <br/>}< br/> Private Static void start2 (testbinarytree1 BT) <br/>{< br/> BT. sort1 (); <br/> system. out. println (); <br/> BT. sort2 (); <br/> system. out. println (); <br/> BT. sort3 (); <br/> system. out. println (); <br/> BT. sort4 (); <br/>}< br/> class testnode1 <br/>{< br/> int data; <br/> Public static int I; <br/> Public testnode1 () <br/>{< br/> ++ I; <br/>}< br/> Public int getdata () <br/>{< br/> return data; <br/>}< br/> Public void setdata (INT data) <br/>{< br/> This. data = data; <br/>}< br/>}

 

 

/*

Input: = 1

Root = 1 time

-------------------------

-------------------------

Input: = 3

Root DATA = 1

Because it is greater

Input: = 3

Root = 3 2 times

-------------------------

Right, the top layer of Layer 1 is: 1

-------------------------

Input: = 2

Root DATA = 1

Because it is greater

Input: = 2

Root DATA = 3

Because it is less than, left

Input: = 2

Root = 2 3 times

-------------------------

The top layer of left 1 is: 3

-------------------------

Input: = 1

Root DATA = 1

Because it is less than, left

Input: = 1

Root = 1 4 times

-------------------------

The top layer of left 1 is: 1

-------------------------

Input: = 4

Root DATA = 1

Because it is greater

Input: = 4

Root DATA = 3

Because it is greater

Input: = 4

Root = 4 5 times

-------------------------

The top layer of right 2 is: 3

-------------------------

Input: = 9

Root DATA = 1

Because it is greater

Input: = 9

Root DATA = 3

Because it is greater

Input: = 9

Root DATA = 4

Because it is greater

Input: = 9

Root = 9 6 times

-------------------------

Right, Layer 3, Layer 4

-------------------------

Input: =-1

Root DATA = 1

Because it is less than, left

Input: =-1

Root DATA = 1

Because it is less than, left

Input: =-1

Root =-1 7 times

-------------------------

The top layer of left 2 is: 1

-------------------------

Press any key to continue...

*/

 

 

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.