package com.demo.calculate; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import com.demo.calculate.bean.BinaryTree; import com.demo.calculate.bean.TreeNode; /** * @author xjz */ 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[]{1,2,3,4,5,6,7,8}; for (int i = 0; i < arr.length; i++) { binaryTree.add(new TreeNode(arr[i])); } binaryTree.midShow(); binaryTree.rightRotate(); 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); } } public int height() { if (root == null){ return 0; } return root.height(); } public void rightRotate() { if (root == null){ return; } root.rightRotate(); } }
package com.demo.calculate.bean; import android.support.annotation.NonNull; import android.util.Log; /** * @author xjz */ public class TreeNode implements Comparable<TreeNode> { private int value; private TreeNode nodeLeft; private TreeNode nodeRight; public void setNodeLeft(TreeNode nodeLeft) { this.nodeLeft = nodeLeft; } public void setNodeRight(TreeNode nodeRight) { this.nodeRight = nodeRight; } public TreeNode(int value) { this.value = value; } public void frontShow() { Log.i("tag", value + ""); if (nodeLeft != null) { nodeLeft.frontShow(); } if (nodeRight != null) { nodeRight.frontShow(); } } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public void midShow() { /** * 4 2 5 1 6 3 7 * 1 根据点是1 左节点是2 右节点是3 midShow(); * 2 根据点是2 左节点是4 右节点是5 midShow(); */ if (nodeLeft != null) { nodeLeft.midShow(); } Log.i("tag", value + ""); //根节点是2 第一次的时候value是4 if (nodeRight != null) { nodeRight.midShow(); } } public void afterShow() { if (nodeLeft != null) { nodeLeft.afterShow(); } if (nodeRight != null) { nodeRight.afterShow(); } Log.i("tag", value + ""); } //前序查找 public TreeNode frontSearch(int i) { TreeNode target = null; if (this.value == i) { return this; } else { if (nodeLeft != null) { target = nodeLeft.frontSearch(i); } if (target != null) { return target; } if (nodeRight != null) { target = nodeRight.frontSearch(i); } } return target; } public void deleteNode(int i) { TreeNode parent = this; if (this.value == i) { parent = null; } //判断左儿子 if (parent != null && parent.nodeLeft != null && parent.nodeLeft.value == i) { parent.nodeLeft = null; return; } //判断右儿子 if (parent != null && parent.nodeRight != null && parent.nodeRight.value == i) { parent.nodeRight = null; return; } //递归删除左面的儿子 parent = nodeLeft; if (parent != null) { parent.deleteNode(i); } //递归删除右面的儿子 parent = nodeRight; if (parent != null) { parent.deleteNode(i); } } @Override public int compareTo(@NonNull TreeNode o) { return value - o.value; } public void add(TreeNode node) { if (node == null) { return; } //判断要插入的节点的值是不是比当前节点的值小,小的话就作为左子节点 if (node.value < value) { if (nodeLeft == null) { nodeLeft = node; } else { nodeLeft.add(node); } //判断要插入的节点的值是不是比当前节点的值大,小的话就作为右子节点 } else { if (nodeRight == null) { nodeRight = node; } else { nodeRight.add(node); } } } public int height() { return Math.max(getLeftHeight(), getRightHeight()) + 1; } /** * 获取左子数的高度 */ public int getLeftHeight() { if (nodeLeft == null) { return 0; } return nodeLeft.height(); } /** * 获取右子数的高度 * * @return */ public int getRightHeight() { if (nodeRight == null) { return 0; } return nodeRight.height(); } /** * 二叉树的右旋转 */ public void rightRotate() { //创建一个新的节点 TreeNode newRight = new TreeNode(value); //把当前节点的右子树设置为新节点的右子树 newRight.nodeRight = nodeRight; //把当前节点的左子树的右子树设置为新节点的左子树 newRight.nodeLeft = nodeLeft.nodeRight; //把当前节点的值设置为左子树的值 value = nodeLeft.value; //把当前节点的左子树设置为左子树的左子树 nodeLeft = nodeLeft.nodeLeft; //把新节点设置为当前节点的右子树 nodeRight = newRight; } /** * 二叉树的左旋转旋转 */ public void leftRotate() { TreeNode newLeft = new TreeNode(value); newLeft.nodeLeft = nodeLeft; newLeft.nodeRight = nodeRight.nodeLeft; value = nodeRight.value; nodeRight = nodeRight.nodeRight; nodeLeft = newLeft; } }