数据结构学习之二叉树

1树的存储
#define maxsize 100;
typedef char elemtype;
struct tnode
{elemtype data;
int parent;
};
struct tnode
{
	elemtype data;
	struct tnode parent;
}
struct tnode *p;//树的链式存储
2.二叉树的建立
#define NULL 0
typedef struct btnode
{
	char data;
	struct brnode *lchild,*rchild;
}st;
st *t;			//*t为指向二叉树结点类型的指针
char ch;
int node;
st *create(st *p)
{
	if((ch=getchar())=='#')
		p=NULL;
	else
	{
		p=(st*)malloc (sizeof(st));
		p->data=ch;
		p->lchild=create(p->lchild); 			//递归创建左结点
		p->rchild=create(p->rchild);			//递归创建右结点
		
	}
	return p;				//返回指向二叉树结点的指针
}
3.二叉树的先根遍历
void pretravel(struct btnode *bt)
{
	if(bt!=NULL)
	{
		printf("%2c",bt->data);
		pretravel(bt->lchild);  //递归调用左结点
		pretravel(bt->rchild);	//递归调用右结点
	}
	printf("\n");
}
②先根遍历的非递归实现
#define maxsize 20;
struct bnode
{
	elemtype data;
	struct bnode *lchild,rchild;
};//通过栈实现
struct stack
{
	int top;
	struct bnode *num[maxsize];
};
void pretravel(struct bnode *bt)
{
	struct stack p;
	struct bnode *q;
	q=bt;
	p.top = -1;
	while ((q!=NULL)||p.top!=-1))
		if(q!=NULL)
		{
			printf("%c",q->data);
			if(p.top<maxsize)
			{
				p.top++;
				p.num[p.top]=q;//将指根结点的指针入栈,遍历完左子树后在沿结点遍历右子树。
				q=q->lchild;
			}
			else 
				printf("stack overflow");
		}
		else if (p.top>-1)
		{
			q=p.num[p.top];
			/* 从堆栈中弹出只想上一次遍历过得结点的指针 */
			p.top--;
			q=q->rchild;
		}
}
4.二叉树的中根遍历LDR
递归实现
void intravel(struct btnode *bt)
{
	if(bt!=NULL)
	{
		intravel(bt->lchild);
		printf("%2c",p->data);
		intravel(bt->rchild);
	
	}
	printf("\n")
}
非递归实现
#define maxsize 20
struct bnode{
	datatype data;
	struct bnode *lchild ,rchild;
	
};
struct stack{
	int top;
	struct bnode *num[maxsize];
};
void intravel (struct bnode *bt)
{
	struct stack p;
	struct bnode *q;
	q= bt;
	p.top = -1;
	do{
		while(q!=NULL)
		{
			if(p.top<maxsize)
			{
				p.top++;
				p.num[p.top]=q;
				q=q->lchild;
			}
			else printf("stack overflow\n");
		}
		if(p.top>-1)
		{
			q=p.num[p.top];
			p.top--;
			printf("%c",q->data);
			q=q->rchild;
		}
		while ((p.top==-1)&&(q!=NULL))
	}
}
5.二叉树的后根遍历LRD
void lasttravel(struct btnode *bt)
{
	if(bt!=NULL)
	{
		lasttravel(bt->lchild);
		lasttravel(bt->rchild);
		printf("%2c" ,bt->data);
	}
	printf("\n");
}
非递归实现
#define maxsize 20
struct bnode {
	data;
	struct bnode *lchild,rchild;
	
};
struct stacknum
/* 定义一个指针栈 */
{
	int top1;
	struct bnode *next;
};
struct stack{
	int top;
	struct stacknum num[maxsize];
	
}
void lasttravel(struct bnode *bt)
{
	struct stack s;
	struct stacknum p;
	struct bnode *q;
	q=bt;
	s.top=-1;
	while((q!=NULL)||(s.top!=-1))
		if(q!=NULL)
		{
			p.top1=0;
			p.next=q;
			if(s.top<maxsize) //保留访问过结点的指针,并入栈
			{
				s.top++;
				s.num[s.top]=p;
				q=q->lchild;
			}
			else printf("str=ack overflow\n");
			
		}
		else if (s.top>-1)
		{
			p=s.num[s.top];
			s.top--;
			if(p.top1==0)
			{
				p.top1=1;
				s.top++;
				s.num[s.top]=p;
				q=q->rchild;
			}
			else{
				printf("%c",q->data);
				q=NULL;
			}
		}
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值