二叉树的遍历(283)

源代码:

//程序目的:练习二叉树的遍历 

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

typedef int DT;

//定义结点类型
typedef struct node {						 
	DT data;
	struct node *left, *right; 
} Node;

//创建结点 
Node* Creat_Node (Node *np, DT v) {
	np = (Node*)malloc(sizeof(Node));
	np->data = v;
	np->left = np->right = NULL;
	
	return np;
}

//定义根结点结构体 
typedef struct bt {
	Node *root; 
} BT;

//初始化
void Init (BT *t) {
	t->root = NULL;
	return;
}

//将数据插入树 子函数 
void Insert_Help (Node **np, DT v) {
	if (*np == NULL)
	    *np = Creat_Node (*np, v);
	else if (v < (*np)->data)
	    Insert_Help (&((*np)->left), v);
	else
	    Insert_Help (&((*np)->right), v);
	    
	    return;
}

//将数据插入树 主函数
void Insert (BT *t, DT v) {
	Insert_Help (&(t->root), v);
	
	return;
} 

//清空树
void Clear_Help(  Node* np)
{
	if(np==NULL) return;

	Clear_Help( np->left);

	Clear_Help( np->right);

	free(np);

}

void Clear(  BT* t)
{
	Clear_Help(t->root);
}

//递归先序遍历 子函数 
void Pre_Help (Node *np) {
	if (np == NULL)
        return;
	printf("%d ", np->data);
	Pre_Help (np->left);
	Pre_Help (np->right);
	
	return;
}

//递归先序遍历 主函数
void Pre (BT *t) {
	Pre_Help (t->root);
	
	return;
} 

//递归中序遍历  子函数 
void In_Help (Node *np) {
	if (np == NULL)
	    return;
	In_Help (np->left);
	printf("%d ", np->data);
	In_Help (np->right);
	
	return;
}

//递归中序遍历 主函数
void In (BT *t) {
	In_Help (t->root);
	
	return;
}

//递归后序遍历 子函数
void Post_Help (Node *np) {
	if (np == NULL)
	    return;
	Post_Help (np->left);
	Post_Help (np->right);
	printf("%d ", np->data);
	
	return;
} 

//递归后序遍历 主函数
void Post (BT *t) {
	Post_Help (t->root);
	
	return;
}

//定义栈
typedef struct snode {
	Node *np;
	struct snode *next;
} Stack;

//置空栈
Stack* Init_Stack (Stack *top) {
	top = NULL;
	
	return top;
}

//判空栈
int is_empty (Stack *top) {
	if (top == NULL)
	    return 1;
	else
	    return 0;
} 

//取栈顶
Node* Top (Stack *top) {
	Node *p;
	p = top->np;
	
	return p;
} 

//入栈
Stack* Push (Stack *top, Node *n) {
	Stack *p = (Stack*)malloc(sizeof(Stack));
	p->np = n;
	p->next = top;
	top = p;
	
	return top;
} 

//出栈
Stack* Pop (Stack *top) {
	Stack *p;
	p = top;
	top = top->next;
	free(p);
	
	return top;
} 

//非递归先序遍历
void Pre1 (BT *t) {
	if (t->root == NULL)
	    return;
	Stack top, *ls = ⊤
	
	Node *p = t->root;
	
	ls = Init_Stack (ls);
	
	while (!is_empty(ls) || p != NULL) {
		if (p) {
			printf("%d ", p->data);
			ls = Push (ls, p);
			p = p->left;
		}
		else if (!is_empty(ls)) {
			p = Top(ls);
			ls = Pop (ls);
			p = p->right;
			
		}
	}
} 

//非递归中序
void In1 (BT *t) {
	if (t->root == NULL)
	    return;
	Stack top, *ls = ⊤
	
	Node *p = t->root;
	
	ls = Init_Stack (ls);
	
	while (!is_empty(ls) || p != NULL) {
		if (p) {
			ls = Push (ls, p);
			p = p->left;
		}
		else if (!is_empty(ls)) {
			p = Top(ls);
			printf("%d ", p->data);
			ls = Pop (ls);
			p = p->right;
			
		}
	}
}

//非递归后序
void Post1 (BT *t) {
	if (t->root == NULL)
        return;
	Stack top, *ls = ⊤
	Node *p = t->root;
	Node *last = p;
	
	ls = Init_Stack (ls);
	ls = Push (ls, p);
	
	while (!is_empty(ls)) {
		p = Top (ls);
		
		if ((p->left == NULL && p->right == NULL) || (p->right == last) || (p->right == NULL && last == p->left)) {
			printf("%d ", p->data);
			last = p;
			ls = Pop (ls);
			
		}
		else {
			if (p->right != NULL) 
			    ls = Push (ls, p->right);
			if (p->left != NULL) 
			    ls = Push (ls, p->left);
		}
	}
} 




int main() {
	BT t;
	Init (&t);
	Insert(&t,5);
	Insert(&t,6);
	Insert(&t,1);
	Insert(&t,4);
	Insert(&t,3);
	Insert(&t,2);
	Insert(&t,7);
	Insert(&t,8);
	Insert(&t,0);
	printf("\n递归先序:");
	Pre (&t);
	printf("\n非递归先序:");
	Pre1 (&t);
	printf("\n递归中序:");
	In (&t);
	printf("\n非递归中序:");
	In1 (&t);
    printf("\n递归后序:");
	Post (&t);
	printf("\n非递归后序:");
    Post1 (&t);
    Clear (&t);
    
	return 0;
} 

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值