The process of reproducing the recursive construction of binary search tree

Source: Internet
Author: User

Binary search tree (binary search trees), (also: two fork search tree, binary sort tree) it is either an empty tree or a two-tree with the following properties: If its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of its root node; if its right subtree is not empty, The value of all nodes on the right subtree is greater than the value of its root node, and its left and right subtrees are also two-fork sort trees.

In simple terms, it is
The left is less than the middle
Middle of less than right

First set up several data variables

#define TYPE intstructstruct TreeNode* bst;struct TreeNode{ TYPE data; SearchTree left; SearchTree right;};

For this tree, we assume that the tree has been built.
How to find data for a node, that is, to search for data and return the node pointer
One of the most essential ways of a tree is recursion, and if you come up with a situation, you can
As follows

Pseudo code for BST find (Data, BST T) {if(t== NULL)return NULL;if(Data ==T -Data)returnTElse        if(Data <T -Data)returnFind (t -left);Else            returnFind (t -right);}

This search idea of find runs through the search tree.
Let's see how to build a BST.

For example, a string of numbers
1 3 2 8 9 4 1
At the same time in the process of sequencing
First, there's a tree, and then you put those numbers in.

The first step is to build a small tree
Then the first number 1 is the data, the following is the pseudo-code
BST T=malloc ();
T-data=1;
T->left =null;
T->right =null;

The second step is to insert the number into this little tree.

The basic idea is still
The left sub-tree is smaller than the node
To the right sub-tree, larger than the node

The first parameter of the function must have X (the number to be inserted), the root pointer of the binary lookup tree, BST T
Insert (BST t,x)
So, the discussion of these kinds of situations
1.

Toward the tree T to insert 6,insert (6,t);
Compare node data 5, find greater than 5, then 6 to the right subtree inserted
Recursive invocation
Insert (6,t->right)
At this point, 6 should be inserted here.
And here's the end of the condition is that the passed in tree pointer is null
So

伪代码if(TNULLTTTNULLTNULL:}

But there's a problem.
That is, we have opened up new space in the heap, it is necessary to connect this spatial pointer to node 5 in the form of a linked list, so it is necessary to return a tree pointer
So insert re-revision changed to
BST insert (X,bst T)
The pseudo code above also returns T to the

Tif(TNULL) {  T = malloc();  T->data = x;  TNULL;  TNULLreturnT;}

The basic call is

T;  T->right=insert(6,T->right);}

2.

To insert 8 into the tree above.
The same is the comparison, where Condition 1 is the condition of this recursive end
Then recursion with the Else branch
The pseudo-code framework is

BST Insert (X,BSTT){if(T==NULL) {T= malloc ();T->data = x;T->left =NULL;T->right =NULL: }Else{if(T->data > x) {T->left=insert (x,T->left);}Else if(T->data <x) {T->right=insert (x,T->right);}}return T;}

So the insertion of the binary search tree is done.

Complete a topic for this purpose

Input:
Starting with a number n, (1<=n<=20) indicates that there are n needs to be judged, n= 0 when the input ends.
The next line is a sequence, the sequence length is less than 10, contains (0~9) number, no repetition number, according to this sequence can construct a binary search tree.
The next n rows have n sequences, and each sequence format is the same as the first sequence, so please determine whether the two sequences can form the same binary search tree.
Output:
Output Yes if the sequence is the same, otherwise the output no
Sample input:
2
567432
543267
576342
0
Sample output:
YES
NO

The basic idea is to build up these three trees,
And then re-sequence the same tree.

Code

#include <iostream>#include <algorithm>#include <string>using namespace Std;#define TYPE intstruct treenode;typedef struct treenode* searchtree; typedef struct TREENODE* Position;    struct treenode{TYPE data;    Searchtree left; Searchtree right;};/ *searchtreemakeempty (SearchtreeT){if(T!=NULL) {Makeempty (T->left); Makeempty (T->right); FreeT); }return NULL;} Positionfind (TYPE X,searchtreeT){if(T=NULL)return NULL;if(X <T->data)returnFind (x,T->left);Else        if(X >T->data)returnFind (x,T->right);Else            return T;} Positionfindmin (SearchtreeT){if(T==NULL)return NULL;Else        if(T->left = =NULL)return T;Else            returnFindmin (T->left);} Positionfindmax (SearchtreeT){if(T==NULL)return NULL;Else        if(T->right = =NULL)return T;Else            returnFindmax (T->right);} */searchtreeinsert (TYPE X,searchtreeT){if(T==NULL)    {T= (searchtree) malloc (sizeof (struct TreeNode));if(T==NULL) Exit (-1);Else            T->data = x;T->left =NULL;T->right=NULL; }Else if(X <T->data)T->left = Insert (x,T->left);Else if(X >T->data)T->right = Insert (x,T->right);return T;} void DLR (SearchtreeT,string& s) {if(T!=NULL) {S.push_back (T->data); cout<<T->data<<endl; DLR (T->left,s); DLR (T->right,s); }}int Main () {int b[]={5,6,7,4,3,2}; int a[]={5,4,3,2,6,7}; int c[]={5,7,6,3,4,2};    String sb;    String sa;    String SC; Searchtree tb=NULL; Searchtree Ta=NULL; Searchtree tc=NULL; for(int i=0;i<6; ++i) {Tb=insert (B[I],TB);        Ta=insert (A[i],ta);    Tc=insert (C[I],TC);    } DLR (TB,SB);    DLR (TA,SA); DLR (TC,SC);if(SB==SA) cout<<"Yes"<<endl;Elsecout<<"No"<<endl;if(SB==SC) cout<<"Yes"<<endl;Elsecout<<"No"<<endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The process of reproducing the recursive construction of binary 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.