/*
1. The stack pops the node
A false
*/
# include "LinkStack.h"
# define MY_FALSE 0
# define MY_TRUE 1
// binary tree node
typedef struct BINARYNODE
{
char ch;
struct BINARYNODE* lchild;
struct BINARYNODE* rchild;
}BinaryNode;
// non-recursive traversal of binary tree
typedef struct BITREESTACKNODE
{
LinkNode node;
BinaryNode* root;
int flag;
}BiTreeStackNode;
//Create a node in the stack
BiTreeStackNode* CreatBiTreeStackNode(BinaryNode* node, int flag)
{
BiTreeStackNode* newnode = (BiTreeStackNode*)malloc(sizeof(BiTreeStackNode));
newnode->root = node;
newnode->flag = flag;
return newnode;
}
// non-recursive traversal
void NonRecursion(BinaryNode* root)
{
//create stack
LinkStack* stack = Init_LinkStack();
// throw the root node to the top of the stack
Push_LinkStack(stack, (LinkNode*)CreatBiTreeStackNode(root, MY_FALSE));
while(Size_LinkStack(stack))
{
// first pop the top element of the stack
BiTreeStackNode* node = (BiTreeStackNode*)Top_LinkStack(stack);
Pop_LinkStack(stack);
/ / Determine whether the popped node is empty
if(node->root == NULL)
{
continue;
}
if(node->flag == MY_TRUE)
{
printf("%c", node->root->ch);
}
else
{
// preorder traversal
# if 0
// The right node of the current node is pushed onto the stack
Push_LinkStack(stack,(LinkNode*)CreatBiTreeStackNode(node->root->rchild, MY_FALSE));
//The left node of the current node is pushed onto the stack
Push_LinkStack(stack,(LinkNode*)CreatBiTreeStackNode(node->root->lchild, MY_FALSE));
// push the current node to the stack
node->flag = MY_TRUE;
Push_LinkStack(stack, (LinkNode*)node);
# endif
// in-order traversal
// The right node of the current node is pushed onto the stack
Push_LinkStack(stack,(LinkNode*)CreatBiTreeStackNode(node->root->rchild, MY_FALSE));
//The left node of the current node is pushed onto the stack
// Push_LinkStack(stack,(LinkNode*)CreatBiTreeStackNode(node->root->lchild, MY_FALSE));
// push the current node to the stack
node->flag = MY_TRUE;
Push_LinkStack(stack, (LinkNode*)node);
//The left node of the current node is pushed onto the stack
Push_LinkStack(stack,(LinkNode*)CreatBiTreeStackNode(node->root->lchild, MY_FALSE));
}
}
};
// preorder traversal
void Recursion(BinaryNode* root)
{
if(root == NULL)
{
return ;
}
printf("%c", root->ch);
//print left subtree
Recursion(root->lchild);
//print the right subtree
Recursion(root->rchild);
}
// in-order traversal
void Recursion_1(BinaryNode* root)
{
if(root == NULL)
{
return ;
}
//print left subtree
Recursion(root->lchild);
printf("%c", root->ch);
//print the right subtree
Recursion(root->rchild);
}
void CrestBinaryTree()
{
//create node
BinaryNode node1 = {'A', NULL, NULL};
BinaryNode node2 = {'B', NULL, NULL};
BinaryNode node3 = {'C', NULL, NULL};
BinaryNode node4 = {'D', NULL, NULL};
BinaryNode node5 = {'E', NULL, NULL};
BinaryNode node6 = {'F', NULL, NULL};
BinaryNode node7 = {'G', NULL, NULL};
BinaryNode node8 = {'H', NULL, NULL};
//create node relationship
node1.lchild = &node2;
node1.rchild = &node6;
node2.rchild = &node3;
node3.lchild = &node4;
node3.rchild = &node5;
node6.rchild = &node7;
node7.lchild = &node8;
//non-recursive printing of binary tree
NonRecursion(&node1);
printf("\n");
//Recursive printing of binary tree
Recursion(&node1);
printf("\n");
//Recursive printing of binary tree
Recursion_1(&node1);
printf("\n");
}
int main(int argc, char *argv[])
{
CrestBinaryTree();
return 0;
}