Java Self-Study-Collection Framework Binary Tree

Java Collection Framework Binary Tree

Example 1: Binary tree concept

A binary tree consists of nodes
Binary Tree Features:
Each node can have left and right child nodes
Each node has a value

package collection;
 
public class Node {
    // Left Child Node
    public Node leftNode;
    // Right Child Node
    public Node rightNode;
    // value
    public Object value;
}

Example 2: Binary tree sorting - inserting data

Suppose you sort the following 10 random numbers by a binary tree
67,7,30,73,10,0,78,81,10,74
The first step in sorting is to insert data into the binary tree
The basic logic of insertion is that small, the same, are placed on the left and large on the right

  1. 67 at the root node
  2. 7 smaller than 67, placed on the left node of 67
  3. 30 is smaller than 67, find the left node of 67 7, 30 is larger than 7, put it on the right node of 7
  4. 73 is larger than 67, placed on the right node of 67
  5. 10 is smaller than 67, find 67 left node 7, 10 is larger than 7, find 7 right node 30, 10 is smaller than 30, put in 30 left node.
    ...
    ...
  6. 10 is smaller than 67, find 67 left node 7, 10 is larger than 7, find 7 right node 30, 10 is smaller than 30, find 30 left node 10, 10 is the same size as 10, put on the left

     package collection;
    
     public class Node {
         // Left Child Node
         public Node leftNode;
         // Right Child Node
         public Node rightNode;
    
         // value
         public Object value;
    
         // Insert Data
         public void add(Object v) {
             // If the current node has no value, place the data on the current node
             if (null == value)
                 value = v;
    
         // If the current node has a value, make a judgment about the size of the new value in relation to the current value
         else {
             // New value, less than or equal to the current value
    
             if ((Integer) v -((Integer)value) <= 0) {
                 if (null == leftNode)
                     leftNode = new Node();
                 leftNode.add(v);
             }
             // New value, greater than current value
             else {
                 if (null == rightNode)
                     rightNode = new Node();
                 rightNode.add(v);
             }
    
         }
    
     }
    
     public static void main(String[] args) {
    
         int randoms[] = new int[] { 67, 7, 30, 73, 10, 0, 78, 81, 10, 74 };
    
         Node roots = new Node();
         for (int number : randoms) {
             roots.add(number);
         }
    
      }
     }

Example 3: Binary tree sorting-traversal

With the insertion behavior of the previous step, the data is actually sorted.The next thing to do is to iterate through these sorted data into lists or arrays that we commonly use

Traversal of a binary tree is in left, middle, and right order
Left order means that the middle number is traversed and placed on the left
The middle order is: the middle number is traversed and placed in the middle
The right order is: the middle number is traversed and placed on the right
As you can see from the diagram, we want the results to be small to large, so we should use a middle-order traversal

package collection;
 
import java.util.ArrayList;
import java.util.List;
 
public class Node {
    // Left Child Node
    public Node leftNode;
    // Right Child Node
    public Node rightNode;
  
    // value
    public Object value;
  
    // insert data
    public void add(Object v) {
        // If the current node has no value, place the data on the current node
        if (null == value)
            value = v;
  
        // If the current node has a value, make a judgment about the size of the new value in relation to the current value
        else {
            // New value, less than or equal to the current value
             
            if ((Integer) v -((Integer)value) <= 0) {
                if (null == leftNode)
                    leftNode = new Node();
                leftNode.add(v);
            }
            // New value, greater than current value
            else {
                if (null == rightNode)
                    rightNode = new Node();
                rightNode.add(v);
            }
  
        }
  
    }
  
 // Traverse all nodes in middle order
    public List<Object> values() {
        List<Object> values = new ArrayList<>();
  
        // Traversal result of left node
        if (null != leftNode)
            values.addAll(leftNode.values());
  
        // Current Node
        values.add(value);
  
        // Traversal result of right node
        if (null != rightNode)
  
            values.addAll(rightNode.values());
  
        return values;
    }
  
    public static void main(String[] args) {
  
        int randoms[] = new int[] { 67, 7, 30, 73, 10, 0, 78, 81, 10, 74 };
  
        Node roots = new Node();
        for (int number : randoms) {
            roots.add(number);
        }
  
        System.out.println(roots.values());
  
    }
}

Practice: Hero Binary Tree

Based on the above learning and understanding, design a Hero binary tree, HeroNode.
You can insert different Hero objects into this Hero Binary Tree and sort them in reverse order according to Hero's blood volume.

Ten Hero objects were randomly generated, each with a different blood volume. After inserting this HeroNode, the sorting results were printed.

package collection;
 
import java.util.ArrayList;
import java.util.List;
 
import charactor.Hero;
 
public class HeroNode {
 
    public HeroNode leftHero;
 
    public HeroNode rightHero;
 
    // Declare as Hero type
    public Hero value;
 
    public void add(Hero v) {
 
        if (null == value)
            value = v;
 
        else {
 
            // If the new hero has more blood than this node, put it on the left
            if (v.hp > value.hp) {
                if (null == leftHero)
                    leftHero = new HeroNode();
                leftHero.add(v);
            }
 
            else {
                if (null == rightHero)
                    rightHero = new HeroNode();
                rightHero.add(v);
            }
 
        }
 
    }
 
    public List<Object> values() {
        List<Object> values = new ArrayList<>();
 
        if (null != leftHero)
            values.addAll(leftHero.values());
 
        values.add(value);
 
        if (null != rightHero)
 
            values.addAll(rightHero.values());
 
        return values;
    }
 
    public static void main(String[] args) {
 
        List<Hero> hs = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Hero h = new Hero();
            h.name = "hero " + i;
            h.hp = (float) (Math.random() * 900 + 100); // 100-1000 Random Blood Volume
            hs.add(h);
        }
        System.out.println("Initialize 10 Hero");
        System.out.println(hs);
 
        HeroNode heroTree = new HeroNode();
        for (Hero hero : hs) {
            heroTree.add(hero);
        }
        System.out.println("Reversed by blood volume Hero");
        List<Object> treeSortedHeros = heroTree.values();
        System.out.println(treeSortedHeros);
 
    }
}

Tags: Java less

Posted on Thu, 28 Nov 2019 20:42:24 -0500 by sinbad