#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode
{
int value;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;
typedef struct QNode
{
TreeNode *value;
struct QNode *next;
}QNode;
typedef struct LinkQueue
{
QNode *front;
QNode *rear;
int size;
}LinkQueue;
typedef struct SNode
{
TreeNode *value;
struct SNode *next;
}SNode;
typedef struct LinkStack
{
SNode *top;
int size;
}LinkStack;
TreeNode* createNode(int value)
{
TreeNode *node = malloc(sizeof(TreeNode));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
void addNode(TreeNode **root, int value)
{
if (*root == NULL)
{
*root = createNode(value);
}
else
{
if (value < (*root)->value)
{
addNode(&(*root)->left, value);
}
else
{
addNode(&(*root)->right, value);
}
}
}
void inQueue(LinkQueue *queue, TreeNode *value)
{
QNode *node = (QNode*)malloc(sizeof(QNode));
node->value = value;
node->next = queue->rear->next;
queue->rear->next = node;
queue->rear = node;
queue->size += 1;
}
TreeNode * outQueue(LinkQueue *queue)
{
QNode *pop = queue->front->next;
queue->front->next = pop->next;
if (pop == queue->rear)
{
queue->rear = queue->front;
}
queue->size -= 1;
TreeNode *node = pop->value;
free(pop);
return node;
}
void pushStack(LinkStack *stack,TreeNode *node)
{
SNode *snode = (SNode*)malloc(sizeof(SNode));
snode->value = node;
snode->next = stack->top;
stack->top = snode;
stack->size++;
}
TreeNode* popStack(LinkStack *stack)
{
if (stack->size <= 0)
{
return NULL;
}
SNode* pop = stack->top;
stack->top = pop->next;
stack->size--;
TreeNode *node = pop->value;
free(pop);
return node;
}
void preOrder(TreeNode *root)
{
if (root == NULL)
{
return;
}
printf("%d ", root->value);
preOrder(root->left);
preOrder(root->right);
}
void preOrderTravese(TreeNode *root)
{
if (root == NULL)
{
return;
}
LinkStack *stack = (LinkStack *)malloc(sizeof(LinkStack));
stack->top = NULL;
stack->size = 0;
pushStack(stack, root);
while (stack->size > 0)
{
TreeNode *node = popStack(stack);
printf("%d ", node->value);
if (node->right)
{
pushStack(stack, node->right);
}
if (node->left)
{
pushStack(stack, node->left);
}
}
free(stack);
}
void preOrderTravese1(TreeNode *root)
{
if (root == NULL)
{
return;
}
LinkStack *stack = (LinkStack *)malloc(sizeof(LinkStack));
stack->top = NULL;
stack->size = 0;
TreeNode *node = root;
while (node || stack->size > 0)
{
if (node)
{
printf("%d ", node->value);
pushStack(stack, node);
node = node->left;
}
else
{
node = popStack(stack);
node = node->right;
}
}
free(stack);
}
void inOrder(TreeNode *root)
{
if (root == NULL)
{
return;
}
inOrder(root->left);
printf("%d ", root->value);
inOrder(root->right);
}
void inOrderTravese(TreeNode *root)
{
if (root == NULL)
{
return;
}
LinkStack *stack = (LinkStack *)malloc(sizeof(LinkStack));
stack->top = NULL;
stack->size = 0;
TreeNode *node = root;
while (node || stack->size > 0)
{
if (node)
{
pushStack(stack,node);
node = node->left;
}
else
{
node = popStack(stack);
printf("%d ", node->value);
node = node->right;
}
}
free(stack);
}
void postOrder(TreeNode *root)
{
if (root == NULL)
{
return;
}
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->value);
}
void postOrderTraverse(TreeNode *root)
{
if (root == NULL)
{
return;
}
LinkStack *stack = (LinkStack *)malloc(sizeof(LinkStack));
stack->top = NULL;
stack->size = 0;
LinkStack *poststack = (LinkStack *)malloc(sizeof(LinkStack));
poststack->top = NULL;
poststack->size = 0;
pushStack(stack, root);
while (stack->size)
{
TreeNode *node = popStack(stack);
pushStack(poststack, node);
if (node->left)
{
pushStack(stack, node->left);
}
if (node->right)
{
pushStack(stack, node->right);
}
}
while (poststack->size)
{
TreeNode *node = popStack(poststack);
printf("%d ", node->value);
}
free(poststack);
free(stack);
}
void levelOrder(TreeNode *root)
{
if (root == NULL)
{
return;
}
LinkQueue *queue = (LinkQueue*)malloc(sizeof(LinkQueue));
QNode *head = (QNode*)malloc(sizeof(QNode));
head->value = NULL;
head->next = NULL;
queue->front = head;
queue->rear = head;
queue->size = 0;
inQueue(queue,root);
while(queue->size)
{
TreeNode *node = outQueue(queue);
if (node->left)
{
inQueue(queue, node->left);
}
if (node->right)
{
inQueue(queue, node->right);
}
int value = node->value;
printf("%d ", value);
}
}
int main()
{
int values[11] = { 7,3,11,1,5,10,14,2,4,9,16 };
TreeNode *tree_root = createNode(values[0]);
for (int i = 1; i < 11; i++)
{
addNode(&tree_root, values[i]);
}
preOrder(tree_root);
printf("\n");
inOrder(tree_root);
printf("\n");
postOrder(tree_root);
printf("\n\n\n");
preOrderTravese(tree_root);
printf("\n");
inOrderTravese(tree_root);
printf("\n");
postOrderTraverse(tree_root);
printf("\n\n\n");
levelOrder(tree_root);
printf("\n");
return 0;
}
前序 中序 后序遍历 递归解法 不多说 几行代码的事儿
非递归的解法就是利用栈来解决
层次遍历是用队列来解决。。。。。。。
preOrderTravese1 和 inOrderTravese 很像
preOrderTravese 和 postOrderTraverse 很像