Algorithm - Sequential Binary Tree

package com.demo.calculate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.demo.calculate.bean.BinaryTree;
import com.demo.calculate.bean.TreeNode;

public class BinarySortATreectivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binary_sort_tree);
        findViewById(R.id.btn_binarySortTree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createTree();
            }
        });
    }

    private void createTree() {
        BinaryTree binaryTree = new BinaryTree();
        int[] arr = new int[]{7,3,5,9,12,45,23,10};
        for (int i = 0; i <arr.length ; i++) {
            binaryTree.add(new TreeNode(arr[i]));
        }
        binaryTree.midShow();
    }
}

 

package com.demo.calculate.bean;

public class BinaryTree {
    private int value;
    private TreeNode root;
    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public void frontShow() {
        if (root!=null){
            root.frontShow();
        }
    }

    public void midShow() {
        if (root!=null){
            root.midShow();
        }

    }

    public void afterShow() {
        if (root!=null){
            root.afterShow();
        }
    }

    public TreeNode frontSearch(int i) {
        if (root!=null){
            return root.frontSearch(i);
        }
        return  null;
    }

    public void deleteNode(int i) {
        if (root!=null){
            root.deleteNode(i);
        }
    }
    public void add(TreeNode node){
        if (root == null){
            root = node;
        }else{
            root.add(node);
        }
    }

}

 

com.demo.calculate.bean Package; 

Import android.util.Log; 

public class the TreeNode { 
    Private int value; 
    Private the TreeNode nodeLeft; 
    Private the TreeNode nodeRight; 
    public void setNodeLeft (the TreeNode nodeLeft) { 
        this.nodeLeft = nodeLeft; 
    } 
    public void setNodeRight (the TreeNode nodeRight) { 
        this.nodeRight = nodeRight; 
    } 
    public the TreeNode (int value) { 
        this.value = value; 
    } 

    public void midShow () { 
        / ** 
         * 2. 5. 4. 3. 6. 7 1 
         * 1 1 The left point 2 is a right node is a node midShow. 3 (); 
         * 2 2 The point is the left node is the right node 4 is midShow. 5 (); 
         * /
        IF (nodeLeft = null!) { 
            nodeLeft.midShow (); 
        } 
        Log.i ( "Tag", value + ""); 
        // root node 2 is the first time value. 4 
        IF (! nodeRight = null) { 
            nodeRight.midShow (); 
        } 
    } 
    public void the Add (the TreeNode node) { 
        IF (node == null) { 
            return; 
        } 
        // node to be inserted determination value is not smaller than the value of the current node, as the left, then to the small child node 
        IF (node.value <value) { 
            IF (nodeLeft == null) { 
                nodeLeft = node; 
            } the else { 
                nodeLeft.add (node); 
            } 
         // determination value node to be inserted is not the current node value ratio large, small, then to the right child node as 
        } else {
            if (nodeRight==null){
                nodeRight = node;
            }else{
                nodeRight.add(node);
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/xjz19930319/article/details/92803638