简体   繁体   English

获取二叉树节点

[英]getting binary tree node

I made non recursive postorder using stack. 我使用堆栈进行了非递归邮购。 But in Main function, rather than just typing all the TreeNodes by hand, I want to get TreeNodes using scanf. 但是在Main函数中,我不仅想手动键入所有TreeNode,还想使用scanf获得TreeNode。 For example, printf("How many TreeNodes do you want?"); 例如,printf(“您要多少个TreeNode?”); and get numbers of TreeNodes and then printf("enter TreeNodes"); 并获取TreeNodes的数量,然后获取printf(“ enter TreeNodes”); so I enter 1 2 3 4 5 like this. 所以我这样输入1 2 3 4 5 How can I code it? 我该如何编码?

#include <stdio.h>
#include <stdbool.h>
#define STACK_SIZE 10
typedef struct TreeNode {
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;

typedef struct Stack {
    TreeNode *buf[STACK_SIZE];
    int top;
}Stack;


void postOrder(TreeNode *root, Stack *stack)
{
    Stack* s = stack;
    if (root == NULL) return;
    TreeNode* current = root;
    TreeNode *tmp;
    bool done = 0;
    InitStack(s);


    while(!done)
    {
        while (current != NULL)
        {
            if (current->right != NULL)
                Push(s, current->right);
            Push(s, current);
            current = current->left;
        }
        if (IsEmpty(s))
            break;
        current = Pop(s);
        if (IsEmpty(s))
        {
            printf("%d", current->data);
            break;
        }
        tmp = Pop(s);
        if (tmp == current->right)
        {
            Push(s, current);
            current = current->right;
        }
        else
        {
            printf("%d", current->data);
            Push(s, tmp);
            current = NULL;
        }
    }
}
int main()
{   
    Stack s;
    TreeNode one, two, three, four, five;

    one.data = 1;
    two.data = 2;
    three.data = 3;
    four.data = 4;
    five.data = 5;

    one.left = &two;        one.right = &three;
    two.left = &four;       two.right = &five;
    three.left = NULL;      three.right = NULL;
    four.left = NULL;       four.right = NULL;
    five.left = NULL;       five.right = NULL;

    postOrder(&one, &s);

    printf("\n");
    getchar();

    return 0;
}

Implement a separate method to construct tree using dynamically memory allocation. 实现一种单独的方法来使用动态内存分配来构造树。 Use malloc function to allocate memory. 使用malloc函数分配内存。 Insert the new node to tree at appropriate position. 将新节点插入树的适当位置。

void insertNode(TreeNode *root,int data){
        //To Do create node dynamically using malloc and attach to root at appropriate position
    }


Method to read input will look like below main function: 读取输入的方法如下主要功能所示:

int main()
{
    Stack s;
    TreeNode *rootNode = NULL;
    int nodeCount;
    int data;
    printf("Enter number of nodes:\n");
    scanf("%d",&nodeCount);
    printf("enter TreeNodes Data:\n");
    for (int i=0; i < nodeCount; i++) {
        scanf("%d",&data);
        insertNode(rootNode,data);
    }
    postOrder(rootNode, &s);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM