Data structure - tree (build, traverse)

We are currently preparing to move in autumn 2020 session of the algorithm engineer, review the data structure!

Found that tree traversal can achieve the complexity of O (n) time, O (1) space complexity ( Morris traversal ), and quickly learn a wave. And review the establishment of trees, some of the basic operation of traversing the tree.

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct TreeNode{
    int val;
    TreeNode *left, *right;
    TreeNode(int x): val(x), left(NULL), right(NULL){}
}; 

//初始化树 
TreeNode* init_tree1(TreeNode *root, int val){
    if(root==NULL){
        root = new TreeNode(val);
        return root;
    }
    if(val<root->val){
        root->left = create_tree(root->left, val); 
    }else{
        root->right = create_tree(root->right, val); 
    }
    
    return root;
}

//初始化树 
void init_tree2(TreeNode *&root){
    string data;
    cin>>data;
    if(data=="#"){
        root=NULL;
    }else{
        root = new TreeNode(stoi(data));
        init_tree(root->left);
        init_tree (the root -> right); 
    } 
} 
// traversal sequence, the time complexity of O (n), the spatial complexity is O (. 1) 
void MorrisTraversal (the TreeNode * the root) { 
    the TreeNode * = the root CUR, * PREV = NULL;
     the while (! CUR = NULL) {
         // 1. If the left subtree node is null, the output 
        IF (cur> left == NULL) { 
            COUT << cur> Val << "  " ; 
            CUR = cur > right;    // points to the parent node 
        } the else { 
            PREV = cur> left;
             the while(prev-> right = NULL && prev-> right =!! CUR) { 
                PREV = prev-> right;               // Get the left subtree of the current node rightmost node 
            }
             IF (prev-> right == NULL) { 
                PREV -> right = CUR; 
                CUR = cur> left; 
            
            } the else  IF (prev-> right == CUR) {
                 // description of the immediate predecessor node visited, the access node 
                COUT cur <<> << Val "  " ;
                 // next points to the right child node, proceed 
                CUR = cur> right; 
                PREV-> right = NULL; // returns to its original configuration node 
            } 
        } 
        
    } 
    COUT << endl; 
} 

// traversal sequence, the time complexity of O (n), the spatial complexity of O (n-) 
void InOrder (the TreeNode * the root) {
     IF (the root) { 
        InOrder (the root -> left); 
        COUT << directory root-> Val << "  " ; 
        InOrder (the root -> right);      
    } 
} 

// hierarchy traversal 
void levelOrder (the TreeNode * the root) { 
    Queue < * the TreeNode> Q; 
    q.push (the root); 
    the while(!q.empty()){
        int size = q.size();
        for(int i=0;i<size;i++){
            TreeNode *temp = q.front();
            q.pop();
            cout<<temp->val<<" ";
            if(temp->left) q.push(temp->left);
            if(temp->right) q.push(temp->right);
        }
        cout<<endl;
    }
}
int main(){
    TreeNode *root=NULL;
    int s[] = {4,2,6,1,3,5};
    for(int i=0;i<6;i++)
        root = init_tree1(root, s[i]);
    //init_tree2(root);
    levelOrder(root);
    
    cout<<"Morris results: "<<endl;
    MorrisTraversal(root);
    
    cout<<"\nRecursive results:"<<endl;
    inorder(root);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/logo-88/p/11273222.html
Recommended