Tree table search binary sorting tree

Source: Internet
Author: User

First, we will introduce the basic concept: the binary tree is a binary tree, or is empty, or meets the following conditions:

① If the left subtree is not empty, the value on it is smaller than the root value;

② If the right subtree is not empty, the value on it shall not be smaller than the root value;

③ Left/right subtree is also a binary sorting tree

 

The following describes the basic algorithms by constructing a binary sorting tree and searching for its elements.

1. Structure

The basic idea of construction: assume that an binary sorting tree already exists, you need to add elements to it. The method is to create a new

Pointer to load the key into the area pointed to by the pointer, and then traverse the binary sorting tree to find a suitable position, and then create a new node link. For example

How to find a proper position is based on the characteristics of the binary sorting tree, which is constantly searched in the left and right subtree until a suitable null position is found. For details, see

Code.

2. Search

First, compare the relationship between the keyword of the root node and the size of the element to be queried. If they are equal, it indicates that the search is complete. Otherwise, if the former is large

In the left subtree; otherwise, in the right subtree.

The Code is as follows:

# Include <iostream> using namespace STD; struct node // The node contains a keyword and the pointer between the left and right nodes {int key; node * lchild; node * rchild ;}; class bin_search_t // binary sorting tree class {public: bin_search_t (); void insert (node * & T, node * U); void create_bst (node * & T ); node * visit (node * t, int value); node * root;}; bin_search_t: bin_search_t () {root = NULL; // if the root value is null, insert () is determined () and create_bst () must use the root reference} void bin_search_t: insert (node * & T, node * u) // node element that indicates the u Insert to the sorting tree with T as the root {If (t = NULL) // link T = u at a null position each time; else if (u-> key> T-> key) insert (t-> rchild, U); elseinsert (t-> lchild, U);} void bin_search_t :: create_bst (node * & T) // need to be referenced, because the following T = NULL statement cannot be accessed from the root if it is not referenced. {node * u = NULL; const int max = 9999; // indicates the end of constructing the int X; while (1) {CIN> X; If (x = max) break; u = new node; u-> key = x; U-> lchild = NULL; U-> rchild = NULL; insert (t, u) ;}/ * recursive search */node * bin_search_t:: visit (node * t, int value) {If (t = NULL | T-> Key = value) // putting t = NULL in front will save a lot of trouble, because when T = NULL, T-> key will return T; else if (t-> key> value) return visit (t-> lchild, value); elsereturn visit (t-> rchild, value );} /* Non-recursive search node * bin_search_t_n: visit (node * t, int value) {node * P = T; while (P! = NULL) {If (p-> key = value) return P; else if (p-> key> value) P = p-> lchild; elsep = p-> rchild;} return P;} */INT main () {bin_search_t P; node * q; p. create_bst (P. root); q = P. visit (P. root, 9); // take search 9 as an example: cout <q <Endl; // output location 9 If (Q! = NULL) // output the cout <q-> key <Endl; elsecout <"q = NULL" <Endl; return 0 ;}

 

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.