Hierarchical Traversal of Binary Tree UVa122

After getting the problem, don't be afraid and anxious about the seemingly huge problem, just break it down step by step.

As any topic, it requires three parts: input, processing, and output.

The first is input.

Then there is the processing, look at the topic, the level traversal of the binary tree. That's very simple, divided into two parts: binary tree and level traversal.

Binary tree: how to build a tree? How to store input in binary tree?

How to traverse the hierarchy?

As a more troublesome detection input is legal, it is not too late to clean up after the main frame is set up.

#include<cstdio>  
#include<cstring>  
#include<queue>  
#include<vector>  
using namespace std;  
bool failed;  
//If you want to define a binary tree, just like a real tree, what is the basic unit of growth of a tree? is a tree fork, in a binary tree it is
// is a node. Then, back to the mundane situation, any tree, no matter how it grows, must have roots.
// Node definition and operation of binary tree
struct Node{  
	int v; //Any node has a place to save the value of its own node
    bool have_value; //Whether it has been copied
    Node *left,*right;//Recursively build a pointer linked list binary tree  
    Node():have_value(false),left(NULL),right(NULL){}//Constructor, if the destructor is not written, it will be provided by the program by default. Note that the constructor can only be used through new, and the destructor is delete  
//Constructor of the same name, assign an initial value to each quantity, as a function, and finally always pretend to give a {}
};  
//Why is the following u always followed by -> instead of ., because Node is a pointer type!
  
Node* root; //root node of binary tree

 //Every time the tree needs to grow, there needs to be a new node, and in the computer, this requires two steps, one to apply for memory space, that is, to give it a place, and then to manufacture. One radish and one pit, first dig the pit and then make the radish.
Node * newnode () {  
    return new Node();//Apply for new space and call the constructor to initialize  
}  
  
void remove_tree(Node* u){//just try & to see if it is OK to pass a reference, in fact it can be AC  
    if (u==NULL) return;//Judgment in advance  
    remove_tree(u->left);//Recursive release space  
    remove_tree(u->right);  
    delete u; //  
}  
  
void addnode(int v,char* s){//in fact char* is a string,it's the first char's address  
    int n=strlen(s);  
    Node* u=root;//From the root down  
    for (int i=0;i<n;i++){//if it's NULL it won't run this step  
        if (s[i]=='L'){  
            if (u->left==NULL) u->left=newnode();//If the node does not exist, create a new one, otherwise go down  
            u=u->left;  
        }  
        else if (s[i]=='R'){  
            if (u->right==NULL) u->right=newnode();  
            u=u->right;  
        }  
    }  
    if (u->have_value) failed=true;//A value has been assigned, mark it  
    u->v=v;  
    u->have_value=true;  
}  
  
char s[300];  

// read function
bool read_input(){//while reading do not check legitimacy The read-in function does not check for errors when reading in, but resets the initial value of the boolean variable each time it is read in  
    failed=false;  
    remove_tree(root);//Empty the tree, release space (destructor)  
    root=newnode();//Create a root node, call the constructor initialization (constructor) here  
    for (;;){  
        if (scanf("%s",s)!=1) return false;//End of the entire reading  
        if (!strcmp(s,"()")) break;//strcmp function, compare two strings, s1<s2 returns negative, equal to 0, greater than positive, note that the return value is not necessarily  
        int v;  
        sscanf(&s[1],"%d",&v);//sscanf has already read the c string s, and then read it from s, the input source is changed from the keyboard to the c string, read in the form of %d In v, note that the c string regards any pointer to a character as the starting position of the string, so this is equivalent to reading a new string starting from the second position of the string  
        addnode(v,strchr(s,',')+1);//Find the comma, return the pointer to the comma +1  
    }  
    return true;  
}  
  
bool bfs(vector<int>& ans){//pass by reference  
    queue<Node*> q;  
    ans.clear();//Initialization  
    q.push(root);//Initial only root  
    while (!q.empty()){  
        Node* u=q.front();  
        q.pop();//Read the header element and dequeue each time  
        if (!u->have_value) return false;//No value has been assigned, indicating an error  
        ans.push_back(u->v);  
        if (u->left !=NULL) q.push(u->left);  
        if (u->right !=NULL) q.push(u->right);  
    }  
    return true;  
}  
  
int main(){  
    vector<int> ans;  
    while (read_input()){  
        if (!bfs(ans)) failed=1;  
        if (failed) printf("not complete\n");  
        else{  
            for (int i=0;i<ans.size();i++){  
            if (i!=0) printf(" ");  
            printf("%d",ans[i]);  
            }  
            printf("\n");  
        }  
    }  
    return 0;  
}  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325639526&siteId=291194637