Java generates and traverses binary trees, and java generates Binary Trees.

Source: Internet
Author: User

Java generates and traverses binary trees, and java generates Binary Trees.

In java, binary tree and linked list are implemented by defining object references of this class in the class.

For example

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

In this way, when we create a new Tree object, the object will have two objects, left and right, so that the connected

In the linked list, the link is connected to the next one. In the tree, it is equivalent to an edge, so that the result is one by one. In short, the object is connected.

The complete code is as follows:

Package code; public class TwoTree {public static void main (String [] args) {Tree root = new Tree (50); int array [] =, 45, 60}; for (int n = 0; n <8; n ++) {root. insert (root, array [n]);} Bianli bl = new Bianli (root); bl. second (root) ;}} class Tree {int data; Tree left; Tree right; // public Tree (int data) {this. data = data; left = null; right = null;} public void insert (Tree root, int data ){// If the root is larger than the root, if the right side is empty, put it to the right of the root. Otherwise, the right side of the root is the root of another tree, put it to the right. // run it in sequence. if (data> root. data) {if (root. right = null) {root. right = new Tree (data);} else {this. insert (root. right, data) ;}// else {if (root. left = null) root. left = new Tree (data); else this. insert (root. left, data) ;}} class Bianli {Tree root; public Bianli (Tree root) {this. root = root;} // The first Traversal method, namely, forward traversal, root -- left subtree -- right subtree public void first (Tree root) {if (Root! = Null) {System. out. println (root. data); first (root. left); first (root. right) ;}// the second Traversal method. In the middle order, traverse the-shaped traversal, from top to bottom on the right, from bottom to top public void second (Tree root) {if (root! = Null) {second (root. left); System. out. println (root. data); second (root. right );}}
// The third type of post-order traversal
Public void third (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. First, let's talk about the recursion of the Spanning Tree.

Public void insert (Tree root, int data) {// if it is larger than the root, if it is empty on the right, put it on the right of the root. Otherwise, in this case, the right side of the root is the root of the other tree, and put it to the right of the root. // use this to continue if (data> root. data) {if (root. right = null) {root. right = new Tree (data);} else {this. insert (root. right, data) ;}// else {if (root. left = null) root. left = new Tree (data); else this. insert (root. left, data );}}

Recursion is implemented in this way. If the right side of the root is empty, add the added object to the right.

Otherwise, recursion is performed, that is, the object on the right of the root is used as a root of the subtree.

In this way, we can think of a big tree as a multi-class/\ tree (Binary Tree ).

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

Binary tree traversal: three types of traversal in the code, we found that all codes are similar, but the output statements are in different positions. This need

Let's take a look at it for a moment.

We need to go back to the essence 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 recursive example.

We can see from our analysis that recursion is a method call,

For example, the above Code calls the DG method in the DG method.

We need a condition to end recursion, then calculate from the back to the front, and finally return to the place where recursion starts.

Similarly, the traversal of the preceding binary tree is the same. The three traversal codes are similar.

When we meet the ending sign, we need to complete the last second (or first or third) method.

(Abstract understanding 12345678 here I want to visualize recursion. The operation to complete 1 needs to be completed 2, and the operation to complete 2 requires 3 · and so on. Back, end recursion by the time of 8. At this time, all the operations of 8 need to be completed, and so on after 7, and finally return to the place where recursion starts)

 

The difference between the three traversal methods is that the operations after the method is executed (that is, the remaining operations of 8 (76 ).

Some continue to traverse the right, some print first, which leads to different printing results.

The freshman dog is a beginner. Sorry for the error!

Related Article

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.