当前位置:编程学习 > C/C++ >>

二叉树的基本操作

由于岗位实践需要,故写了一个树的基本操作,包括先,中,后序递归和非递归,计算高度,计算左子树个数,无其他用处,警示自己而已 ..
[cpp] 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <stack> 
using namespace std; 
 
// tree node 
typedef struct TreeNode { 
    char data; 
    struct TreeNode* lChild; 
    struct TreeNode* rChild; 
} Node; 
 
int n; 
stack<Node*> s; 
 
// 先序 create tree 
void createTree(Node **t, char *c) 

    n++; 
    if(n > strlen(c) - 1) return; 
    if(c[n] == '0') *t = NULL; 
    else  
    { 
        (*t) = (Node*)malloc(sizeof(Node)); 
        (*t)->data = c[n]; 
        createTree(&(*t)->lChild, c); 
        createTree(&(*t)->rChild, c); 
    } 

// 先序递归访问 
void preVisit(Node *t) 

    if(t) 
    { 
        printf("%c", t->data); 
        preVisit(t->lChild); 
        preVisit(t->rChild); 
    } 

// 中序递归访问 
void midVisit(Node *t) 

    if(t) 
    { 
        midVisit(t->lChild); 
        printf("%c", t->data); 
        midVisit(t->rChild); 
    } 

// 后序递归访问 
void lastVisit(Node *t) 

    if(t) 
    { 
        lastVisit(t->lChild); 
        lastVisit(t->rChild); 
        printf("%c", t->data); 
    } 

 
 
// 先序非递归访问 
void uPre(Node* head) 

    while(!s.empty()) s.pop(); 
    while(head || !s.empty()) 
    { 
        while(head) 
        { 
            printf("%c", head->data); 
            s.push(head); 
            head = head->lChild; 
        } 
        if(!s.empty()) 
        { 
            head = s.top(); 
            s.pop(); 
            head = head->rChild; 
        } 
    } 

 
// 中序非递归访问 
void uMid(Node* head) 

    while(!s.empty()) s.pop(); 
    while(head || !s.empty()) 
    { 
        while(head) 
        { 
            s.push(head); 
            head = head->lChild; 
        } 
        if(!s.empty()) 
        {    
            head = s.top(); 
            printf("%c", head->data); 
            s.pop(); 
            head = head->rChild; 
        } 
    } 
 

// 后序非递归访问 
void uLast(Node* head) 

    Node *p = head; 
    head = NULL; 
    while(!s.empty()) s.pop(); 
    do 
    { 
        while(p) 
        { 
            s.push(p); 
            p = p->lChild; 
        } 
        p = s.top(); 
        p = p->rChild; 
        if(head == p || p == NULL) 
        { 
            head = s.top(); 
            s.pop(); 
            printf("%c", head->data); 
            p = NULL; 
        } 
    }while(!s.empty()); 

// 计算高度 
int calH(Node* head) 

    if(!head) return 0; 
    int left = calH(head->lChild); 
    int right = calH(head->rChild); 
    int h; 
    if(left > right) { 
        h = left + 1; 
    } 
    else  
    { 
        h = right + 1; 
    } 
    return h; 

 
// 计算左子叶子个数 
int calLeftChild(Node *t) 

    if(!t) return 0; 
    else if(t->lChild && !t->lChild->lChild && !t->lChild->rChild) return 1; 
    else return calLeftChild(t->lChild) + calLeftChild(t->rChild); 

 
int main() 

    char a[1000]; 
    int c; 
    scanf("%d", &c); 
 
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,