04-Tree 4 Whether the same binary search tree

Source: Internet
Author: User

Given an insertion sequence, you can uniquely identify a binary search tree. However, a given two-fork search tree can be obtained from a number of different insertion sequences. For example, insert the initial empty 3-fork search tree by sequence {2, 1, 3}, and {2, 1, two}, all with the same result. So for the various insertion sequences of the input, you need to determine whether they can generate the same two-fork search tree.

Input format:

The input contains several sets of test data. The 1th row of each set of data gives two positive integersN≤) andl, The number of elements to be inserted in each sequence and the number of sequences to check. Line 2nd gives n a space-delimited positive integer as the initial insert sequence. Last l line, each row gives n inserted elements , belonging to l a sequence that needs to be checked.

For simplicity's sake, we guarantee that each insertion sequence is a 1 to n arrangement. When read n is 0 o'clock, the flag input ends and this set of data is not processed.

Output format:

For each set of sequences that need to be checked, if the resulting two-fork search tree is generated as the corresponding initial sequence, the output is "Yes", otherwise the output "No".

Input Sample:
4 23 1 4 23 4 1 23 2 4 12 12 11 20
Sample output:
YesNoNo


1#include <stdio.h>2#include <stdlib.h>3 4typedefstructTnode *Ptrtotnode;5 structtnode{6     intData;7 Ptrtotnode right, left;8 };9 typedef ptrtotnode Tree;Ten  One //Two fork search tree Insert function ATree Insert (Tree BST,intdata) { -     if(!BST) { -BST = (Tree)malloc(sizeof(structtnode)); theBst->data =data; -Bst->left = Bst->right =NULL; -     } -     Else{ +         if(Bst->data <data) { -Bst->right = Insert (bst->Right , data); +         } A         Else if(Bst->data >data) { atBst->left = Insert (bst->Left , data); -         } -     } -     returnBST; - } -  in //recursively determine if each node is equal to BST1 and BST2. - BOOLComparebst (tree BST1, tree BST2) { to     BOOLFlag; +     if(! BST1 &&! BST2) flag =true; -     Else if(Bst1->data = = bst2->Data) { theFlag = (Comparebst (bst1->left, Bst2->left) && Comparebst (Bst1->right, bst2->Right )); *     } $     ElseFlag =false;Panax Notoginseng     returnFlag; - } the   + intMain () { A      while(1){ the         intN, L, data; +scanf"%d", &N); -         if(n==0) Break;//according to test instructions, when n==0 jumps out of circulation $scanf"%d", &L); $Tree BST1 =NULL; -          for(intI=0; i<n; i++){            -scanf"%d", &data); theBST1 =Insert (BST1, data); -         }Wuyi          while(l--){ theTree BST2 =NULL; -              for(intI=0; i<n; i++){ Wuscanf"%d", &data); -BST2 =Insert (BST2, data); About             } $             if(Comparebst (BST1, BST2)) printf ("yes\n"); -             Elseprintf"no\n"); -         } -     } A     return 0; +}

04-Tree 4 Whether the same 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.