码迷,mamicode.com
首页 > 其他好文 > 详细

广义表形式创建二叉树

时间:2018-04-01 14:25:53      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:bsp   body   point   stack   otn   har   argv   oid   --   

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    char data;
    struct node *lchild, *rchild;
} BinTreeNode;
BinTreeNode *createBinTreeByGLists(char *glists, int nodeQuantity);
void preOrderTraverse(BinTreeNode *node);
void inOrderTraverse(BinTreeNode *node);
void postOrderTraverse(BinTreeNode *node);

int main(int argc, const char *argv[]) {
    char *gLists = "(A (B (C, D), E (, F)) )";
    BinTreeNode *rootNode = createBinTreeByGLists(gLists, 6);
    printf("pre order:");
    preOrderTraverse(rootNode);
    printf("\n in order:");
    inOrderTraverse(rootNode);
    printf("\n post order:");
    postOrderTraverse(rootNode);

}
BinTreeNode *createBinTreeByGLists(char *glists, int nodeQuantity)
{
    BinTreeNode *rootNode = NULL;
    BinTreeNode *currNode = NULL;
    BinTreeNode **stack = (BinTreeNode **)malloc(sizeof(BinTreeNode *) * nodeQuantity);
    int top = -1;
    int flag = 0;
    const int START_LEFT_CHILD = 1, START_RIGHT_CHILD = 2;
    int index = 0;
    char c = glists[index];
    while (c != \0) {
        switch (c) {
        case (:
            stack[++top] = currNode;
            flag = START_LEFT_CHILD;
            break;
        case ,:
            flag = START_RIGHT_CHILD;
            break;
        case ):
            top--;
            break;
        case  :
            break;
        default:
            currNode = (BinTreeNode *)malloc(sizeof(BinTreeNode));
            currNode->data = c;
            currNode->lchild = currNode->rchild = NULL;
            if (rootNode == NULL) {
                rootNode = currNode;
            }
            else {
                switch (flag) {
                case START_LEFT_CHILD:
                    stack[top]->lchild = currNode;
                    break;
                case START_RIGHT_CHILD:
                    stack[top]->rchild = currNode;
                    break;
                }
            }
        }
        c = glists[++index];
    }
    free(stack);
    return rootNode;
}
void preOrderTraverse(BinTreeNode *node) {
    if (node != NULL) {
        printf("%c", node->data);
        preOrderTraverse(node->lchild);
        preOrderTraverse(node->rchild);
    }
}
void inOrderTraverse(BinTreeNode *node) {
    if (node != NULL) {
        inOrderTraverse(node->lchild);
        printf("%c", node->data);
        inOrderTraverse(node->rchild);
    }
}
void postOrderTraverse(BinTreeNode *node) {
    if (node != NULL) {
        postOrderTraverse(node->lchild);
        postOrderTraverse(node->rchild);
        printf("%c", node->data);
    }
}

 

广义表形式创建二叉树

标签:bsp   body   point   stack   otn   har   argv   oid   --   

原文地址:https://www.cnblogs.com/diaohaiwei/p/8686297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!