fix build_tree, binary_search_tree.cs

fix two figures
pull/758/head
krahets 1 year ago
parent af2aeb0897
commit cb9c14f5ff

@ -7,18 +7,18 @@
#include "../utils/common.hpp" #include "../utils/common.hpp"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
TreeNode *dfs(vector<int> &preorder, vector<int> &inorder, unordered_map<int, int> &hmap, int i, int l, int r) { TreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) if (r - l < 0)
return NULL; return NULL;
// 初始化根节点 // 初始化根节点
TreeNode *root = new TreeNode(preorder[i]); TreeNode *root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
int m = hmap[preorder[i]]; int m = inorderMap[preorder[i]];
// 子问题:构建左子树 // 子问题:构建左子树
root->left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root->right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -26,11 +26,11 @@ TreeNode *dfs(vector<int> &preorder, vector<int> &inorder, unordered_map<int, in
/* 构建二叉树 */ /* 构建二叉树 */
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
unordered_map<int, int> hmap; unordered_map<int, int> inorderMap;
for (int i = 0; i < inorder.size(); i++) { for (int i = 0; i < inorder.size(); i++) {
hmap[inorder[i]] = i; inorderMap[inorder[i]] = i;
} }
TreeNode *root = dfs(preorder, inorder, hmap, 0, 0, inorder.size() - 1); TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);
return root; return root;
} }

@ -8,18 +8,18 @@ namespace hello_algo.chapter_divide_and_conquer;
public class build_tree { public class build_tree {
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
public TreeNode dfs(int[] preorder, int[] inorder, Dictionary<int, int> hmap, int i, int l, int r) { public TreeNode dfs(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) if (r - l < 0)
return null; return null;
// 初始化根节点 // 初始化根节点
TreeNode root = new TreeNode(preorder[i]); TreeNode root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
int m = hmap[preorder[i]]; int m = inorderMap[preorder[i]];
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -27,11 +27,11 @@ public class build_tree {
/* 构建二叉树 */ /* 构建二叉树 */
public TreeNode buildTree(int[] preorder, int[] inorder) { public TreeNode buildTree(int[] preorder, int[] inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
Dictionary<int, int> hmap = new Dictionary<int, int>(); Dictionary<int, int> inorderMap = new Dictionary<int, int>();
for (int i = 0; i < inorder.Length; i++) { for (int i = 0; i < inorder.Length; i++) {
hmap.TryAdd(inorder[i], i); inorderMap.TryAdd(inorder[i], i);
} }
TreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.Length - 1); TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.Length - 1);
return root; return root;
} }

@ -91,7 +91,7 @@ class BinarySearchTree {
cur = cur.left; cur = cur.left;
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur == null || pre == null) if (cur == null)
return; return;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) { if (cur.left == null || cur.right == null) {

@ -10,8 +10,7 @@ import '../utils/tree_node.dart';
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
TreeNode? dfs( TreeNode? dfs(
List<int> preorder, List<int> preorder,
List<int> inorder, Map<int, int> inorderMap,
Map<int, int> hmap,
int i, int i,
int l, int l,
int r, int r,
@ -23,11 +22,11 @@ TreeNode? dfs(
// //
TreeNode? root = TreeNode(preorder[i]); TreeNode? root = TreeNode(preorder[i]);
// m // m
int m = hmap[preorder[i]]!; int m = inorderMap[preorder[i]]!;
// //
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// //
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// //
return root; return root;
} }
@ -35,11 +34,11 @@ TreeNode? dfs(
/* 构建二叉树 */ /* 构建二叉树 */
TreeNode? buildTree(List<int> preorder, List<int> inorder) { TreeNode? buildTree(List<int> preorder, List<int> inorder) {
// inorder // inorder
Map<int, int> hmap = {}; Map<int, int> inorderMap = {};
for (int i = 0; i < inorder.length; i++) { for (int i = 0; i < inorder.length; i++) {
hmap[inorder[i]] = i; inorderMap[inorder[i]] = i;
} }
TreeNode? root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }

@ -7,7 +7,7 @@ package chapter_divide_and_conquer
import . "github.com/krahets/hello-algo/pkg" import . "github.com/krahets/hello-algo/pkg"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeNode { func dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {
// 子树区间为空时终止 // 子树区间为空时终止
if r-l < 0 { if r-l < 0 {
return nil return nil
@ -15,11 +15,11 @@ func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeN
// 初始化根节点 // 初始化根节点
root := NewTreeNode(preorder[i]) root := NewTreeNode(preorder[i])
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
m := hmap[preorder[i]] m := inorderMap[preorder[i]]
// 子问题:构建左子树 // 子问题:构建左子树
root.Left = dfsBuildTree(preorder, inorder, hmap, i+1, l, m-1) root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)
// 子问题:构建右子树 // 子问题:构建右子树
root.Right = dfsBuildTree(preorder, inorder, hmap, i+1+m-l, m+1, r) root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)
// 返回根节点 // 返回根节点
return root return root
} }
@ -27,11 +27,11 @@ func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeN
/* 构建二叉树 */ /* 构建二叉树 */
func buildTree(preorder, inorder []int) *TreeNode { func buildTree(preorder, inorder []int) *TreeNode {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
hmap := make(map[int]int, len(inorder)) inorderMap := make(map[int]int, len(inorder))
for i := 0; i < len(inorder); i++ { for i := 0; i < len(inorder); i++ {
hmap[inorder[i]] = i inorderMap[inorder[i]] = i
} }
root := dfsBuildTree(preorder, inorder, hmap, 0, 0, len(inorder)-1) root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)
return root return root
} }

@ -11,18 +11,18 @@ import java.util.*;
public class build_tree { public class build_tree {
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
static TreeNode dfs(int[] preorder, int[] inorder, Map<Integer, Integer> hmap, int i, int l, int r) { static TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) if (r - l < 0)
return null; return null;
// 初始化根节点 // 初始化根节点
TreeNode root = new TreeNode(preorder[i]); TreeNode root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
int m = hmap.get(preorder[i]); int m = inorderMap.get(preorder[i]);
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -30,11 +30,11 @@ public class build_tree {
/* 构建二叉树 */ /* 构建二叉树 */
static TreeNode buildTree(int[] preorder, int[] inorder) { static TreeNode buildTree(int[] preorder, int[] inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
Map<Integer, Integer> hmap = new HashMap<>(); Map<Integer, Integer> inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { for (int i = 0; i < inorder.length; i++) {
hmap.put(inorder[i], i); inorderMap.put(inorder[i], i);
} }
TreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }

@ -8,17 +8,17 @@ const { printTree } = require('../modules/PrintUtil');
const { TreeNode } = require('../modules/TreeNode'); const { TreeNode } = require('../modules/TreeNode');
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
function dfs(preorder, inorder, hmap, i, l, r) { function dfs(preorder, inorderMap, i, l, r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) return null; if (r - l < 0) return null;
// 初始化根节点 // 初始化根节点
const root = new TreeNode(preorder[i]); const root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
const m = hmap.get(preorder[i]); const m = inorderMap.get(preorder[i]);
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -26,11 +26,11 @@ function dfs(preorder, inorder, hmap, i, l, r) {
/* 构建二叉树 */ /* 构建二叉树 */
function buildTree(preorder, inorder) { function buildTree(preorder, inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let hmap = new Map(); let inorderMap = new Map();
for (let i = 0; i < inorder.length; i++) { for (let i = 0; i < inorder.length; i++) {
hmap.set(inorder[i], i); inorderMap.set(inorder[i], i);
} }
const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }

@ -12,8 +12,7 @@ from modules import *
def dfs( def dfs(
preorder: list[int], preorder: list[int],
inorder: list[int], inorder_map: dict[int, int],
hmap: dict[int, int],
i: int, i: int,
l: int, l: int,
r: int, r: int,
@ -25,11 +24,11 @@ def dfs(
# 初始化根节点 # 初始化根节点
root = TreeNode(preorder[i]) root = TreeNode(preorder[i])
# 查询 m ,从而划分左右子树 # 查询 m ,从而划分左右子树
m = hmap[preorder[i]] m = inorder_map[preorder[i]]
# 子问题:构建左子树 # 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1) root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)
# 子问题:构建右子树 # 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r) root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)
# 返回根节点 # 返回根节点
return root return root
@ -37,8 +36,8 @@ def dfs(
def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None: def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:
"""构建二叉树""" """构建二叉树"""
# 初始化哈希表,存储 inorder 元素到索引的映射 # 初始化哈希表,存储 inorder 元素到索引的映射
hmap = {val: i for i, val in enumerate(inorder)} inorder_map = {val: i for i, val in enumerate(inorder)}
root = dfs(preorder, inorder, hmap, 0, 0, len(inorder) - 1) root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)
return root return root

@ -10,17 +10,17 @@ include!("../include/include.rs");
use tree_node::TreeNode; use tree_node::TreeNode;
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> { fn dfs(preorder: &[i32], inorderMap: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {
// 子树区间为空时终止 // 子树区间为空时终止
if r - l < 0 { return None; } if r - l < 0 { return None; }
// 初始化根节点 // 初始化根节点
let root = TreeNode::new(preorder[i as usize]); let root = TreeNode::new(preorder[i as usize]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
let m = hmap.get(&preorder[i as usize]).unwrap(); let m = inorderMap.get(&preorder[i as usize]).unwrap();
// 子问题:构建左子树 // 子问题:构建左子树
root.borrow_mut().left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.borrow_mut().left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.borrow_mut().right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.borrow_mut().right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
Some(root) Some(root)
} }
@ -28,11 +28,11 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i
/* 构建二叉树 */ /* 构建二叉树 */
fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> { fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let mut hmap: HashMap<i32, i32> = HashMap::new(); let mut inorderMap: HashMap<i32, i32> = HashMap::new();
for i in 0..inorder.len() { for i in 0..inorder.len() {
hmap.insert(inorder[i], i as i32); inorderMap.insert(inorder[i], i as i32);
} }
let root = dfs(preorder, inorder, &hmap, 0, 0, inorder.len() as i32 - 1); let root = dfs(preorder, &inorderMap, 0, 0, inorder.len() as i32 - 1);
root root
} }

@ -7,7 +7,7 @@
import utils import utils
/* */ /* */
func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? { func dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {
// //
if r - l < 0 { if r - l < 0 {
return nil return nil
@ -15,11 +15,11 @@ func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: I
// //
let root = TreeNode(x: preorder[i]) let root = TreeNode(x: preorder[i])
// m // m
let m = hmap[preorder[i]]! let m = inorderMap[preorder[i]]!
// //
root.left = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1, l: l, r: m - 1) root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)
// //
root.right = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1 + m - l, l: m + 1, r: r) root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)
// //
return root return root
} }
@ -27,8 +27,8 @@ func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: I
/* */ /* */
func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? { func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {
// inorder // inorder
let hmap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset } let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
return dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: 0, l: 0, r: inorder.count - 1) return dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1)
} }
@main @main

@ -10,8 +10,7 @@ import { TreeNode } from '../modules/TreeNode';
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
function dfs( function dfs(
preorder: number[], preorder: number[],
inorder: number[], inorderMap: Map<number, number>,
hmap: Map<number, number>,
i: number, i: number,
l: number, l: number,
r: number r: number
@ -21,11 +20,11 @@ function dfs(
// 初始化根节点 // 初始化根节点
const root: TreeNode = new TreeNode(preorder[i]); const root: TreeNode = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
const m = hmap.get(preorder[i]); const m = inorderMap.get(preorder[i]);
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -33,11 +32,11 @@ function dfs(
/* 构建二叉树 */ /* 构建二叉树 */
function buildTree(preorder: number[], inorder: number[]): TreeNode | null { function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let hmap = new Map<number, number>(); let inorderMap = new Map<number, number>();
for (let i = 0; i < inorder.length; i++) { for (let i = 0; i < inorder.length; i++) {
hmap.set(inorder[i], i); inorderMap.set(inorder[i], i);
} }
const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 54 KiB

Loading…
Cancel
Save