// 先判断是否存在,不存在则让输入为root if (root == null) { root = new TreeNode(key); return; }
// current 代表你要插入的值 // parent 插入的父亲
TreeNode current = root; TreeNode parent = null; while (true) { parent = current; if (key < parent.value) { current = parent.left; if (current == null) { parent.left = new TreeNode(key); return; } } elseif (key > parent.value) { current = parent.right; if (current == null) { parent.right = new TreeNode(key); return; } } else { return; // BST does not allow nodes with the same value } } }
/** * 删除需要考虑三种情况 * <p> * Case 1: if node to be deleted has no children (删除节点没有子节点) * Case 2: if node to be deleted has only one child (删除节点有一个子节点,可以是左,也可以是右) * Case 3: current.left != null && current.right != null (删除节点有两个节点,左右节点都存在) * * https://www.bilibili.com/video/BV1qQ4y1M7Z4 * * @param key * @return * @insideparam parent: 用来记录上一个父亲节点 * @insideparam current:用来记录当前的节点 */ publicbooleandelete(int key){ TreeNode parent = root; TreeNode current = root; boolean isLeftChild = false; while (current != null && current.value != key) { parent = current; if (current.value > key) { isLeftChild = true; current = current.left; } else { isLeftChild = false; current = current.right; } } if (current == null) { returnfalse; } // Case 1: if node to be deleted has no children if (current.left == null && current.right == null) { if (current == root) { root = null; } elseif (isLeftChild) { parent.left = null; } else { parent.right = null; } // Case 2: if node to be deleted has only one child } elseif (current.right == null) { if (current == root) { root = current.left; } elseif (isLeftChild) { parent.left = current.left; // 和下方不一致的主要是current.left 和current.right 的区别 } else { parent.right = current.left; } } elseif (current.left == null) { if (current == root) { root = current.right; } elseif (isLeftChild) { parent.left = current.right; } else { parent.right = current.right; } // Case 3: current.left != null && current.right != null // 找右子树上的最小的节点来替代--> successor } else { // 这一步是获得最小的节点 TreeNode successor = getSuccessor(current); if (current == root) { root = successor; } elseif (isLeftChild) { parent.left = successor; } else { parent.right = successor; } successor.left = current.left; } returntrue; }
private TreeNode getSuccessor(TreeNode node){ // 找右子树上的最小的节点来替代--> successor // 同时遵循保证树形结构的完整 TreeNode successor = null; // 最后需要放上去的那个节点 TreeNode successorParent = null; TreeNode current = node.right; // 找到待删除位置的sub右子树上找到最小的点 // 这里的while循环已经保证了最小左子树,但是没法保证最小左子树是否含有右节点 while (current != null) { successorParent = successor; successor = current; current = current.left; }
Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
As a reminder, a binary search tree is a tree that satisfies these constraints:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
public TreeNode convertBST(TreeNode root){ return traverse(root); } int sum = 0; public TreeNode traverse(TreeNode root){ if (root == null) returnnull; traverse(root.right); sum = sum + root.val; root.val = sum; traverse(root.left); return root; }
Post title:二分查找树(Binary Search Tree)
Post author:Yuxuan Wu
Create time:2021-07-26 00:43:23
Post link:yuxuanwu17.github.io2021/07/26/2021-07-26-二分查找树(Binary-Search-Tree)/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.