[Rust] Normalize mid calculation in case overflow (#1363)

* Normalize mid calculate in case overflow

* Change ALL language

* Update merge_sort.py

* Update merge_sort.zig

* Update binary_search_tree.zig

* Update binary_search_recur.py

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
rongyi
2024-05-18 18:19:19 +08:00
committed by GitHub
parent 0e221540a3
commit 21be3fdaf8
41 changed files with 57 additions and 59 deletions

View File

@@ -34,7 +34,7 @@ pub fn BinarySearchTree(comptime T: type) type {
fn buildTree(self: *Self, nums: []T, i: usize, j: usize) !?*inc.TreeNode(T) {
if (i > j) return null;
// 将数组中间节点作为根节点
var mid = (i + j) / 2;
var mid = i + (j - i) / 2;
var node = try self.mem_allocator.create(inc.TreeNode(T));
node.init(nums[mid]);
// 递归建立左子树和右子树
@@ -145,7 +145,7 @@ pub fn BinarySearchTree(comptime T: type) type {
cur.?.val = tmp_val;
}
}
};
};
}
// Driver Code
@@ -179,4 +179,4 @@ pub fn main() !void {
try inc.PrintUtil.printTree(bst.getRoot(), null, false);
_ = try std.io.getStdIn().reader().readByte();
}
}