fix dart code for binary_search_tree (#711)

pull/714/head
liuyuxin 1 year ago committed by GitHub
parent 95bc0ffdb2
commit 233e842d63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,9 +12,9 @@ class BinarySearchTree {
late TreeNode? _root;
/* 构造方法 */
BinarySearchTree(List<int> nums) {
nums.sort(); //
_root = buildTree(nums, 0, nums.length - 1); //
BinarySearchTree() {
//
_root = null;
}
/* 获取二叉树的根节点 */
@ -22,19 +22,6 @@ class BinarySearchTree {
return _root;
}
/* 构建二叉上搜索树 */
TreeNode? buildTree(List<int> nums, int i, int j) {
if (i > j) {
return null;
}
//
int mid = (i + j) ~/ 2;
TreeNode? root = TreeNode(nums[mid]);
root.left = buildTree(nums, i, mid - 1);
root.right = buildTree(nums, mid + 1, j);
return root;
}
/* 查找节点 */
TreeNode? search(int num) {
TreeNode? cur = _root;
@ -87,7 +74,6 @@ class BinarySearchTree {
void remove(int num) {
//
if (_root == null) return;
TreeNode? cur = _root;
TreeNode? pre = null;
//
@ -136,8 +122,12 @@ class BinarySearchTree {
/* Driver Code */
void main() {
/* 初始化二叉搜索树 */
List<int> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
BinarySearchTree bst = BinarySearchTree(nums);
BinarySearchTree bst = BinarySearchTree();
//
List<int> nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
for (int num in nums) {
bst.insert(num);
}
print("\n初始化的二叉树为\n");
printTree(bst.getRoot());

Loading…
Cancel
Save