美文网首页
数据结构-树

数据结构-树

作者: harrytc | 来源:发表于2018-10-07 19:25 被阅读19次

    对于树的数据结构中我感觉最大的难点就属递归了,递归的思想说起貌似很好懂,但是,如果写的话还是似会非会,此处需要加强学习。

    //  main.c
    //  treee
    //
    //  Created by harrytc on 2018/10/7.
    //  Copyright © 2018年 harrytc. All rights reserved.
    //
    
    #include <stdio.h>
    # include <stdlib.h>
    #define maxSize 100
    typedef struct BTNode
    {
        char data;
        struct BTNode * lchild;
        struct BTNode * rchild;
    }BTNode;
    
    
    
    void Visit(BTNode *p)
    {
        
    }
    
    int getDepth (BTNode * p)
    {
        int LD,RD;
        if (p == NULL)
            return 0;
        else
        {
            LD = getDepth(p->lchild);
            RD = getDepth(p->rchild);
            return (LD > RD ? LD : RD) + 1;
        }
    }
    
    void trave (BTNode * p, int k)
    {
        int n = 0;
        if (p != NULL)
        {
            ++n;
            if (k == n)
            {
                printf("%c \n",p->data);
            }
            return;
        }
        trave(p->lchild, k);
        trave(p->rchild, k);
    }
    
    void preorder(BTNode *p) /*前序递归遍历*/
    {
        if(p != NULL)
        {
            Visit(p);
            preorder(p->lchild);
            preorder(p->rchild);
        }
    }
    
    void preorderNonrecursion(BTNode *bt) /*前序非递归遍历*/
    {
        BTNode * Stack[maxSize];
        int top = -1;
        BTNode * p;
        Stack[++top] = bt;
        while(top != -1)
        {
            p = Stack[top--];
            Visit(p);
            if (p->rchild != NULL)
                Stack[top++] = p->rchild;
            if (p->lchild != NULL)
                Stack[top--] = p->lchild;
        }
    }
    
    void inorder(BTNode * p)    /*中序遍历*/
    {
        if(p != NULL)
        {
            inorder(p->lchild);
            Visit(p);
            inorder(p->rchild);
        }
    }
    
    
    
    void postorder(BTNode * p)    /*后序遍历*/
    {
        if (p != NULL)
        {
            postorder(p->lchild);
            postorder(p->rchild);
            Visit(p);
        }
    }
    
    
    
    int main(int argc, char *argv[])
    {
        struct BTNode * pA = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pB = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pC = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pD = (struct BTNode *)malloc(sizeof(struct BTNode));
        struct BTNode * pE = (struct BTNode *)malloc(sizeof(struct BTNode));
        
        pA->data = 'A';
        pB->data = 'B';
        pC->data = 'C';
        pD->data = 'D';
        pE->data = 'E';
        
        pA->lchild = pB;
        pA->rchild = pC;
        pB->lchild = pB->rchild = NULL;
        pC->lchild = pD;
        pC->rchild = NULL;
        pD->lchild = NULL;
        pD->rchild = pE;
        pE->lchild = pE->rchild = NULL;
        printf("%d \n", getDepth(pA));
        
    }
    

    最近比较忙,晚些时候会把注释补详细

    相关文章

      网友评论

          本文标题:数据结构-树

          本文链接:https://www.haomeiwen.com/subject/utqraftx.html