mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -14,56 +14,56 @@ class BinarySearchTree {
|
||||
|
||||
/* Constructor */
|
||||
public BinarySearchTree() {
|
||||
// Initialize empty tree
|
||||
// Initialize an empty tree
|
||||
root = null;
|
||||
}
|
||||
|
||||
/* Get binary tree root node */
|
||||
/* Get the root of the binary tree */
|
||||
public TreeNode getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
/* Find a node */
|
||||
public TreeNode search(int num) {
|
||||
TreeNode cur = root;
|
||||
// Loop search, exit after passing leaf node
|
||||
// Search in a loop until reaching a null reference beyond a leaf
|
||||
while (cur != null) {
|
||||
// Target node is in cur's right subtree
|
||||
// The target node lies in the right subtree of cur
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// Target node is in cur's left subtree
|
||||
// The target node lies in the left subtree of cur
|
||||
else if (cur.val > num)
|
||||
cur = cur.left;
|
||||
// Found target node, exit loop
|
||||
// The target node was found; exit the loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Return target node
|
||||
// Return the target node
|
||||
return cur;
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
/* Insert a node */
|
||||
public void insert(int num) {
|
||||
// If tree is empty, initialize root node
|
||||
// If the tree is empty, initialize the root node
|
||||
if (root == null) {
|
||||
root = new TreeNode(num);
|
||||
return;
|
||||
}
|
||||
TreeNode cur = root, pre = null;
|
||||
// Loop search, exit after passing leaf node
|
||||
// Search in a loop until reaching a null reference beyond a leaf
|
||||
while (cur != null) {
|
||||
// Found duplicate node, return directly
|
||||
// Return immediately if a duplicate node is found
|
||||
if (cur.val == num)
|
||||
return;
|
||||
pre = cur;
|
||||
// Insertion position is in cur's right subtree
|
||||
// The insertion position lies in the right subtree of cur
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// Insertion position is in cur's left subtree
|
||||
// The insertion position lies in the left subtree of cur
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// Insert node
|
||||
// Insert a node
|
||||
TreeNode node = new TreeNode(num);
|
||||
if (pre.val < num)
|
||||
pre.right = node;
|
||||
@@ -71,53 +71,53 @@ class BinarySearchTree {
|
||||
pre.left = node;
|
||||
}
|
||||
|
||||
/* Remove node */
|
||||
/* Remove a node */
|
||||
public void remove(int num) {
|
||||
// If tree is empty, return directly
|
||||
// Return immediately if the tree is empty
|
||||
if (root == null)
|
||||
return;
|
||||
TreeNode cur = root, pre = null;
|
||||
// Loop search, exit after passing leaf node
|
||||
// Search in a loop until reaching a null reference beyond a leaf
|
||||
while (cur != null) {
|
||||
// Found node to delete, exit loop
|
||||
// The node to remove was found; exit the loop
|
||||
if (cur.val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// Node to delete is in cur's right subtree
|
||||
// The node to remove lies in the right subtree of cur
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// Node to delete is in cur's left subtree
|
||||
// The node to remove lies in the left subtree of cur
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// If no node to delete, return directly
|
||||
// Return immediately if the node to remove does not exist
|
||||
if (cur == null)
|
||||
return;
|
||||
// Number of child nodes = 0 or 1
|
||||
// Number of children = 0 or 1
|
||||
if (cur.left == null || cur.right == null) {
|
||||
// When number of child nodes = 0 / 1, child = null / that child node
|
||||
// With 0 / 1 children, child is null / the child node, respectively
|
||||
TreeNode child = cur.left != null ? cur.left : cur.right;
|
||||
// Delete node cur
|
||||
// Remove node cur
|
||||
if (cur != root) {
|
||||
if (pre.left == cur)
|
||||
pre.left = child;
|
||||
else
|
||||
pre.right = child;
|
||||
} else {
|
||||
// If deleted node is root node, reassign root node
|
||||
// If the removed node is the root, assign the new root
|
||||
root = child;
|
||||
}
|
||||
}
|
||||
// Number of child nodes = 2
|
||||
// Number of children = 2
|
||||
else {
|
||||
// Get next node of cur in inorder traversal
|
||||
// Get the inorder successor of cur
|
||||
TreeNode tmp = cur.right;
|
||||
while (tmp.left != null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// Recursively delete node tmp
|
||||
// Recursively remove node tmp
|
||||
remove(tmp.val);
|
||||
// Replace cur with tmp
|
||||
// Replace the value of cur with the value of tmp
|
||||
cur.val = tmp.val;
|
||||
}
|
||||
}
|
||||
@@ -125,34 +125,34 @@ class BinarySearchTree {
|
||||
|
||||
public class binary_search_tree {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary search tree */
|
||||
/* Initialize the binary search tree */
|
||||
BinarySearchTree bst = new BinarySearchTree();
|
||||
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
|
||||
// Different insertion orders produce different binary trees; this sequence produces a perfect binary tree
|
||||
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
|
||||
for (int num : nums) {
|
||||
bst.insert(num);
|
||||
}
|
||||
System.out.println("\nInitialized binary tree is\n");
|
||||
System.out.println("\nThe initialized binary tree is:\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
|
||||
/* Search node */
|
||||
/* Find a node */
|
||||
TreeNode node = bst.search(7);
|
||||
System.out.println("\nFound node object is " + node + ", node value = " + node.val);
|
||||
System.out.println("\nFound node object: " + node + ", node value = " + node.val);
|
||||
|
||||
/* Insert node */
|
||||
/* Insert a node */
|
||||
bst.insert(16);
|
||||
System.out.println("\nAfter inserting node 16, binary tree is\n");
|
||||
System.out.println("\nBinary tree after inserting node 16:\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
|
||||
/* Remove node */
|
||||
/* Remove a node */
|
||||
bst.remove(1);
|
||||
System.out.println("\nAfter removing node 1, binary tree is\n");
|
||||
System.out.println("\nBinary tree after removing node 1:\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
bst.remove(2);
|
||||
System.out.println("\nAfter removing node 2, binary tree is\n");
|
||||
System.out.println("\nBinary tree after removing node 2:\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
bst.remove(4);
|
||||
System.out.println("\nAfter removing node 4, binary tree is\n");
|
||||
System.out.println("\nBinary tree after removing node 4:\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user