Data structure refresher: Day 13

 

Table of contents

1. Search in Binary Search Tree

 1. Recursion

Complexity analysis

2. Iteration

Complexity analysis

2. Insertion operation in binary search book

 1. Simulation

Ideas and Algorithms

Complexity analysis

1. Search in Binary Search Tree

700. Search in a binary search tree - LeetCode https://leetcode.cn/problems/search-in-a-binary-search-tree/?plan=data-structures&plan_progress=ggfacv7

 1. Recursion

Binary search trees satisfy the following properties:

The element values ​​of all nodes in the left subtree are less than the element value of the root; the
element values ​​of all nodes in the right subtree are greater than the element value of the root.
Based on this, the following algorithm can be obtained:

If root is empty, return an empty node;
if val=root.val, return \textit{root}root;
if val<root.val, recurse the left subtree;
if val>root.val, recurse the right subtree.

class Solution {
public:
    TreeNode *searchBST(TreeNode *root, int val) {
        if (root == nullptr) {
            return nullptr;
        }
        if (val == root->val) {
            return root;
        }
        return searchBST(val < root->val ? root->left : root->right, val);
    }
};

Complexity analysis

Time complexity: O(N), where N is the number of nodes in the binary search tree. In the worst case, the binary search tree is a chain, and the element we are looking for is smaller (larger) than the element at the end of the chain. In this case, we need to recurse N times.

Space complexity: O(N). Recursion requires O(N) stack space in the worst case.

2. Iteration

We change the recursive method of method 1 to iterative writing:

If root is empty, jump out of the loop and return an empty node;
if val=root.val, return root;
if val<root.val, set root to root.left;
if val>root.val, set root to root.right.

class Solution {
public:
    TreeNode *searchBST(TreeNode *root, int val) {
        while (root) {
            if (val == root->val) {
                return root;
            }
            root = val < root->val ? root->left : root->right;
        }
        return nullptr;
    }
};

Complexity analysis

Time complexity: O(N), where NN is the number of nodes of the binary search tree. In the worst case, the binary search tree is a chain, and the element we are looking for is smaller (larger) than the element at the end of the chain. In this case, we need to iterate N times.

Space complexity: O(1). No extra space is used.

2. Insertion operation in binary search book

701. Insertion operation in a binary search tree - LeetCode https://leetcode.cn/problems/insert-into-a-binary-search-tree/?plan=data-structures&plan_progress=ggfacv7

 1. Simulation

Ideas and Algorithms

First, review the properties of binary search trees: for any node root, the values ​​of all nodes on the left subtree (if it exists) are less than root.val, and the values ​​of all nodes on the right subtree (if it exists) are greater than root. val, and they are all binary search trees.

Therefore, when inserting val into a subtree rooted at root, you can determine which subtree to insert val into based on the size relationship between val and root.val.

If the subtree is not empty, the problem is transformed into inserting \textit{val}val into the corresponding subtree.
Otherwise, create a new node with \textit{val}val as the value here and link it to its parent node root.

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if (root == nullptr) {
            return new TreeNode(val);
        }
        TreeNode* pos = root;
        while (pos != nullptr) {
            if (val < pos->val) {
                if (pos->left == nullptr) {
                    pos->left = new TreeNode(val);
                    break;
                } else {
                    pos = pos->left;
                }
            } else {
                if (pos->right == nullptr) {
                    pos->right = new TreeNode(val);
                    break;
                } else {
                    pos = pos->right;
                }
            }
        }
        return root;
    }
};

Complexity analysis

Time complexity: O(N), where N is the number of nodes in the tree. In the worst case, we need to insert the value into the deepest leaf node of the tree, and the deepest leaf node is O(N).

Space complexity: O(1). We only used a constant size of space.

Guess you like

Origin blog.csdn.net/m0_63309778/article/details/126746985