Binary Tree

来源:互联网 发布:java 反射 实例化单例 编辑:程序博客网 时间:2024/06/10 04:12

广义表建立二叉树

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 nodestruct Node{    int data;    struct Node *left, *right;};// Stack typestruct Stack{    int size;    int top;    struct Node* *array;};// A utility function to create a stack of given sizestruct 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 STACKint 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 treevoid 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 0bool 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