Binary sorting (search) tree

Source: Internet
Author: User

the two-fork sorting tree is similar to the sub-optimal two tree, usually taking a two-fork list as the storage structure of the two-fork sort tree . The sequential traversal of the two-fork sort tree can get an ordered sequence of a keyword , an unordered sequence can be constructed by constructing a binary sort tree into an ordered sequence, and the process of constructing the tree is the process of ordering the unordered sequence. Each new node that is inserted is a new leaf node on the two-fork sort tree, and the insertion operation does not have to move other nodes, just change the pointer of a node, and the null becomes non-empty. The complexity of searching, inserting, and deleting is equal to the height of the tree, O (log (n)).

different from heap sorting. To understand the concept of "heap": a very special structure-logically a completely binary tree, but stored on a sequential array (priority queue)
Therefore, it is best not to use the linked list description; because the heap is essentially a contiguous storage space, just for the sake of sorting, we think of it as a------all--two------the tree!

At first Node*root=null; Defines a null pointer that points to the root node of the binary tree.

To read a value from an existing array:

    1. First determine whether the current pointer is empty (recursive exit), if it is empty, create a new struct and: 1) put the current value into the node. 2) The left and right pointers of this node are assigned null;

    2. is not a null pointer. Compares the current value to the size of num in the node. If it is less than or equal to Num, go left. Greater than num, go right. (This operation ensures that the left son is always less than equal to the father, the right son is always greater than the father, two identical numbers placed next to each other)

Achievements completed

Output from a two-fork tree (output the tree to the original array):

To sort from small to large (export the tree to the original array):

    1. Determines whether the current pointer is empty (recursive exit), or return if it is empty.

    2. Because the right side is small, so to the right recursion.

    3. Right function recursive end output father

    4. Finally, left-hand recursion.

/* If you want to export from large to small, Exchange 2nd, 4 steps. */

Red line for small to large output order

#include <iostream>using namespace std;struct node{    int num;    Node*left;    Node*right;}; void Build_tree (node*&p, int x) {    if (p = = NULL)    {        p = new node;        P->left = P->right = NULL;        P->num = x;        return;    }    if (x <= p->num)    {        Build_tree (p->left, x);    }    if (x > P->num)    {        Build_tree (p->right, x);}    } int output (Node*&p,int*&ans,int i) {    if (p = = NULL)        return i;    I=output (p->left,ans,i);    Ans[i] = p->num; i++;     I=output (p->right,ans,i);    return i;} void BST (int*a, int N) {    node*root = NULL;    int i;    for (i = 0; i < N; i++)    {        build_tree (root, A[i]);    }    Output (root,a,0);}

Binary sorting (search) tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.