Java generates two fork tree and traverse

Source: Internet
Author: User

The method of implementing a two-fork tree and a linked list in Java is to define an object reference to the class in the class

Like what

class tree{    int  data;    Tree left;    Tree right;}

In this case, when we are new to a tree, the object has left and right two objects, thus connecting the

Role, in the list is connected to the next, in the tree is equivalent to the edge, so that the effect of one after another. Anyway, the object is connected.

Here is the complete code

 PackageCode; Public classtwotree{ Public Static voidMain (string[] args) {Tree root=NewTree (50); intarray[]={25,89,48,90,101,13,45,60};  for(intn=0;n<8;n++) {Root.insert (root, array[n]); } Bianli BL=NewBianli (root);    Bl.second (root); }}classtree{intdata;    Tree left;    Tree right; //every tree     PublicTree (intdata) {         This. data=data; Left=NULL; Right=NULL; }     Public voidInsert (Tree root,intdata) {        //if it is larger than the root, if the right is empty, put it on the right side of the root, otherwise, take the right side of the root of the other tree and put it on his right.//sequentiallydown        if(data>root.data) {if(root.right==NULL) {Root.right=NewTree (data); }                       Else            {                 This. Insert (root.right, data); }        }        //On the left .        Else        {            if(root.left==NULL) Root.left=NewTree (data); Else                 This. Insert (root.left, data); }            }    }classbianli{Tree Root;  PublicBianli (Tree root) { This. root=Root; }    //First traversal method, pre-sequence traversal, root--Zuozi--right subtree     Public voidFirst (Tree root) {if(root!=NULL) {System.out.println (root.data);            First (Root.left);        First (root.right); }            }    //The second method of traversal. Middle sequence traversal A-zigzag, right from top to bottom, left bottom to top     Public voidsecond (Tree root) {if(root!=NULL) {second (root.left);            System.out.println (Root.data);        Second (root.right); }    }
The third kind of post-traversal
Public voidthird (Tree root) {if(root!=NULL) {third (root.left); Third (root.right); System.out.println (Root.data); } }}

In the code we can find that a lot of recursion is used, and the recursion of the spanning tree is taken first.

 Public voidInsert (Tree root,intdata) {        //if it is larger than the root, if the right is empty, put it on the right side of the root, otherwise, take the right side of the root of the other tree and put it on his right.//take it down .        if(data>root.data) {if(root.right==NULL) {Root.right=NewTree (data); }                     Else            {                 This. Insert (root.right, data); }        }        //On the left .        Else        {            if(root.left==NULL) Root.left=NewTree (data); Else                 This. Insert (root.left, data); }            }

The recursion here is implemented so that when we have the right side of the root, we add the added object to the right.

Otherwise, recursively, the object on the right of the root as a root of the subtree, and so on.

We can understand that a large tree is a tree (a binary tree) of/\.

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

The traversal of the binary tree, in the code of three kinds of traversal, we find all the code is similar, but the output of the statement location is different. This need

Let's take a brief look at it.

We need to go back to the nature of recursion.

int num=0;  Public void DG (int  n) {   if(n==1)       return 1;            Num=n*dg (n-1);      }

This is the factorial of N, the most classic example of recursion.

Our analysis can be known that recursion is a method of invoking itself,

For example, the DG method inside the code above calls the DG approach

We need a condition to end the recursion, then calculate from the back, and finally go back to where the recursion began.

Similarly, the traversal of the above binary tree is the same, three kinds of traversal code is very similar.

When we meet the end of the flag, we need to complete the last second (or first or third) method

(Abstract understanding 12345678 Here I have recursive visualization, complete 1 of the operation needs to complete 2, complete 2 need 3 And so on Then, back to 8, the end of the recursion, this time need to complete 8 of all operations, and then to 7 to complete all the operations and so on, and finally return to the beginning of the recursive place)

And the three kinds of traversal methods are different, that is, after executing the method of the operation (that is, 8 (76 The remaining operations).

Some are to continue to the right of the traversal, some print first, which results in a different printing result.

Freshman dog Beginner, wrong please understand!

Java generates two fork tree and traverse

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.