Binary Search Tree_BST

Posted by dannyb785 on Sat, 30 May 2020 18:29:54 +0200

Catalog

Binary Search Tree

Definition

A binary search tree (English: Binary Search Tree), also known as a binary search tree, an ordered binary tree, or a sorted binary tree, refers to an empty tree or has the following properties Binary Tree:

  1. If the left subtree of any node is not empty, the value of all nodes in the left subtree is less than the value of its root node.
  2. If the right subtree of any node is not empty, the value of all nodes in the right subtree is greater than or equal to the value of its root node.
  3. The left and right subtrees of any node are also binary lookup trees.

Recommended explanatory videos:

https://www.bilibili.com/video/BV1Qx411m7Lb
https://www.bilibili.com/video/BV1Qx411m7jE
https://www.bilibili.com/video/BV1os411Y7Rt

Lunar lantern@BILIBILI

Forms and basic properties

Full Binary Tree:

In a binary tree, if all branch nodes have left and right child nodes and the leaf nodes are concentrated at the bottom of the binary tree, such a tree is called a full binary tree

Complete Binary Tree:

Complete binary trees are those in which at most the nodes in the bottom two layers of a binary tree can have a degree less than 2, and the leaf nodes in the bottom two layers are arranged sequentially on the leftmost position of the layer.

Difference:

Full binary trees are a special case of full binary trees, because full binary trees are full, but not full at all.So you should also imagine the shape, full means that every node outside the leaf node has two children, and the full meaning is that the last layer is not full.

Five important properties of a binary tree:

  1. There are at most 2 i-1 nodes on the level I of a binary tree.(i>=1)
  2. If the depth in a binary tree is k, then there are at most 2k-1 nodes.(k>=1)
  3. n0=n2+1 n0 Node with degree 0 N2 Node with degree 2
  4. In a complete binary tree, the depth of a complete binary tree with n nodes is [log2n]+1, where [log2n]+1 is rounded down.
  5. If a complete binary tree with n nodes is numbered from top to bottom and from left to right one to n, then any node numbered i i n the complete binary tree is:
  1. If i=1, the node is the root of a binary tree and has no parents. Otherwise, the node numbered [i/2] is its parent node.
  2. If 2i>n, the node has no left child, otherwise, the node numbered 2I is its left child node.
  3. 2i+1>n, then the node has no right child node, otherwise, the node numbered 2i+1 is its right child node.

Code base implementation

Recursive methods are used extensively in the implementation, so the idea of recursion is the essence.

#include <bits/stdc++.h>

using namespace std;

struct Node
{
    int data;
    Node *left = nullptr;
    Node *right = nullptr;
};

//Iterative method for tree building, inserting nodes
void insertByIterate(Node *&root, int value) //Note that the root is a reference of type Node* because the root itself needs to be modified
{
    Node *node = new Node;
    node->data = value;
    if (root == nullptr)
    {
        root = node;
        return;
    }
    else
    {
        Node *temp = root;
        while (temp)
        {
            // If (value == temp->data) //Avoid inserting duplicate elements
            //     return;
            if (value < temp->data)
            {
                if (temp->left)
                {
                    temp = temp->left;
                }
                else
                {
                    temp->left = node;
                    return;
                }
            }
            else
            {
                if (temp->right)
                {
                    temp = temp->right;
                }
                else
                {
                    temp->right = node;
                    return;
                }
            }
        }
    }
}

//Recursive method implements tree building, inserting nodes
// ! may not be able to record parent node, its own number of layers
void insertByRecursion(Node *&root, int value)
{
    if (root == nullptr)
    {
        root = new Node;
        root->data = value;
        return;
    }
    // If (data == root->data) //Prohibit duplicate elements
    //     return;
    if (value < root->data)
        insertByRecursion(root->left, value);
    else
        insertByRecursion(root->right, value);
}

void preorder(Node *root) //The same order after ordinal
{
    if (root)
    {
        cout << root->data << ' ';
        preorder(root->left);
        preorder(root->right);
    }
}

int main(void)
{
    int arr[7] = {6, 1, 2, -10, 4, 5, 7};
    Node *tree = nullptr;
    for (size_t i = 0; i < 7; i++)
    {
        insertByRecursion(tree, arr[i]);
    }
    preorder(tree);
    cout << endl;
    return 0;
}

Related Issues

L2-004 Is this a binary search tree?

A binary search tree can be recursively defined as a binary tree with the following properties:For any node,

  • The key value of all nodes in the left subtree is less than that of the node.
  • The key value of all nodes in the right subtree is greater than or equal to the key value of the node.
  • Its left and right subtrees are binary search trees.

The so-called "mirror image" of a binary search tree is the tree that is obtained when the left and right subtrees of all nodes are shifted.

Given a sequence of integer keys, write a program to determine if this is the result of a presequential traversal of a binary search tree or its mirror.

Input format:

The first line of input gives a positive integer N (< 1000).The next line gives N integer key values separated by spaces.

Output format:

If the input sequence is the result of a pre-sequential traversal of a binary search tree or its mirror, YES is output in one line first, and then in the next line the result of the post-sequential traversal of the tree is output.There is one space between the numbers and no extra space at the beginning or end of a line.If the answer is no, NO is output.

Input Sample 1:

7
8 6 5 7 10 8 11

Output Sample 1:

YES
5 7 6 8 11 10 8

Input Sample 2:

7
8 10 11 8 6 7 5

Output Sample 2:

YES
11 8 10 7 5 6 8

Input Sample 3:

7
8 6 8 5 10 9 11

Output example 3:

NO

thinking

Problem

#include <bits/stdc++.h>

using namespace std;

struct Node
{
    int data;
    Node *left = 0;
    Node *right = 0;
} *tree = 0;
int n;
typedef vector<int> arr;
void insert(Node *&root, int value)
{
    if (root == 0)
    {
        root = new Node;
        root->data = value;
        return;
    }
    if (value < root->data)
        insert(root->left, value);
    else
        insert(root->right, value);
}
void preOrder(Node *root, vector<int> &res)
{
    if (root)
    {
        res.push_back(root->data);
        preOrder(root->left, res);
        preOrder(root->right, res);
    }
}
void preOrderMirror(Node *root, vector<int> &res)
{
    if (root)
    {
        res.push_back(root->data);
        preOrderMirror(root->right, res);
        preOrderMirror(root->left, res);
    }
}
void postOrder(Node *tree)
{
    static int count(0);
    if (tree)
    {
        postOrder(tree->left);
        postOrder(tree->right);
        cout << tree->data;
        count++;
        if (count != n)
            cout << " ";
    }
}
void postOrderMirror(Node *tree)
{
    static int count(0);
    if (tree)
    {
        postOrderMirror(tree->right);
        postOrderMirror(tree->left);
        cout << tree->data;
        count++;
        if (count != n)
            cout << " ";
    }
}

int main(void)
{
    cin >> n;
    vector<int> input;
    int temp;
    for (int i = 0; i < n; i++)
    {
        cin >> temp;
        insert(tree, temp);
        input.push_back(temp);
    }
    vector<int> res_pre, res_premirror;
    preOrder(tree, res_pre);
    preOrderMirror(tree, res_premirror);
    if (input == res_pre)
    {
        cout << "YES" << endl;
        postOrder(tree);
    }
    else if (input == res_premirror)
    {
        cout << "YES" << endl;
        postOrderMirror(tree);
    }
    else
    {
        cout << "NO" << endl;
    }
    return 0;
}

Topics: C++ less