非递归先序创建二叉树、非递归先序遍历二叉树(C语言代码)

关于用非递归方法对二叉树进行操作的原理已经有很多介绍,不再赘述,只简单介绍一下代码的思路:

非递归先序创建二叉树的代码利用栈顶标记当前节点位,代码约定输入'#'时表示该位置没有输入,每次输入新元素都作两轮if判定(左右指示器方向,是否输入'#'),据此改变状态进行下一步的调整。

非递归先序遍历二叉树使用栈模拟递归。

#include
#include

typedef char TreeData
typedef struct BTreeNode{
    TreeData data;
    BTreeNode *lChild,*rChild;
}BTreeNode;
typedef BTreeNode* StackData;
typedef struct LinkStack{
    StackData data;
    LinkStack *link;
}LinkStack;

LinkStack* InitStack();
void Push(LinkStack* top, StackData x);
bool StackEmpty(LinkStack* top);
StackData Pop(LinkStack* top);
StackData GetTop(LinkStack* top);
void MakeEmpty(LinkStack* top);
BTreeNode* NonRecursiveCreate();
void NonRecursivePreOrder(BTreeNode* T);

int main()
{
	BTreeNode *T = NonRecursiveCreate();
	NonRecursivePreOrder(T);
    printf("\n");
    system("pause");
    return 0;
}

//STACK//

LinkStack* InitStack()
{
    LinkStack* top = (LinkStack*)malloc(sizeof(LinkStack));
    top->link = NULL;
    return top;
}

void Push(LinkStack* top, StackData x)
{
   	LinkStack *p;
   	p = (LinkStack*)malloc(sizeof(LinkStack));
    p->data = x;
    p->link = top->link;
    top->link = p;
}

bool StackEmpty(LinkStack* top)
{
    if(top->link == NULL)
		return true;
    else
		return false;
}

StackData Pop(LinkStack* top)
{
    StackData x;
	LinkStack *p;
    if(StackEmpty(top))
		return 0;
    p = top->link;
    top->link = p->link;
    x = p->data;
    free(p);
    return x;
}

StackData GetTop(LinkStack* top)
{
    StackData x;
    if(StackEmpty(top))
		return 0;
    x = top->link->data;
    return x;
}

void MakeEmpty(LinkStack* top)
{
    LinkStack *p=top->link, *q;
    while(p!=NULL)
	{
    	q = p;
        p = p->link;
        free(q);
    }
}

//-----//

BTreeNode* NonRecursiveCreate()
{
    LinkStack *S = InitStack();
    BTreeNode *T, *p;
    T = (BTreeNode*)malloc(sizeof(BTreeNode));
    T->data = '#';
    T->lChild = NULL;
    T->rChild = NULL;
    char ch;
    bool isLeft = true;
    printf("Please input an element:");
    scanf("%c", &ch);
    getchar();
    if(ch != '#')
    {
        T->data = ch;
        //T->lChild = NULL;
        //T->rChild = NULL;
        Push(S, T);
    }
    while(!StackEmpty(S))
    {
        printf("Please input an element:");
        scanf("%c",&ch);
        getchar();
        p = (BTreeNode*)malloc(sizeof(BTreeNode));
        p->data = ch;
        //p->lChild = NULL;
        //p->rChild = NULL;
        if(isLeft)
        {
            if(ch != '#')
            {
                GetTop(S)->lChild = p;
                Push(S,p);
            }
            else
            {
                GetTop(S)->lChild = NULL;
                isLeft = false;
            }
        }
        else
        {
            if(ch != '#')
            {
                GetTop(S)->rChild = p;
                isLeft = true;
            }
            else
            {
                GetTop(S)->rChild = NULL;
                Pop(S);
            }
        }
    }
    return T;
}

void NonRecursivePreOrder(BTreeNode* T)
{
    LinkStack* S = InitStack();
    while(T || !StackEmpty(S))
    {
        while(T)
        {
            printf("%c ",T->data);
            Push(S,T);
            T = T->lChild;
        }
        if(!StackEmpty(S))
        {
            T = Pop(S);
            T = T->rChild;
        }
    }
}

 

你可能感兴趣的:(数据结构与算法,数据结构,二叉树,c语言)