mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-13 01:08:11 +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:
@@ -9,7 +9,7 @@ package chapter_tree;
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* Binary tree class represented by array */
|
||||
/* Binary tree class using an array representation */
|
||||
class ArrayBinaryTree {
|
||||
private List<Integer> tree;
|
||||
|
||||
@@ -18,30 +18,30 @@ class ArrayBinaryTree {
|
||||
tree = new ArrayList<>(arr);
|
||||
}
|
||||
|
||||
/* List capacity */
|
||||
/* List size */
|
||||
public int size() {
|
||||
return tree.size();
|
||||
}
|
||||
|
||||
/* Get value of node at index i */
|
||||
/* Get the value of the node at index i */
|
||||
public Integer val(int i) {
|
||||
// If index out of bounds, return null to represent empty position
|
||||
// If the index is out of bounds, return null to represent an empty position
|
||||
if (i < 0 || i >= size())
|
||||
return null;
|
||||
return tree.get(i);
|
||||
}
|
||||
|
||||
/* Get index of left child node of node at index i */
|
||||
/* Get the left child index of the node at index i */
|
||||
public Integer left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node of node at index i */
|
||||
/* Get the right child index of the node at index i */
|
||||
public Integer right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node of node at index i */
|
||||
/* Get the parent index of the node at index i */
|
||||
public Integer parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class ArrayBinaryTree {
|
||||
/* Level-order traversal */
|
||||
public List<Integer> levelOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
// Traverse array directly
|
||||
// Traverse the array directly
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (val(i) != null)
|
||||
res.add(val(i));
|
||||
@@ -59,7 +59,7 @@ class ArrayBinaryTree {
|
||||
|
||||
/* Depth-first traversal */
|
||||
private void dfs(Integer i, String order, List<Integer> res) {
|
||||
// If empty position, return
|
||||
// Return if this position is empty
|
||||
if (val(i) == null)
|
||||
return;
|
||||
// Preorder traversal
|
||||
@@ -99,38 +99,38 @@ class ArrayBinaryTree {
|
||||
|
||||
public class array_binary_tree {
|
||||
public static void main(String[] args) {
|
||||
// Initialize binary tree
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
// Initialize the binary tree
|
||||
// Use a helper function that builds a binary tree directly from an array
|
||||
List<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
|
||||
|
||||
TreeNode root = TreeNode.listToTree(arr);
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
System.out.println("Array representation of binary tree:");
|
||||
System.out.println("\nInitialize the binary tree\n");
|
||||
System.out.println("Array representation of the binary tree:");
|
||||
System.out.println(arr);
|
||||
System.out.println("Linked list representation of binary tree:");
|
||||
System.out.println("Linked representation of the binary tree:");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// Binary tree class represented by array
|
||||
// Binary tree class using an array representation
|
||||
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
|
||||
|
||||
// Access node
|
||||
// Access a node
|
||||
int i = 1;
|
||||
Integer l = abt.left(i);
|
||||
Integer r = abt.right(i);
|
||||
Integer p = abt.parent(i);
|
||||
System.out.println("\nCurrent node index is " + i + ", value is " + abt.val(i));
|
||||
System.out.println("Its left child node index is " + l + ", value is " + (l == null ? "null" : abt.val(l)));
|
||||
System.out.println("Its right child node index is " + r + ", value is " + (r == null ? "null" : abt.val(r)));
|
||||
System.out.println("Its parent node index is " + p + ", value is " + (p == null ? "null" : abt.val(p)));
|
||||
System.out.println("\nCurrent node index: " + i + " with value " + abt.val(i));
|
||||
System.out.println("Left child index: " + l + " with value " + (l == null ? "null" : abt.val(l)));
|
||||
System.out.println("Right child index: " + r + " with value " + (r == null ? "null" : abt.val(r)));
|
||||
System.out.println("Parent index: " + p + " with value " + (p == null ? "null" : abt.val(p)));
|
||||
|
||||
// Traverse tree
|
||||
// Traverse the tree
|
||||
List<Integer> res = abt.levelOrder();
|
||||
System.out.println("\nLevel-order traversal is:" + res);
|
||||
System.out.println("\nLevel-order traversal:" + res);
|
||||
res = abt.preOrder();
|
||||
System.out.println("Preorder traversal is:" + res);
|
||||
System.out.println("Preorder traversal:" + res);
|
||||
res = abt.inOrder();
|
||||
System.out.println("Inorder traversal is:" + res);
|
||||
System.out.println("Inorder traversal:" + res);
|
||||
res = abt.postOrder();
|
||||
System.out.println("Postorder traversal is:" + res);
|
||||
System.out.println("Postorder traversal:" + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,118 +12,118 @@ import utils.*;
|
||||
class AVLTree {
|
||||
TreeNode root; // Root node
|
||||
|
||||
/* Get node height */
|
||||
/* Get the node height */
|
||||
public int height(TreeNode node) {
|
||||
// Empty node height is -1, leaf node height is 0
|
||||
// A null node has height -1; a leaf has height 0
|
||||
return node == null ? -1 : node.height;
|
||||
}
|
||||
|
||||
/* Update node height */
|
||||
/* Update the node height */
|
||||
private void updateHeight(TreeNode node) {
|
||||
// Node height equals the height of the tallest subtree + 1
|
||||
// The node height equals the height of its taller subtree + 1
|
||||
node.height = Math.max(height(node.left), height(node.right)) + 1;
|
||||
}
|
||||
|
||||
/* Get balance factor */
|
||||
/* Get the balance factor */
|
||||
public int balanceFactor(TreeNode node) {
|
||||
// Empty node balance factor is 0
|
||||
// A null node has balance factor 0
|
||||
if (node == null)
|
||||
return 0;
|
||||
// Node balance factor = left subtree height - right subtree height
|
||||
return height(node.left) - height(node.right);
|
||||
}
|
||||
|
||||
/* Right rotation operation */
|
||||
/* Right rotation */
|
||||
private TreeNode rightRotate(TreeNode node) {
|
||||
TreeNode child = node.left;
|
||||
TreeNode grandChild = child.right;
|
||||
// Using child as pivot, rotate node to the right
|
||||
// Rotate node to the right around child
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// Update node height
|
||||
// Update the node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
// Return the root of the rotated subtree
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Left rotation operation */
|
||||
/* Left rotation */
|
||||
private TreeNode leftRotate(TreeNode node) {
|
||||
TreeNode child = node.right;
|
||||
TreeNode grandChild = child.left;
|
||||
// Using child as pivot, rotate node to the left
|
||||
// Rotate node to the left around child
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// Update node height
|
||||
// Update the node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
// Return the root of the rotated subtree
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Perform rotation operation to restore balance to this subtree */
|
||||
/* Perform rotations to restore balance in this subtree */
|
||||
private TreeNode rotate(TreeNode node) {
|
||||
// Get balance factor of node
|
||||
// Get the balance factor of node
|
||||
int balanceFactor = balanceFactor(node);
|
||||
// Left-leaning tree
|
||||
// Left-heavy tree
|
||||
if (balanceFactor > 1) {
|
||||
if (balanceFactor(node.left) >= 0) {
|
||||
// Right rotation
|
||||
// Rotate right
|
||||
return rightRotate(node);
|
||||
} else {
|
||||
// First left rotation then right rotation
|
||||
// Rotate left, then right
|
||||
node.left = leftRotate(node.left);
|
||||
return rightRotate(node);
|
||||
}
|
||||
}
|
||||
// Right-leaning tree
|
||||
// Right-heavy tree
|
||||
if (balanceFactor < -1) {
|
||||
if (balanceFactor(node.right) <= 0) {
|
||||
// Left rotation
|
||||
// Rotate left
|
||||
return leftRotate(node);
|
||||
} else {
|
||||
// First right rotation then left rotation
|
||||
// Rotate right, then left
|
||||
node.right = rightRotate(node.right);
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// Balanced tree, no rotation needed, return directly
|
||||
// The tree is balanced; return without rotating
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
/* Insert a node */
|
||||
public void insert(int val) {
|
||||
root = insertHelper(root, val);
|
||||
}
|
||||
|
||||
/* Recursively insert node (helper method) */
|
||||
/* Insert a node recursively (helper method) */
|
||||
private TreeNode insertHelper(TreeNode node, int val) {
|
||||
if (node == null)
|
||||
return new TreeNode(val);
|
||||
/* 1. Find insertion position and insert node */
|
||||
/* 1. Find the insertion position and insert the node */
|
||||
if (val < node.val)
|
||||
node.left = insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = insertHelper(node.right, val);
|
||||
else
|
||||
return node; // Duplicate node not inserted, return directly
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
return node; // Return without inserting a duplicate node
|
||||
updateHeight(node); // Update the node height
|
||||
/* 2. Perform rotations to restore balance in this subtree */
|
||||
node = rotate(node);
|
||||
// Return root node of subtree
|
||||
// Return the subtree root
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Remove node */
|
||||
/* Remove a node */
|
||||
public void remove(int val) {
|
||||
root = removeHelper(root, val);
|
||||
}
|
||||
|
||||
/* Recursively delete node (helper method) */
|
||||
/* Remove a node recursively (helper method) */
|
||||
private TreeNode removeHelper(TreeNode node, int val) {
|
||||
if (node == null)
|
||||
return null;
|
||||
/* 1. Find node and delete */
|
||||
/* 1. Find and remove the node */
|
||||
if (val < node.val)
|
||||
node.left = removeHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
@@ -131,14 +131,14 @@ class AVLTree {
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
TreeNode child = node.left != null ? node.left : node.right;
|
||||
// Number of child nodes = 0, delete node directly and return
|
||||
// Number of children = 0: remove node and return
|
||||
if (child == null)
|
||||
return null;
|
||||
// Number of child nodes = 1, delete node directly
|
||||
// Number of children = 1: remove node directly
|
||||
else
|
||||
node = child;
|
||||
} else {
|
||||
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
|
||||
// Number of children = 2: remove the inorder successor and use its value to replace the current node value
|
||||
TreeNode temp = node.right;
|
||||
while (temp.left != null) {
|
||||
temp = temp.left;
|
||||
@@ -147,29 +147,29 @@ class AVLTree {
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
updateHeight(node); // Update the node height
|
||||
/* 2. Perform rotations to restore balance in this subtree */
|
||||
node = rotate(node);
|
||||
// Return root node of subtree
|
||||
// Return the subtree root
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
/* Find a node */
|
||||
public TreeNode search(int val) {
|
||||
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 < val)
|
||||
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 > val)
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -177,22 +177,22 @@ class AVLTree {
|
||||
public class avl_tree {
|
||||
static void testInsert(AVLTree tree, int val) {
|
||||
tree.insert(val);
|
||||
System.out.println("\nAfter inserting node " + val + ", AVL tree is");
|
||||
System.out.println("\nAfter inserting node " + val + " the AVL tree is:");
|
||||
PrintUtil.printTree(tree.root);
|
||||
}
|
||||
|
||||
static void testRemove(AVLTree tree, int val) {
|
||||
tree.remove(val);
|
||||
System.out.println("\nAfter removing node " + val + ", AVL tree is");
|
||||
System.out.println("\nAfter removing node " + val + " the AVL tree is:");
|
||||
PrintUtil.printTree(tree.root);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
|
||||
/* Initialize an empty AVL tree */
|
||||
AVLTree avlTree = new AVLTree();
|
||||
|
||||
/* Insert node */
|
||||
// Delete nodes
|
||||
/* Insert a node */
|
||||
// Observe how the AVL tree maintains balance after a node is inserted
|
||||
testInsert(avlTree, 1);
|
||||
testInsert(avlTree, 2);
|
||||
testInsert(avlTree, 3);
|
||||
@@ -204,17 +204,17 @@ public class avl_tree {
|
||||
testInsert(avlTree, 10);
|
||||
testInsert(avlTree, 6);
|
||||
|
||||
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
|
||||
/* Insert a duplicate node */
|
||||
testInsert(avlTree, 7);
|
||||
|
||||
/* Remove node */
|
||||
// Delete node with degree 1
|
||||
testRemove(avlTree, 8); // Delete node with degree 2
|
||||
testRemove(avlTree, 5); // Remove node with degree 1
|
||||
testRemove(avlTree, 4); // Remove node with degree 2
|
||||
/* Remove a node */
|
||||
// Observe how the AVL tree maintains balance after a node is removed
|
||||
testRemove(avlTree, 8); // Remove a node with 0 children
|
||||
testRemove(avlTree, 5); // Remove a node with 1 child
|
||||
testRemove(avlTree, 4); // Remove a node with 2 children
|
||||
|
||||
/* Search node */
|
||||
/* Look up a node */
|
||||
TreeNode node = avlTree.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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,31 +10,31 @@ import utils.*;
|
||||
|
||||
public class binary_tree {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary tree */
|
||||
// Initialize nodes
|
||||
/* Initialize the binary tree */
|
||||
// Initialize the nodes
|
||||
TreeNode n1 = new TreeNode(1);
|
||||
TreeNode n2 = new TreeNode(2);
|
||||
TreeNode n3 = new TreeNode(3);
|
||||
TreeNode n4 = new TreeNode(4);
|
||||
TreeNode n5 = new TreeNode(5);
|
||||
// Build references (pointers) between nodes
|
||||
// Set up references (pointers) between the nodes
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
n2.right = n5;
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
System.out.println("\nInitialize the binary tree\n");
|
||||
PrintUtil.printTree(n1);
|
||||
|
||||
/* Insert node P between n1 -> n2 */
|
||||
/* Insert and remove nodes */
|
||||
TreeNode P = new TreeNode(0);
|
||||
// Delete node
|
||||
// Insert node P between n1 and n2 in n1 -> n2
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
System.out.println("\nAfter inserting node P\n");
|
||||
System.out.println("\nAfter inserting node P:\n");
|
||||
PrintUtil.printTree(n1);
|
||||
// Remove node P
|
||||
n1.left = n2;
|
||||
System.out.println("\nAfter removing node P\n");
|
||||
System.out.println("\nAfter removing node P:\n");
|
||||
PrintUtil.printTree(n1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,31 +12,31 @@ import java.util.*;
|
||||
public class binary_tree_bfs {
|
||||
/* Level-order traversal */
|
||||
static List<Integer> levelOrder(TreeNode root) {
|
||||
// Initialize queue, add root node
|
||||
// Initialize a queue and enqueue the root
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
// Initialize a list to save the traversal sequence
|
||||
// Initialize a list to store the traversal sequence
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll(); // Dequeue
|
||||
list.add(node.val); // Save node value
|
||||
TreeNode node = queue.poll(); // Dequeue from the queue
|
||||
list.add(node.val); // Store the node value
|
||||
if (node.left != null)
|
||||
queue.offer(node.left); // Left child node enqueue
|
||||
queue.offer(node.left); // Enqueue the left child
|
||||
if (node.right != null)
|
||||
queue.offer(node.right); // Right child node enqueue
|
||||
queue.offer(node.right); // Enqueue the right child
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
/* Initialize the binary tree */
|
||||
// Use a helper function that builds a binary tree directly from an array
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
System.out.println("\nInitialize the binary tree\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
/* Level-order traversal */
|
||||
List<Integer> list = levelOrder(root);
|
||||
System.out.println("\nLevel-order traversal node print sequence = " + list);
|
||||
System.out.println("\nNode sequence from level-order traversal = " + list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,14 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class binary_tree_dfs {
|
||||
// Initialize list for storing traversal sequence
|
||||
// Initialize a list to store the traversal sequence
|
||||
static ArrayList<Integer> list = new ArrayList<>();
|
||||
|
||||
/* Preorder traversal */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
// Visit priority: root node -> left subtree -> right subtree
|
||||
// Visit order: root -> left subtree -> right subtree
|
||||
list.add(root.val);
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
@@ -27,7 +27,7 @@ public class binary_tree_dfs {
|
||||
static void inOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
// Visit priority: left subtree -> root node -> right subtree
|
||||
// Visit order: left subtree -> root -> right subtree
|
||||
inOrder(root.left);
|
||||
list.add(root.val);
|
||||
inOrder(root.right);
|
||||
@@ -37,32 +37,32 @@ public class binary_tree_dfs {
|
||||
static void postOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
// Visit priority: left subtree -> right subtree -> root node
|
||||
// Visit order: left subtree -> right subtree -> root
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
list.add(root.val);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
/* Initialize the binary tree */
|
||||
// Use a helper function that builds a binary tree directly from an array
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
System.out.println("\nInitialize the binary tree\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
/* Preorder traversal */
|
||||
list.clear();
|
||||
preOrder(root);
|
||||
System.out.println("\nPreorder traversal node print sequence = " + list);
|
||||
System.out.println("\nNode sequence from preorder traversal = " + list);
|
||||
|
||||
/* Inorder traversal */
|
||||
list.clear();
|
||||
inOrder(root);
|
||||
System.out.println("\nInorder traversal node print sequence = " + list);
|
||||
System.out.println("\nNode sequence from inorder traversal = " + list);
|
||||
|
||||
/* Postorder traversal */
|
||||
list.clear();
|
||||
postOrder(root);
|
||||
System.out.println("\nPostorder traversal node print sequence = " + list);
|
||||
System.out.println("\nNode sequence from postorder traversal = " + list);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user