package search1;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author CSDN@日星月云
* @date 2022/10/31 10:20
*/
class BSTNode {
int key;
BSTNode lChild;
BSTNode rChild;
public BSTNode() {
}
public BSTNode(int key) {
this.key = key;
}
public BSTNode(int key, BSTNode lChild, BSTNode rChild) {
this.key = key;
this.lChild = lChild;
this.rChild = rChild;
}
@Override
public String toString() {
return "BSTNode{" +
"key=" + key +
", lChild=" + lChild +
", rChild=" + rChild +
'}';
}
}
class BsTree {
BSTNode root;
BsTree(){
root=null;
}
//用扩展先序遍历序列创建二叉链表
//依靠队列,当队首元素分配了左右孩子就出队
public BSTNode setTree(Integer[] data)//data为传入的形参数组,如Integer[] nums = {1,2,null,4,null,5}
{
root = new BSTNode(data[1]);
Queue<BSTNode> queue = new LinkedList<>();//先入先出,构造子树
queue.offer(root);
int i = 2;//从第二个元素开始入列
while(i<data.length)
{
BSTNode node = queue.poll();//弹出队列中的树节点
if(data[i] != null)//当实参的元素不为null时,添加左节点
{
node.lChild = new BSTNode(data[i]);
queue.offer(node.lChild);
}
i++;//不管实参(传入Integer[] data的Integer数组)的元素是不是null,均需要后移一位
if(data[i] != null)//当实参的元素不为null时,添加右节点
{
node.rChild = new BSTNode(data[i]);
queue.offer(node.rChild);
}
i++;
}
return root;//返回根节点
}
//访问
void visit(int n){
System.out.print(n+" ");
}
// 递归 中序
void inOrder(BSTNode root){
if (root!=null){
inOrder(root.lChild);
visit(root.key);
inOrder(root.rChild);
}
}
//基于二叉排序树查找的非递归实现
BSTNode searchBSTN(BSTNode root, int k){
BSTNode q;
q=root;
while (q!=null){
if(q.key==k){
return q;
}
if (k<q.key){
q=q.lChild;
}else{
q=q.rChild;
}
}
return null;
}
//基于二叉排序树查找的递归实现
BSTNode searchBST(BSTNode root, int k){
if (root==null) return null;
else if(root.key==k) return root;
else if(k<root.key) return searchBST(root.lChild, root.key);
else return searchBST(root.rChild, k);
}
//二叉排序树的插入
void insertBST(BSTNode root,int k){
BSTNode s;
if (root==null){
s=new BSTNode(k);
root=s;//值传递机制赋值失败
}
else if(k<root.key) {insertBST(root.lChild,k);}
else if(k>root.key) {insertBST(root.rChild,k);}
}
//二叉排序树的插入
public void insert(int k){
BSTNode newNode=new BSTNode(k);
if (root==null){
root=newNode;
}
else{
BSTNode current = root; // start at root
BSTNode parent;
while (true) {//(exits internally)
parent = current;
if (k < current.key) {// go left?
current = current.lChild;
if (current == null) {// if end of the line,
// insert on left
parent.lChild = newNode;
return;
}
}// end if go left
else {// or go right?
current = current.rChild;
if (current == null) {// if end of the line
// insert on right
parent.rChild = newNode;
return;
}
} // end else go right
} // end while
}
}
//二叉排序树的创建
public void create(int []nums){
for (int i = 0; i < nums.length; i++) {
insert(nums[i]);
}
}
public boolean delete(int key) { //delete node with given key
// (assumes non-empty list)
BSTNode current = root;
BSTNode parent = root;
boolean isLeftChild = true;
while (current.key != key) {// search for node
parent = current;
if (key < current.key) { // go left?
isLeftChild = true;
current = current.lChild;
} else {
// or go right?
isLeftChild = false;
current = current.rChild;
}
if (current == null)// end of the line,
return false; // didn't find it
} // end while
// found node to delete
// if no children, simply delete it
if (current.lChild == null && current.rChild == null) {
if (current == root)// if root,
root = null;// tree is empty
else if (isLeftChild)
parent.lChild = null;// disconnect
else// from parent
parent.rChild = null;
}
// if no right child, replace with left subtree
else if (current.rChild == null) {
if (current == root) root = current.lChild;
else if (isLeftChild)
parent.lChild = current.lChild;
else
parent.rChild = current.lChild;
}
// if no left child, replace with right subtree
else if (current.lChild == null) {
if (current == root)
root = current.rChild;
else if (isLeftChild) parent.lChild = current.rChild;
else
parent.rChild = current.rChild;
} else {// two children, so replace with inorder successor
// get successor of node to delete (current)
BSTNode successor = getSuccessor(current);
// connect parent of current to successor instead
if (current == root)
root = successor;
else if (isLeftChild)
parent.lChild = successor;
else
parent.rChild = successor;
// connect successor to current's left child
successor.lChild = current.lChild;
// end else two children// (successor cannot have a left child)
}
return true; // success
}// end delete()
// returns node with next-highest value after delNode
// goes to right child, then right child's left descendents
private BSTNode getSuccessor(BSTNode delNode) {
BSTNode successorParent = delNode;
BSTNode successor = delNode;
BSTNode current = delNode.rChild;// go to right child
while (current != null) {// until no more
// left children,
successorParent = successor;
successor = current;// go to left child
current = current.lChild;
}
// if successor not
if (successor != delNode.rChild) {// right child,
// make connections
successorParent.lChild = successor.rChild;
successor.rChild = delNode.rChild;
}
return successor;
}
}
public class SearchBSt {
public static void main(String[] args) {
//用扩展先序遍历序列创建二叉链表,手动创建二叉搜索数
Integer[] nums={null,44,21,65,14,32,58,72,null,null,null,null,null,null,null,80};
BsTree bsTree = new BsTree();
bsTree.setTree(nums);
bsTree.inOrder(bsTree.root);//14 21 32 44 58 65 72 80
System.out.println();
//测试基于二叉排序树查找的非递归实现
System.out.println(bsTree.searchBSTN(bsTree.root, 80));//BSTNode{key=80, lChild=null, rChild=null}
//测试基于二叉排序树查找的递归实现
System.out.println(bsTree.searchBST(bsTree.root, 80));//BSTNode{key=80, lChild=null, rChild=null}
//测试二叉排序树的插入
// new SearchBSt().insertBST(bsTree.root, 69);
// bsTree.inOrder(bsTree.root);//14 21 32 44 58 65 72 80
//测试二叉排序树的插入
bsTree.insert(69);
bsTree.inOrder(bsTree.root);//14 21 32 44 58 65 69 72 80
System.out.println();
//测试二叉排序树的建立
BsTree bst=new BsTree();
int[] numbers={44,21,65,14,32,58,72,80};
bst.create(numbers);
bst.inOrder(bst.root);//14 21 32 44 58 65 72 80
System.out.println();
//测试二叉排序树的删除
// bst.delete(14);
// bst.inOrder(bst.root);//14 21 32 44 58 65 72 80
// System.out.println();//21 32 44 58 65 72 80
// bst.delete(80);
// bst.inOrder(bst.root);//14 21 32 44 58 65 72 80
// System.out.println();//14 21 32 44 58 65 72
// bst.delete(21);
// bst.inOrder(bst.root);//14 21 32 44 58 65 72 80
// System.out.println();//14 32 44 58 65 72 80
// bst.delete(72);
// bst.inOrder(bst.root);//14 21 32 44 58 65 72 80
// System.out.println();//14 21 32 44 58 65 80
bst.delete(32);
bst.inOrder(bst.root);//14 21 32 44 58 65 72 80
System.out.println();//14 21 44 58 65 72 80
}
}