Binary Tree

广义表建立二叉树

struct node *Create(char *str)
{
    //str是二叉树的广义表表示的字符串
    //st是栈空间,b是新建二叉链表的根指针
    struct node *St[100],*P = NULL,*b;
    int top = -1,k,j = 0;
    char ch;
    ch = str[j];
    //初始化的二叉链为空
    b = NULL;

    for (j = 0; str[j] != '\0';j ++)
    {
        ch = str[j];
        switch (ch)
        {
            //作为左结点
            case '(':
                top ++;
                St[top] = P;
                k =1;
                break;
            case ')':
                top --;
                break;
                //作为右结点
            case ',':
                k = 2;
                break;
            default:
                P = (struct node *)malloc(sizeof(struct node));
                P->data = ch;
                P->left = P->right = NULL;
                if (b == NULL)
                {
                    // p指向二叉树的根结点
                    b = P;
                }
                else
                {
                    switch(k)
                    {
                        case 1:
                            St[top]->left= P;
                            break;
                        case 2:
                            St[top]->right = P;
                            break;
                    }
                }
        }
    }
    return b;
}

后续遍历(非递归)

// C program for iterative postorder traversal using one stack
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

// A tree node
struct Node
{
    int data;
    struct Node *left, *right;
};

// Stack type
struct Stack
{
    int size;
    int top;
    struct Node* *array;
};

// A utility function to create a stack of given size
struct Stack* createStack(int size)
{
    struct Stack* stack =
            (struct Stack*) malloc(sizeof(struct Stack));
    stack->size = size;
    stack->top = -1;
    stack->array =
            (struct node**) malloc(stack->size * sizeof(struct node*));
    return stack;
}

// BASIC OPERATIONS OF STACK
int isFull(struct Stack* stack)
{  return stack->top - 1 == stack->size; }

int isEmpty(struct Stack* stack)
{  return stack->top == -1;  }

void push(struct Stack* stack, struct node* node)
{
    if (isFull(stack))
        return;
    stack->array[++stack->top] = node;
}

struct node* pop(struct Stack* stack)
{
    if (isEmpty(stack))
        return NULL;
    return stack->array[stack->top--];
}

// An iterative function to do post order traversal of a given binary tree
void postOrderIterative(struct node* root)
{
    if (root == NULL)
        return;

    // Create two stacks
    struct Stack* s1 = createStack(MAX_SIZE);
    struct Stack* s2 = createStack(MAX_SIZE);

    // push root to first stack
    push(s1, root);
    struct node* node;

    // Run while first stack is not empty
    while (!isEmpty(s1))
    {
        // Pop an item from s1 and push it to s2
        node = pop(s1);
        push(s2, node);

        // Push left and right children of removed item to s1
        if (node->left)
            push(s1, node->left);
        if (node->right)
            push(s1, node->right);
    }

    // Print all elements of second stack
    while (!isEmpty(s2))
    {
        node = pop(s2);
        printf("%c", node->data);
        //cout << node->data;
    }
}

Construct Tree from given Inorder and Preorder traversals

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define bool int
#define true 1
#define false 0

bool flag = true;

int preIndex = 0;

struct node {
    char data;
    struct node* left;
    struct node* right;
};

struct node* newNode(char new_data) {
    struct node* temp = (struct node*)malloc(sizeof(struct node));
    temp->data = new_data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

int Search(char in[], int inStart, int inEnd, char value) {
    int i = 0;
    for (i = inStart; i <= inEnd; i++) {
        if (in[i] == value) {
            return i;
        }
    }

    return -1;
}

struct node* buildTree(char in[], char pre[], int inStart, int inEnd) {


    if (inStart > inEnd) {
        return NULL;
    }

    struct node* tNode = newNode(pre[preIndex++]);

    if (inStart == inEnd) {
        return tNode;
    }

    int inIndex = Search(in, inStart, inEnd, tNode->data);

    if (inIndex == -1) {
        flag = false;

        return NULL;
    }

    tNode->left = buildTree(in, pre, inStart, inIndex - 1);
    tNode->right = buildTree(in, pre, inIndex + 1, inEnd);

    return tNode;
}

void postOrder(struct node* root) {
    if (root == NULL) {
        return;
    }

    postOrder(root->left);
    postOrder(root->right);
    printf("%c", root->data);
}

int main() {
//    freopen("test.txt", "r", stdin);
    char pre[26];
    char in[26];
    while (~scanf("%s", pre)) {
        /*int pre[] = {1, 2, 4, 7, 3, 5, 6, 8};
        int in[] = {4, 7, 2, 1, 5, 3, 8, 6};*/

        scanf("%s", in);

        struct node* root = buildTree(in, pre, 0, strlen(pre) - 1);

        if (!flag) {
            printf("No\n");
        } else {
            postOrder(root);
            printf("\n");
        }

        preIndex = 0;
        flag = true;
    }

    /*int pre[] = {1, 2, 4, 7, 3, 5, 6, 8};
    int in[] = {4, 7, 2, 1, 5, 3, 8, 6};

    struct node* root = buildTree(in, pre, 0, len - 1);
    postOrder(root);*/

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值