Binary Tree of Java

Source: Internet
Author: User

Reference Http://how2j.cn/k/collection/collection-tree/476.html#nowhere

Binary Tree Concept

A binary tree is made up of a variety of nodes
Binary Tree Features:
Each node can have left dial hand nodes, right child nodes
Each node has a value

 Public class Node {    //  left dial hand nodes public         leftnode; // Right child node     Public Node Rightnode;     // value     Public Object value;}
Binary tree sorting-inserting data

Suppose that the following 10 random numbers are sorted by a binary tree
67,7,30,73,10,0,78,81,10,74
The first step in sorting is to insert the data into the two fork tree
Insert basic logic is, small, the same on the left, Big put on the right
1.67 on the root node
2.7:67 small, placed on the left node of 67
3.30:67 small, find 67 left node 7, 30:7 Large, put on the right node of 7
4.73:67 large, placed on 67 right node
5.10:67 Small, find 67 left node 7, 10:7 Large, find 7 of the right node 30, 10:30 small, placed on the left node of 30.
...
...
9.10:67 Small, find 67 left node 7, 10:7 Large, find 7 right node 30, 10:30 Small, find 30 left node 10,10 and 101 like large, put on the left

Package collection;  Public classNode {//left dial hand node     PublicNode Leftnode; //Right child node     PublicNode Rightnode; //value     PublicObject value; //Inserting Data     Public voidAdd (Object v) {//If the current node has no value, put the data on the current node        if(NULL==value) value=v; //If the current node has a value, it is judged that the new value is in relation to the size of the current value        Else {            //new value, smaller or the same than the current value                         if(integer) v-((integer) value) <=0) {                if(NULL==leftnode) Leftnode=NewNode ();            Leftnode.add (v); }            //new value, larger than current value            Else {                if(NULL==rightnode) Rightnode=NewNode ();            Rightnode.add (v); }          }      }       Public Static voidMain (string[] args) {intRandoms[] =New int[] { the,7, -, the,Ten,0, +,Bayi,Ten, About }; Node Roots=NewNode ();  for(intnumber:randoms)        {roots.add (number); }      }}
Binary Tree Sort-traverse

with the insertion behavior of the previous step, the data is actually sorted out. the next thing to do is to look at these already sequenced data and traverse them into the form of our usual list or array.

The traversal of binary tree is left sequence, middle order, right order
Zoo Xu that is: The middle number is traversed and placed on the left
The middle sequence is: The median number is traversed and placed in the middle
Right order: The middle number is traversed and placed on the right
As we can see, we hope that the result of the traversal is small to large, so we should use the middle sequence traversal

Package collection; import Java.util.arraylist;import java.util.List; Public classNode {//left dial hand node     PublicNode Leftnode; //Right child node     PublicNode Rightnode; //value     PublicObject value; //Inserting Data     Public voidAdd (Object v) {//If the current node has no value, put the data on the current node        if(NULL==value) value=v; //If the current node has a value, it is judged that the new value is in relation to the size of the current value        Else {            //new value, smaller or the same than the current value                         if(integer) v-((integer) value) <=0) {                if(NULL==leftnode) Leftnode=NewNode ();            Leftnode.add (v); }            //new value, larger than current value            Else {                if(NULL==rightnode) Rightnode=NewNode ();            Rightnode.add (v); }          }      }   //middle order traversal of all nodes     PublicList<object>values () {List<Object> values =NewArraylist<>(); //The traversal result of the left node        if(NULL!=leftnode) Values.addall (Leftnode.values ()); //Current NodeValues.add (value); //The traversal result of the right node        if(NULL!=rightnode) Values.addall (Rightnode.values ()); returnvalues; }       Public Static voidMain (string[] args) {intRandoms[] =New int[] { the,7, -, the,Ten,0, +,Bayi,Ten, About }; Node Roots=NewNode ();  for(intnumber:randoms)        {roots.add (number); } System. out. println (Roots.values ()); }}
Exercise-Compare bubble method, selection method and two-fork tree sorting performance Differences

Wait to do

Binary Tree of Java

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.