二叉树的非递归遍历算法(先序,中序,后序)

头文件–辅助数据结构—栈

#ifndef __STACK_H__
#define  __STACK_H__
#include<stdio.h>
#define MaxSize 50
#define FALSE 0
#define TRUE 1

typedef struct btnode {
    
    
	char element;
	struct btnode* Lchild, * Rchild;
}BTNode;
typedef struct btree {
    
    
	struct btnode* Root;
}BTree;
typedef struct stack {
    
    
	int top, maxstack;
	BTNode* element[MaxSize];
}Stack;
void CreateStack(Stack* s, int m)
{
    
    
	s->top = -1;
	s->maxstack = m;
}
int isEmpty(Stack s) {
    
    
	return s.top < 0;
}
int isFull(Stack s) {
    
    
	return s.top >= s.maxstack - 1;
}
void Push(Stack* s, BTNode* x) {
    
    
	if (isFull(*s)) {
    
    
		printf("OVERFLOW\n");
	}
	else {
    
    
		s->element[++s->top] = x;
	}
}
void Pop(Stack* s) {
    
    
	if (s->top < 0) {
    
    
		printf("UNDERFLOW\n");
	}
	else {
    
    
		s->top--;
	}
}
BTNode* StackTop(Stack s) {
    
    
	if (s.top < 0) {
    
    
		printf("UNDERFLOW\n");
		return  NULL;
	}
	else {
    
    
		return s.element[s.top];
	}
}
#endif // !1

源文件—包含组建二叉树–以及对应的非递归遍历算法
还有返回二叉树深度,结点数量,叶子数量,按层次遍历二叉树的算法。

#include<stdio.h>
#include<stdlib.h>
#include"stack.h"

//创建新节点
BTNode* NewNode() {
    
    
	BTNode* p = (BTNode*)malloc(sizeof(BTNode));
	return p;
}
//创建二叉树
void CreateBT(BTree* bt) {
    
    
	bt->Root = NULL;
}
//将两个二叉树组建为一个二叉树
void MakeBT(BTree* bt, char x, BTree* lt, BTree* rt) {
    
    
	BTNode* p = NewNode();
	p->element = x;
	p->Rchild = rt->Root;
	p->Lchild = lt->Root;
	lt->Root = rt->Root = NULL;
	bt->Root = p;
}
//将二叉树拆分为两个二叉树
void BreakBT(BTree* bt, BTree* lt, BTree* rt) {
    
    
	BTNode* p = bt->Root;
	if (p) {
    
    
		lt->Root = p->Lchild;
		rt->Root = p->Rchild;
		bt->Root = NULL;
		free(p);
	}
}
int Size(BTNode* p) {
    
    
	int s, s1, s2;
	if (!p) {
    
    
		return 0;
	}
	else {
    
    
		s1 = Size(p->Lchild);
		s2 = Size(p->Rchild);
		s = s1 + s2 + 1;
		return s;
	}
}
//求二叉树的结点数量
int SizeofBT(BTree bt) {
    
    
	return Size(bt.Root);
}
int Depth(BTNode* p) {
    
    
	if (!p) {
    
    
		return 0;
	}
	else {
    
    
		return 1 + max(Depth(p->Lchild), Depth(p->Rchild));
	}
}
//求二叉树的高度
int DepthofBT(BTree bt) {
    
    
	return Depth(bt.Root);
}
int CountLeaf(BTNode* p, int count)
{
    
    
	if (p != NULL)
	{
    
    
		if (p->Lchild == NULL && p->Rchild == NULL) {
    
    
			count++;
		}
		count = CountLeaf(p->Lchild, count);
		count = CountLeaf(p->Rchild, count);
	}
	return count;
}
int NumberofLeaf(BTree bt, int count) {
    
    
	return CountLeaf(bt.Root, count);
}
void LevelOrd(BTree bt) {
    
    
	BTNode* p;
	BTNode* queue[256];
	int front = -1, rear = -1;
	p = bt.Root;
	rear++;
	queue[rear] = p;
	while (front != rear) {
    
    
		front++;
		p = queue[front];
		printf("%c ", p->element);
		if (p->Lchild != NULL) {
    
    
			rear++;
			queue[rear] = p->Lchild;
		}
		if (p->Rchild != NULL) {
    
    
			rear++;
			queue[rear] = p->Rchild;
		}
	}
	printf("\n");
}
void Visit(BTNode* p) {
    
    
	printf("%c ", p->element);
}
//先序遍历
void IPreOrder(BTree* bt, void  (*Visit)(BTNode* u)) {
    
       //Visit 是函数指针
	Stack s;
	BTNode* p = bt->Root;
	CreateStack(&s, MaxSize);
	Push(&s, NULL);
	while (p) {
    
    
		(*Visit)(p);
		if (p->Rchild) {
    
    
			Push(&s, p->Rchild);
		}
		if (p->Lchild) {
    
    
			p = p->Lchild;
		}
		else {
    
    
			p=StackTop(s);
			Pop(&s);
		}
	}
}
//中序遍历
void IInOrder(BTree* bt, void (*Visit)(BTNode* u)) {
    
    
	Stack s;
	BTNode* p = bt->Root;
	CreateStack(&s, MaxSize);
	while (p || !isEmpty(s)) {
    
    
		if (!p) {
    
    
			p=StackTop(s);
			Pop(&s);
			(*Visit)(p);
			p = p->Rchild;
		}
		else {
    
    
			Push(&s, p);
			p = p->Lchild;
		}
	}
}
//非递归后序遍历
void IPostOrder(BTree* bt, void (*Visit)(BTNode* u)) {
    
    
	BTNode* p = bt->Root;
	Stack s;
	int tag[MaxSize];
	CreateStack(&s, MaxSize);

	while (p || !isEmpty(s)) {
    
    

		while (p != NULL)
		{
    
    
			Push(&s, p);
			tag[s.top] = 0;
			p = p->Lchild;
		}
		while (!isEmpty(s) && tag[s.top] == 1)
		{
    
    
			p = StackTop(s);
			Pop(&s);
			(*Visit)(p);
		}
		if (!isEmpty(s))
		{
    
    
			tag[s.top] = 1;   //设置标记右子树已经访问 
			p = StackTop(s);
			p = p->Rchild;
		}
		else
		{
    
    
			break;
		}
	}
}
void main() {
    
    
	BTree* a, * x, * y, * z, * s, * t;

	a = (BTree*)malloc(sizeof(BTree));
	x = (BTree*)malloc(sizeof(BTree));
	y = (BTree*)malloc(sizeof(BTree));
	z = (BTree*)malloc(sizeof(BTree));
	s = (BTree*)malloc(sizeof(BTree));
	t = (BTree*)malloc(sizeof(BTree));
	CreateBT(a);
	CreateBT(x);
	CreateBT(y);
	CreateBT(z);
	CreateBT(s);
	CreateBT(t);
	MakeBT(s, 'E', a, a);
	MakeBT(t, 'F', a, a);
	MakeBT(y, 'C', s, t);
	MakeBT(t, 'D', a, a);
	MakeBT(x, 'B', a, t);
	MakeBT(z, 'A', x, y);

	printf("\n");
	printf("the total node number is: %d\n", SizeofBT(*z));
	printf("the tree depth is: %d\n", DepthofBT(*z));
	printf("the number of leaves is: %d\n", NumberofLeaf(*z, 0));
	printf("按层次遍历二叉树:\n");
	LevelOrd(*z);
	IPreOrder(z, Visit);
	printf("\n");
	IInOrder(z, Visit);
	printf("\n");
	IPostOrder(z, Visit);
	printf("\n");
	printf("hhhhhhhhhhh\n");
}

猜你喜欢

转载自blog.csdn.net/m0_47472749/article/details/121619206