stackTraversalBinaryTree.c
binaryTreeLinkedlist.c
stack_linkedList.c
#include <stdio.h>
#include "binaryTreeLinkedlist.h"
#include "stack_linkedList.h"
/*
直接用堆栈遍历二叉树
先序,中序,后序
后序最特殊,重点介绍下:(此方式用一个栈)
1首先把根节点压入栈,
2之后把结点的右孩子结点,左孩子结点分别压入,再循环把左孩子的两个子结点压入。
3当遇到到一个结点:情况一:左右孩子结点都没有
情况二:之前出栈的结点是当前栈顶结点的孩子结点
则出栈并打印
4 然后,从步骤2开始重复执行
另外,也有一种通过建立访问次数的标志来做的后序遍历,此方法网上例子很多可以参考,不在此列出
*/
//先序
void preOrderTraversal(BinaryTree* binTree);
//中序
void inOrderTraversal(BinaryTree* binTree);
//后序
void postOrderTraversal(BinaryTree* binTree);
int main()
{
BinaryTree* binTree=createBinTree();
preOrderTraversal(binTree);
printf("\n");
inOrderTraversal(binTree);
printf("\n");
postOrderTraversal(binTree);
printf("\n");
return 0;
}
//先序
void preOrderTraversal(BinaryTree* binTree)
{
LinkedStack* myStack=CreateStack();
while(binTree || !isEmpty(myStack))
{
while(binTree)
{
printf("%s ",binTree->data);
Push(myStack,binTree);
binTree=binTree->left;
}
if(!isEmpty(myStack))
{
binTree=Pop(myStack);
binTree=binTree->right;
}
}
}
//中序
void inOrderTraversal(BinaryTree* binTree)
{
LinkedStack* myStack=CreateStack();
while(binTree || !isEmpty(myStack))
{
while(binTree)
{
Push(myStack,binTree);
binTree=binTree->left;
}
if(!isEmpty(myStack))
{
binTree=Pop(myStack);
printf("%s ",binTree->data);
binTree=binTree->right;
}
}
}
//后序
void postOrderTraversal(BinaryTree* binTree)
{
LinkedStack* myStack=CreateStack();
BinaryTree* lastPopedNode=NULL;
BinaryTree* topTreeNode=binTree;
if(binTree)
{
Push(myStack,binTree);
}
while(topTreeNode || !isEmpty(myStack))
{
while(topTreeNode)
{
if(topTreeNode->right!=NULL)
{
Push(myStack,topTreeNode->right);
}
if(topTreeNode->left)
{
Push(myStack,topTreeNode->left);
}
topTreeNode=topTreeNode->left;
}
while(top(myStack) && top(myStack)->left==NULL && top(myStack)->right==NULL)
{
printf("%s ",top(myStack)->data);
lastPopedNode=Pop(myStack);
topTreeNode=top(myStack);
}
while(lastPopedNode!=NULL && topTreeNode!=NULL && (lastPopedNode ==topTreeNode->left || lastPopedNode == topTreeNode->right))
{
printf("%s ",topTreeNode->data);
lastPopedNode=Pop(myStack);
topTreeNode=top(myStack);
}
}
}
binaryTreeLinkedlist.h
#ifndef binaryTreeLinkedlist_h
#define binaryTreeLinkedlist_h
/*
链表存储实现二叉树
*/
typedef char* NodeDataType;
typedef struct _BinaryNode{
NodeDataType data;
struct _BinaryNode* left;
struct _BinaryNode* right;
}BinaryTree;
typedef BinaryTree BinaryNode;
/*
函数声明
*/
BinaryNode* createBinNode(NodeDataType data);
BinaryTree* attachNode(BinaryNode* binNode1,BinaryNode* binNode2,int tag);
BinaryTree* createBinTree();
#endif
stack_linkedList.h
#ifndef stack_linkedList_h
#define stack_linkedList_h
#include "binaryTreeLinkedlist.h"
typedef BinaryNode* ElementType;
typedef struct _Node{
ElementType Data;
struct _Node* Next;
}LinkedStack;
LinkedStack* CreateStack();
void Push(LinkedStack* ls,ElementType item);
ElementType Pop(LinkedStack* ls);
int isEmpty(LinkedStack* ls);
ElementType top(LinkedStack* ls);
#endif
binaryTreeLinkedlist.c
#include <stdio.h>
#include <stdlib.h>
#include "binaryTreeLinkedlist.h"
BinaryNode* createBinNode(NodeDataType data)
{
BinaryNode* binNode=(BinaryNode*)malloc(sizeof(BinaryNode));
binNode->data=data;
binNode->left=NULL;
binNode->right=NULL;
return binNode;
}
BinaryTree* attachNode(BinaryNode* binNode1,BinaryNode* binNode2,int tag)
{
if(tag == -1)
{
binNode1->left=binNode2;
}else if(tag == 1)
{
binNode1->right=binNode2;
}
return binNode1;
}
BinaryTree* createBinTree()
{
BinaryNode* rootNode =createBinNode("A");
BinaryNode* nodeB =createBinNode("B");
BinaryNode* nodeC =createBinNode("C");
BinaryNode* nodeD =createBinNode("D");
BinaryNode* nodeE =createBinNode("E");
BinaryNode* nodeF =createBinNode("F");
BinaryNode* nodeG =createBinNode("G");
BinaryNode* nodeH =createBinNode("H");
BinaryNode* nodeI =createBinNode("I");
BinaryNode* nodeJ =createBinNode("J");
BinaryNode* nodeK =createBinNode("K");
attachNode(rootNode,nodeB,-1);
attachNode(rootNode,nodeC,1);
attachNode(nodeB,nodeD,-1);
attachNode(nodeB,nodeE,1);
attachNode(nodeC,nodeF,-1);
attachNode(nodeC,nodeG,1);
attachNode(nodeD,nodeH,-1);
attachNode(nodeD,nodeI,1);
attachNode(nodeE,nodeJ,-1);
attachNode(nodeE,nodeK,1);
return rootNode;
}
stack_linkedList.c
#include <stdio.h>
#include <stdlib.h>
#include "stack_linkedList.h"
/*
链式存储实现栈 使用头结点
*/
LinkedStack* CreateStack()
{
LinkedStack* ls=(LinkedStack*)malloc(sizeof(LinkedStack));
ls->Next=NULL;
return ls;
}
void Push(LinkedStack* ls,ElementType item)
{
LinkedStack* s=(LinkedStack*)malloc(sizeof(LinkedStack));
s->Data=item;
s->Next=ls->Next;
ls->Next=s;
}
ElementType Pop(LinkedStack* ls)
{
if(ls->Next==NULL)
{
printf("栈已空\n");
return NULL;
}else
{
LinkedStack* tmp=ls->Next;
ElementType data=tmp->Data;
ls->Next=tmp->Next;
free(tmp);
return data;
}
}
int isEmpty(LinkedStack* ls)
{
if(ls->Next==NULL)
{
return 1;
}else
{
return 0;
}
}
ElementType top(LinkedStack* ls)
{
ElementType element;
if(ls && ls->Next)
{
element=ls->Next->Data;
}else
{
element=NULL;
}
return element;
}