package com.structure.demo; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class BinarySortTreeActivity extends Activity { private int[] array = {7, 3, 10, 12, 5, 1, 9}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BinarySortTree binarySortTree = new BinarySortTree(); for (int i = 0; i < array.length; i++) { binarySortTree.add(new SortTreeNode(array[i])); } // binarySortTree.preOrder(); /* SortTreeNode searchNode = binarySortTree.search(7); Log.i("tag", searchNode.toString());*/ // SortTreeNode sortTreeNode = binarySortTree.searchParent(8); binarySortTree.preOrder(); } } class BinarySortTree { private SortTreeNode root; public void add(SortTreeNode node) { if (root == null) { root = node; } else { root.add(node); } } /** * 查找节点 * * @param value 要查找的节点的值 * @return */ public SortTreeNode search(int value) { if (root == null) { return null; } return root.search(value); } /** * 前序遍历 */ public void preOrder() { if (root == null) { Log.i("tag","二叉树为空,不能遍历"); return; } root.preOrder(); } /** * 查找父节点 */ public SortTreeNode searchParent(int value) { if (root == null) { return null; } return root.searchParent(value); } /** * @param value 要删除的节点的值 * 1、删除的节点是叶子节点 * 2、删除的节点是只有一个叶子节点的根节点 * 3、删除是有两个子节点的根节点 */ public void delNode(int value) { if (root == null) { return; } else { //找到要删除的节点 targetNode SortTreeNode targetNode = search(value); //如果没找到要删除的节点 if (targetNode == null) { return; } //如果我们发现这个二叉排序树只有一个节点 if (root.left == null && root.right == null) { root = null; return; } //去找到targetNode的父节点 SortTreeNode parent = searchParent(value); //要删除的节点是叶子节点 if (targetNode.left == null && targetNode.right == null) { //判断targetNode是父节点的左子节点还是右子节点 if (parent != null && parent.left.value == value) { parent.left = null; } else if (parent != null && parent.right.value == value) { parent.right = null; } //删除的是有两个子节点的根节点 } else if (targetNode.left != null && targetNode.right != null) { //删除的是只有一个子节点的根节点 //这个时候我们需要找到右子树中最小的值或者是左子树中最大的值来替换这个值即可 int minValue = delRightTreeMin(targetNode.right); targetNode.value = minValue; } else { //如果删除的节点有左子节点 if (targetNode.left != null) { if (parent != null) { if (parent.left.value == value) { parent.left = targetNode.left; } else { parent.right = targetNode.left; } } else { root = targetNode.left; } } else { if (parent != null) { if (parent.left.value == value) { parent.left = targetNode.right; } else { parent.right = targetNode.right; } } else { root = targetNode.right; } } } } } /** * @param node 传入的节点(当做二叉排序树的根节点) * @return 返回的以node为根节点的二叉排序树的最小节点的值 */ public int delRightTreeMin(SortTreeNode node) { SortTreeNode target = node; //循环查找左子节点,就会找到最小值 while (target.left != null) { target = target.left; } //这个时候target就指向了最小值 //删除这个最小节点 delNode(target.value); return target.value; } } class SortTreeNode { int value; SortTreeNode left; SortTreeNode right; public SortTreeNode(int value) { this.value = value; } @Override public String toString() { return "SortTreeNode{" + "value=" + value + '}'; } /** * 创建一棵二叉排序树 */ public void add(SortTreeNode node) { if (node == null) { return; } //当前节点的值小于要插入的值,这个时候要把节点插入到当前节点的右面 if (this.value < node.value) { if (this.right == null) { this.right = node; } else { this.right.add(node); } } else { if (this.left == null) { this.left = node; } else { this.left.add(node); } } } /** * 前序遍历 */ public void preOrder() { Log.i("tag", this.value + "========="); if (this.left != null) { this.left.preOrder(); } if (this.right != null) { this.right.preOrder(); } } /** * 查找要删除的节点 */ public SortTreeNode search(int value) { if (this.value == value) { return this; } //要查找的值小于当前节点的值那么就向左面查找 if (value < this.value) { if (this.left.value == value) { return this.left; } else { return this.left.search(value); } //要查找的值大于或者等于当前节点的值向右查找 } if (value >= this.value) { if (this.right.value == value) { return this.right; } else { return this.right.search(value); } } return null; } /** * 查找要删除的节点的父节点 * * @param value 要删除的节点的值 */ public SortTreeNode searchParent(int value) { if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) { return this; } else { //如果当前节点的值小于 if (this.value < value) { if (this.right != null) { return this.right.searchParent(value); } } if (this.value > value) { if (this.left != null) { return this.left.searchParent(value); } } } return null; } }
09-17
09-17