C语言 统计二叉树叶子结点,度为2的结点、所有结点个数(非递归,利用栈)
#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 Isempty(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;
}
}
}
}
void CountLeafNode(Node* root)
{
Stack* sta;
sta = (Stack*)malloc(sizeof(Stack));
InitStack(sta);
Node* p;
p = malloc(sizeof(Node));
p = root;
Node* q;
q = malloc(sizeof(Node));
int count = 0;
PushStack(sta, p);
while (sta->top != -1)
{
q = PopStack(sta);
p = q;
if (p->left && p->right)
{
PushStack(sta, p->right);
PushStack(sta, p->left);
}
else if (p->left && p->right == NULL)
{
PushStack(sta, p->left);
}
else if (p->right && p->left == NULL)
{
PushStack(sta, p->right);
}
else
{
count++;
}
}
printf("%d \n", count);
}
void CountTwoNode(Node* root)
{
Stack* sta;
sta = (Stack*)malloc(sizeof(Stack));
InitStack(sta);
Node* p;
p = malloc(sizeof(Node));
p = root;
Node* q;
q = malloc(sizeof(Node));
int count = 0;
PushStack(sta, p);
while (sta->top != -1)
{
q = PopStack(sta);
p = q;
if (p->left && p->right)
{
PushStack(sta, p->right);
PushStack(sta, p->left);
count++;
}
else if (p->left && p->right == NULL)
{
PushStack(sta, p->left);
}
else if (p->right && p->left == NULL)
{
PushStack(sta, p->right);
}
}
printf("%d \n", count);
}
void CountAllNode(Node* root)
{
Stack* sta;
sta = (Stack*)malloc(sizeof(Stack));
InitStack(sta);
Node* p;
p = malloc(sizeof(Node));
p = root;
Node* q;
q = malloc(sizeof(Node));
int count = 0;
PushStack(sta, p);
while (sta->top != -1)
{
q = PopStack(sta);
p = q;
if (p->left && p->right)
{
PushStack(sta, p->right);
PushStack(sta, p->left);
count++;
}
else if (p->left && p->right == NULL)
{
PushStack(sta, p->left);
count++;
}
else if (p->right && p->left == NULL)
{
PushStack(sta, p->right);
count++;
}
else
{
count++;
}
}
printf("%d \n", count);
}
int main()
{
int arr[5] = { 3,2,5,4,6 };
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");
printf("the non-recursion of leveorder result:");
leveinorder(root);
printf("\n");
CountLeafNode(root);
CountTwoNode(root);
CountAllNode(root);
}