Non-recursive traversal of binary tree

# ifndef LINKSTACK_H
# define LINKSTACK_H

# include <stdio.h>
# include <string.h>
# include <stdlib.h>


//node of chain stack
typedef struct LINKNODE
{
	struct LINKNODE* next;
}LinkNode;

//chain stack
typedef struct LINKSTACK
{
	LinkNode head;
	int size;
}LinkStack;

//initialization function
LinkStack* Init_LinkStack();
// push the stack
void Push_LinkStack(LinkStack* stack, LinkNode* data);
// pop the stack
void Pop_LinkStack(LinkStack* stack);
//return the top element of the stack
LinkNode* Top_LinkStack(LinkStack* stack);
//return the number of stack elements
int Size_LinkStack(LinkStack* stack);
// clear stack
void Clear_SeqStack(LinkStack* stack);
//destroy
void FreeSpace_SeqStack(LinkStack* stack);

# endif



# include "LinkStack.h"

//initialization function
LinkStack* Init_LinkStack()
{
	LinkStack* stack = (LinkStack*)malloc(sizeof(LinkStack));
	stack->head.next = NULL;
	stack->size = 0;
	return stack;
}
// push the stack
void Push_LinkStack(LinkStack* stack, LinkNode* data)
{
	if(stack == NULL)
	{
		return ;
	}
	if(data == NULL)
	{
		return ;
	}

	data->next = stack->head.next;
	stack->head.next = data;
	stack->size++;
}
// pop the stack
void Pop_LinkStack(LinkStack* stack)
{
	if(stack == NULL)
	{
		return ;
	}
	if(stack->size == 0)
	{
		return ;
	}
	// first valid node
	LinkNode* pNext = stack->head.next;
	stack->head.next = pNext->next;
	stack->size--;
}
//return the top element of the stack
LinkNode* Top_LinkStack(LinkStack* stack)
{
	if(stack == NULL)
	{
		return NULL;
	}
	if(stack->size == 0)
	{
		return NULL;
	}
	return stack->head.next;
}
//return the number of stack elements
int Size_LinkStack(LinkStack* stack)
{
	if(stack == NULL)
	{
		return -1;
	}
	return stack->size;
}
// clear stack
void Clear_SeqStack(LinkStack* stack)
{
	if(stack == NULL)
	{
		return ;
	}
	stack->head.next = NULL;
	stack->size = 0;

}
//destroy
void FreeSpace_SeqStack(LinkStack* stack)
{
	if(stack == NULL)
	{
		return ;
	}
}


/*
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;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325972556&siteId=291194637