#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define MAX 100
typedef struct node//build struct node
{
int data;
int tap;
struct node* left;
struct node* right;
} Node;
typedef struct
{
Node * data[MAX];
int top;
}Stack;
void InitStack(Stack* sta)
{
sta->top = -1;
}
## 标题
void PushStack(Stack* sta, Node * p)
{
sta->data[sta->top++] = p;
}
Node * PopStack(Stack* sta)
{
return sta->data[--sta->top];
}
void PrintfStack(Stack* sta)
{
if (sta->top == -1)
{
printf("the stack is empty\n");
}
else
{
printf("the stack is:");
int p;
p = sta->top;
while (p != -1)
{
printf("%d ", sta->data[p]);
p--;
}
}
printf("\n");
}
void insert_node(Node* p, int value)//insert value into the root
{
Node* node = malloc(sizeof(Node));//storage allocation
node->data = value;
node->left = NULL;
node->right = NULL;
Node* temp = p;//temporary node
while (temp != NULL)
{
if (value < temp->data)
{
if (temp->left == NULL)
{
temp->left = node;
return;
}
else
{
temp = temp->left;
}
}
else
{
if (temp->right == NULL)
{
temp->right = node;
return;
}
else
{
temp = temp->right;
}
}
}
}
void preorder(Node* node)
{
if (node != NULL)
{
printf("%d ", node->data);
preorder(node->left);
preorder(node->right);
}
}
void inorder(Node* node)
{
if (node != NULL)
{
inorder(node->left);
printf("%d ", node->data);
inorder(node->right);
}
}
void postorder(Node* node)
{
if (node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d ", node->data);
}
}
void preorderstack(Node* root)
{
Stack* sta;
sta = (Stack * )malloc(sizeof(Stack));
InitStack(sta);
Node * p;
p = malloc(sizeof(Node));
p = root;
while (p || sta->top != -1 )
{
while (p)
{
printf("%d ", p->data);
PushStack(sta,p);
p = p->left;
}
if (sta->top != -1)
{
p = PopStack(sta);
p = p->right;
}
}
}
void inorder_stack(Node* root)
{
Stack* sta;
sta = (Stack*)malloc(sizeof(Stack));
InitStack(sta);
Node* p;
p = malloc(sizeof(Node));
p = root;
while (p || sta->top != -1)
{
while (p)
{
//printf("%d ", p->data);
PushStack(sta, p);
p = p->left;
}
if (sta->top != -1)
{
p = PopStack(sta);
printf("%d ", p->data);
p = p->right;
}
}
}
void postorder_stack(Node* root)
{
Stack* sta;
sta = (Stack*)malloc(sizeof(Stack));
InitStack(sta);
Node* p;
p = malloc(sizeof(Node));
p = root;
int falag = 0;
while (p || sta->top != -1)
{
while (p)
{
p->tap = 1;
PushStack(sta, p);
p = p->left;
}
if (sta->top != -1)
{
p = PopStack(sta);
//printf("%d ", p->data);
//p = p->right;
if (p->tap == 1)
{
p->tap++;
PushStack(sta, p);
p = p->right;
}
else if (p->tap == 2)
{
printf("%d ", p->data);
p = NULL;
}
}
}
}
int main()
{
int arr[5] = { 3,1,4,2,5 };
Node* root = malloc(sizeof(Node));
root->data = arr[0];
root->left = NULL;
root->right = NULL;
int i;
for (i = 1; i < 5; i++)
{
insert_node(root, arr[i]);
}
printf("the preorder result:");
preorder(root);
printf("\n");
printf("the inorder result:");
inorder(root);
printf("\n");
printf("the postorder result:");
postorder(root);
printf("\n");
printf("the non-recursion of preorder result:");
preorderstack(root);
printf("\n");
printf("the non-recursion of preorder result:");
inorder_stack(root);
printf("\n");
printf("the non-recursion of preorder result:");
postorder_stack(root);
printf("\n");
}
2021-09-28
最新推荐文章于 2024-09-13 16:27:15 发布