Create a binary sort tree traversal and delete

Binary Sort/Search Tree

Why is it

Array

Unranked: added directly in the tail, speed; look slow.

Sort: binary search, find and speed; add new data, the need to find an insertion position behind the array, slowly

List

Add quick look slow;

Brief introduction

BST: (Binary Sort (Search) Tree), for any non-leaf node of a binary sort tree, the required value of left child of the current node's value is less than the value of right child is greater than the value of the current node.

Special note: if there is the same value, the node on the left or right child node subnode

For example, the previous data (7,3, 10, 12, 5, 1, 9), corresponding to binary sort tree:

Create a binary sort tree

An array created to correspond to binary sort tree, and using the preorder binary sort tree, such as: an array (7, 3, 10, 12, 5, 1, 9), created to correspond to binary sort tree Array .

Creating ideas:

Insert a node

If the value is less than the value of the root node, this time need is determined whether the left child of the root node is empty, then inserted into the empty node; not empty, add places left child as the current node, recursively

If the value is greater than the value of the root node, this time need is determined whether the right child of the root node is empty, then inserted into the empty node; is not empty, the right places to add child as the current node, recursively

In order to traverse thinking :() infixOrder

If the left child of the current node is not empty, then continue to traverse until it is empty;

First output value of the current node;

If the right child at the head node is not empty, then continue to traverse until it's empty.

Delete ideas:

Three cases:

  1. Delete leaf node (for example: 2, 5, 9, 12)

    The left and right child nodes of the node

    • Found to be abridged point: targetNode, such as 2
    • Found be abridged parent point: parent, in this case 1
    • TargetNode determination is left child or right child of parent, child node 2 1 is a right, the right child node 1 is set to null;
      • Left child node: parent.left = null
      • Right child: parent.right = null
  2. To delete a node only sub-tree (for example: 1); the only son of his father's footsteps, namely: the left or right subtree subtree will be truncated to the point of deleting the entire mobile node location

    I.e., the node has a left child or right child node

    • Found to be abridged point: targetNode, such as 1
    • Found be abridged parent point: parent, in this case 3
    • Points started to discuss: Look targetNode is left child or right child's parent
      • Left child node: Also look abridged point the child is left child or right child
        • 左孩子:parent.left = targetNode.left
        • Right child: parent.left = targetNode.right;
      • Right child: the child should look abridged point is the left child or right child
        • Left children: parent.right = targetNode.left
        • Right child: parent.right = targetNode.right;
  3. Delete There are two sub-tree node. (For example: 7, 3,10)

    That is, the node has a left child node and the right child node. Found to be truncated immediate predecessor point p (or direct successor) s, p replaced by s, and then delete the node s. How to find a direct precursor p it? The tree traversal sequence, can be found in the precursor and point abridged successor node.

    • Found to be abridged point: targetNode, such as 10
    • To find the point to be abridged parent: parent, in this case 7
    • TargetNode from the right subtree of (here find its direct successor node) find the smallest node, this time for 12; if we are looking predecessor node, it would need to find the largest node in the left subtree.
    • Temp with a temporary variable to save the minimum node, i.e., temp = 12;
    • Summary delete the most points
    • Let value be abridged point becomes the value of temp, targetNode.value = temp;

Code

```java
package algorithm;

/**

  • @ClassName: Demo21_BinarySortTree
  • @author: benjamin
  • @version: 1.0
  • @description: additions and deletions binary sort tree traversal
  • @createTime: 2019/08/27/11:29
    */

public class Demo21_BinarySortTree {

static void main public (String [] args) {
int [] = {ARR. 7,. 3, 10, 12 is,. 5,. 1,. 9, 2};
BinarySortTree binarySortTree new new BinarySortTree = ();
// add to the junction of loop binary sort tree
for (int I = 0; I <arr.length; I ++) {
binarySortTree.add (new new the Node (ARR [I]));
}

//中序遍历二叉排序树
System.out.println("中序遍历二叉排序树~");
binarySortTree.infixOrder(); // 1, 3, 5, 7, 9, 10, 12

//测试一下删除叶子结点

binarySortTree.delNode(12);

binarySortTree.delNode(5);
binarySortTree.delNode(10);
binarySortTree.delNode(2);
binarySortTree.delNode(3);

binarySortTree.delNode(9);
binarySortTree.delNode(1);
binarySortTree.delNode(7);

System.out.println("root=" + binarySortTree.getRoot());

System.out.println("删除结点后");
binarySortTree.infixOrder();

}

}

// create a binary sort tree
class BinarySortTree {

private Node root;

public Node getRoot() {
return root;
}

// Find the node to be deleted
public the Node Search (int value) {
IF (the root == null) {
return null;
} the else {
return root.search (value);
}
}

//查找父结点
public Node searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}

: // writing method
// 1 Summary points most value in return for the root node of the binary sort tree.
The most summary point // 2 deleted node is the root node of the binary sort tree.

/**

  • @param node incoming node (the root node as the binary sort tree)
  • @return node returns a value to the root node points most summary binary sort tree
    * /
    public int delRightTreeMin (the Node node) {
    the Node = target node;
    // Find the left child node cycle, will find the minimum value
    the while (target.left = null!) {
    target = target.left;
    }
    // At this points to the target point most summary
    // remove most point Summary
    delNode (Target.Value);
    return Target.Value;
    }

// delete nodes
public void delNode (int value) {
IF (the root == null) {
return;
} {the else
. //. 1 needs to go to find the node to be deleted targetNode
the Node Search targetNode = (value);
// If you do not find the node you want to delete
IF (targetNode == null) {
return;
}
// If we find that the current binary sort tree Fengyun only one node
if (root.left == null && root.right == null ) {
the root = null;
return;
}

  //去找到targetNode的父结点
  Node parent = searchParent(value);
  //如果要删除的结点是叶子结点
  if (targetNode.left == null && targetNode.right == null) {
    //判断targetNode 是父结点的左子结点,还是右子结点
    if (parent.left != null && parent.left.value == value) { //是左子结点
      parent.left = null;
    } else if (parent.right != null && parent.right.value == value) {//是由子结点
      parent.right = null;
    }
  } else if (targetNode.left != null && targetNode.right != null) { //删除有两颗子树的节点
    int minVal = delRightTreeMin(targetNode.right);
    targetNode.value = minVal;


  } else { // 删除只有一颗子树的结点
    //如果要删除的结点有左子结点
    if (targetNode.left != null) {
      if (parent != null) {
        //如果 targetNode 是 parent 的左子结点
        if (parent.left.value == value) {
          parent.left = targetNode.left;
        } else { //  targetNode 是 parent 的右子结点
          parent.right = targetNode.left;
        }
      } else {
        root = targetNode.left;
      }
    } else { //如果要删除的结点有右子结点
      if (parent != null) {
        //如果 targetNode 是 parent 的左子结点
        if (parent.left.value == value) {
          parent.left = targetNode.right;
        } else { //如果 targetNode 是 parent 的右子结点
          parent.right = targetNode.right;
        }
      } else {
        root = targetNode.right;
      }
    }

  }

}

}

The method of adding the node //
public void the Add (the Node Node) {
IF (root == null) {
root = Node; // null if the root is directed directly from root Node
} the else {
root.add (Node);
}
}

Inorder traversal //
public void infixOrder () {
IF (! The root = null) {
root.infixOrder ();
} the else {
System.out.println ( "binary sort tree is empty, can not traverse");
}
}
}

// Create Node node
class Node {

int value;
Node left;
Node right;

public Node(int value) {

this.value = value;

}

// Find the node to be deleted

/**

  • @param value desired value nodes removed
  • @return the node returned if found, otherwise return null
    * /
    public the Node Search (int value) {
    IF (this.value == value) {// find the node that is
    ; the this return
    } the else IF (value <the this. value) {// If the value is less than the current node to find, left subtree recursive search
    // If the left child node is empty
    IF (this.left == null) {
    return null;
    }
    return this.left.search ( value);
    } Find the else {// If the value is not less than the current node, the right subtree recursive search
    IF (this.right == null) {
    return null;
    }
    return this.right.search (value);
    }

}
// Find To delete a node's parent node

/**

  • @Param value the value of the node to be found
  • @return returns the parent node of the node to be deleted, if not return null
    * /
    public searchParent the Node (int value) {
    // If the current node is the parent node to remove the node, it returns
    if (! (this.left = value == null && this.left.value) ||
    (this.right = null && this.right.value == value)!) {
    return the this;
    } the else {
    // If the queried value is less than the value of the current node, and the left child node of the current node is not empty
    IF (value <this.value this.left = null &&!) {
    return this.left.searchParent (value); // left recursive search subtree
    } the else IF (value> = this.value this.right = null &&!) {
    return this.right.searchParent (value); // right subtree recursive search
    } the else {
    return null; // no find the parent node
    }
    }

}

@Override
public String toString() {
return "Node [value=" + value + "]";
}

The method of adding node //
// recursive form of added nodes, attention to the need to meet the requirements of binary sort tree
public void the Add (the Node Node) {
IF (Node == null) {
return;
}

//判断传入的结点的值,和当前子树的根结点的值关系
if (node.value < this.value) {
  //如果当前结点左子结点为null
  if (this.left == null) {
    this.left = node;
  } else {
    //递归的向左子树添加
    this.left.add(node);
  }
} else { //添加的结点的值大于 当前结点的值
  if (this.right == null) {
    this.right = node;
  } else {
    //递归的向右子树添加
    this.right.add(node);
  }

}

}

//中序遍历
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
}
```1566871739222.png

Guess you like

Origin www.cnblogs.com/benjieqiang/p/11417392.html