[Programming question] judge whether the integer sequence is the result of post-sequential traversal of the Binary Search Tree. If so, construct the binary search tree.

Source: Internet
Author: User

Judge whether the integer sequence is the result of Binary Search Tree's post-sequential Traversal
Question: enter an integer array to determine whether the array is the result of a binary search tree's sequential traversal.
If true is returned, otherwise false is returned.
For example, input 5, 7, 6, 9, 11, 10, and 8, because the integer sequence is the result of post-sequential traversal of the following tree:
8
/\
6 10
/\/\
5 7 9 11
Therefore, true is returned.
If 7, 4, 6, and 5 are input, the result of post-sequential traversal of no tree is this sequence, so false is returned.

 

 

I started to be stupid when I was doing this. I thought about how to judge from the past to the next based on the relationship between numbers. Later, I suddenly realized that based on the features of post-sequential traversal. The last number of the sequence is the number of the root node. You only need to insert the number into the binary search tree from the back to the top.

The tree can be built successfully, because we just keep inserting data. However, it is not certain whether the result of this constructed tree conforms to the post-sequential traversal is a given sequence. Need to judge.

Principles of judgment:

Because we insert numbers from the back of the sequence to the front. This indicates that the point to be inserted first should appear after the next traversal. If the left subtree of a node of a tree has been constructed, it means that you should not access the right subtree again later, because the left subtree is accessed first in the traversal process, and then the right subtree is accessed. You can determine whether the input sequence is valid.

 

Code: write the code for one night. When the build fails, the system traverses and releases the memory in sequence. After the function is generated, t is released, but it is not the desired null, but a random value. I don't know where the problem is. However, the overall function is correct.

Code function: judge whether the result is a binary search tree in descending order based on input. If yes, build the binary search tree.

# Include <stdio. h> # include <stdlib. h ># include <vector> using namespace STD; typedef struct binarytree {int data; struct binarytree * rchild, * lchild;} binarytree; void visit (binarytree * & P) {If (P! = NULL) {free (p); P = NULL ;}} int aferordertraverse (binarytree * & T) // returns the {binarytree * P; vector <binarytree *> stack; int tag [30] = {0}; // use the tag to record whether the left subtree 0 is saved or the right subtree 1 int tagnum = 0; // The number of records stored in the stack. push_back (t); tag [0] = 0; tagnum = 1; while (! Stack. Empty () {While (P = stack. Back ())! = NULL) {stack. push_back (p-> lchild); tag [tagnum ++] = 0;} // as long as the right subtree pops up, the parent node is accessed and the while (TAG [tagnum-1] = 1) // if the right subtree pops up and the starting empty set is used to access its parent node (it must be the previous node), the {stack will pop up. pop_back (); tagnum --; visit (stack. back ();} stack. pop_back (); tagnum --; If (! Stack. empty () {P = stack. back (); stack. push_back (p-> rchild); tag [tagnum ++] = 1 ;}return 0 ;}// input, the array to be constructed, the length of the array and the binary search tree to be returned. 1 indicates that the construction is successful. 0 indicates that the construction fails. Int treefromafterorder (int * In, int Leng, binarytree * & T) {int n = Leng; T = NULL; // make the root of the tree empty initially while (n! = 0) // Insert the node one time from the back to the front {binarytree * TMP = (binarytree *) malloc (sizeof (binarytree )); // construct the currently inserted node TMP-> DATA = in [n-1]; // The data TMP-> lchild = NULL; TMP-> rchild = NULL; If (t = NULL) // if the root is empty, set the current node to the root {T = TMP ;} else // if the root is not null {binarytree * x = T; binarytree * PX = NULL; while (X! = NULL) // cyclically query the inserted location and determine whether it is legal {If (TMP-> DATA> X-> data) {If (X-> lchild! = NULL) // when moving to the right, the left subtree must be empty because the point is inserted from the back to the front. If the left subtree is not empty, the right subtree should have been accessed and should not be accessed again. Otherwise sequential traversal condition {aferordertraverse (t ); // release the allocated memory return 0;} else {PX = x; X = x-> rchild ;}} else {PX = X; X = x-> lchild; }}if (TMP-> DATA> PX-> data) {PX-> rchild = TMP ;} else {PX-> lchild = TMP;} n --;} return 1;} int main () {int A [8] = }; int B [4] = {7, 4, 6, 5}; binarytree * t = NULL; int result = treefromafterorder (B, 4, T); If (result = 1) {printf ("success");} else {printf ("wrong");} 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.