数据结构算法day40 -day50

【作业】
在这里插入图片描述

//度为1
int oneNodes(BiTree T){
	if(T==NULL){
		return 0;
	}else if(T->lchild!=NULL||T->rchild!=NULL){	//T结点为度为1的结点时
		return oneNodes(T->lchild)+oneNodes(T->rchild)+1;
	}else{	//当T结点为度为2和度为0的结点时
		return oneNodes(T->lchild)+oneNodes(T->rchild);
	}
	
}

//度为0
int zoreNodes(BiTree T){
	if(T==NULL){
		return 0;
	}else if(T->lchild!=NULL&&T->rchild!=NULL){	//T结点为度为1的结点时
		return oneNodes(b->lchild)+oneNodes(b->rchild);
	}else{	//当T结点为度为2和度为1的结点时
		return oneNodes(b->lchild)+oneNodes(b->rchild);
	}
	
}

// 计算叶子结点数
void caculateLeafNum(BinaryTree * root, int *leafNum)
{
	if(root ==NULL)
		return;
	if(root->pLeft ==NULL&&root->pRight ==NULL)
	{
		(*leafNum)++;
	}
	caculateLeafNum(root->pLeft,leafNum);
	caculateLeafNum(root->pRight,leafNum);
}

【day40】计算二叉树中所有结点的个数

//法一
int countNodes(BiTree T){
	if(T==NULL){
		return 0;
	}else{
		return countNodes(p->lchild)+countNodes(p->rchild)+1;
	}
}
//法二
int countNodes(BiTree T){
	int n1,n2; 
	if(T==NULL){
		return 0;
	}else{
		n1=countNodes(p->lchild);
		n2=countNodes(p->rchild);
		return n1+n2+1;
	}
}
//法三遍历法(先序)中序后序同理
void countNodes(BiTree T,int *n;){ 
	if(T!=NULL){
		(*n)++;
		countNodes(p->lchild,n);
		countNodes(p->rchild,n);
	}
}


【day41】在这里插入图片描述

算法思想:先序遍历二叉树,求第K个结点的值

void getValue(BiTree T,int *k){
	if(T!=NULL){
		(*k)--;			//计数器
		if(*k==0){		//判断是否已经遍历到第k个结点
			printf("%d\n",T->data);
			return T->data;
		}
		getValue(T->lchild,k);
		getValue(T->rchild,k);
	}
}

【day42】计算二叉树中所有叶子结点个数

递归法:F(N)=F(左)+F(右);
递归边界(两个):if(pNULL) return 0;
if(p->lchild
NULL&&p->rchild==NULL) reurn 1;

int count(BiTree T){
	int n1,n2;
	if(p==NULL){	//防止一开始传进来的树就是空树
		return 0;
	}
	if(p->lchild==NULL&&p->rchild==NULL){	//如果为叶子结点
		return 1;
	}else{	//非叶子结点(分支结点)
		n1=count(p->lchild);
		n2=count(p->rchild);
		return n1+n2;
	}
}

遍历法:每遍历到一个结点就判断它是否为叶子结点 是叶子结点则计数器加一

int n=0;
void count(BiTree T){
	if(T!=NULL){	//判断树是否为空
		//visit(T);
		if(T->lchild==NULL&&T->rchild==NULL){
			n++;
		}
		count(T->lchild);
		count(T->rchild);
	}
}

【day43】在这里插入图片描述

算法思想:先(中、后)序遍历二叉树,找到data域为key的结点,并让p指向该节点


void searchKey(BiNode *T,BiNode *p,int key){
	if(T!=NULL){
		//visit(T);
		if(T->data==key){
			p=T;
		}
		searchKey(T->lchild,p,key);
		searchKey(T->rchild,p,key);
	}
}

【day44】在这里插入图片描述

算法思想:利用中序遍历,遍历二叉树并将所有叶子结点通过其右孩子指针链接成一个单链表
叶子结点在三种遍历下的先后次序都是固定的

int tag=0;
void linkLeaves(BiNode *p,BiNode *head,BiNode *tail){
	if(p!=NULL){
		if(p->lchild==NULL&&p->rchild==NULL){
			if(head==NULL){
				head=p;
				tail=p;
			}else{
				tail->rchild=p;
				tail=p;
			}
		}
		linkLeaves(p->lchild,head,tail);
		linkLeaves(p->rchild,head,tail);
	}
}

【day45】在这里插入图片描述

void levelInoder(BiTree T,BiNode *s){
	int n=0;
	BiNode *p=T
	if(T!=NULL){
		while(p->data!=s->data){
			if(p->data<s->data){
				p=p->rchild;
			}else{
				p=p->lchild;
			}
			++n
		}
	}
}

【day46】在这里插入图片描述

int wpl=0;
int wpl_PreOrder(BiTree root,int deep){
	if(root->lchild==NULL&&root->rchild==NULL){	//如果为叶子结点,则累计wpl
		wpl=wpl+deep*root->weight;
	}
	if(root->lchild!=NULL){		//若左子树不空,则左子树递归遍历
		wpl_PreOrder(root->lchild,deep+1);
	}
	if(root->rchild!=NULL){		//若右子树不空,则右子树递归遍历
		wpl_PreOrder(root->rchild,deep+1);
	}
	return wpl;
}
int	WPL(BiTree root){
	return wpl_PreOrder(root,1);
}

【day47】在这里插入图片描述

算法思想:利用中序遍历的表达式树,1.根节点不加括号 2.遍历到叶子叶子结点不加括号,3.遍历到其他结点时,访问左子树前加左括号,访问完右子树之前加右括号

在这里插入图片描述

【day48】
在这里插入图片描述

算法思想:遍历整个二叉树,在遍历之前其孩子之前先判断该节点的值是否为X,如果是则输出该结点的层号;否则继续遍历其子树,层数计算,第一次经过某个结点之前层数加一,第三次经过该节点层数减一,以此来计算当前结点的层数;

int h=1;
int fun(BiTree p,int x){
	if(p!=NULL){
		if(p->data==x){
			printf("%d",h);
			return h;
		}
		h++;	//第一次进入一个结点 层数加一
		fun(p->lchild,x);//访问左子树
		fun(p->rchild,x);//访问右子树
		h--;	//第三次经过该节点退出时 层数减一
		
	}
}

【day49】
在这里插入图片描述

算法思想:先写一个计算单个结点到根节点的路径的功能函数,然后再遍历整个二叉树,每访问一个结点调用一次求路径函数

在这里插入图片描述
在这里插入图片描述
【day50】
在这里插入图片描述

int i=0,top=0;
ElemType stack[maxsize];
void path(BT Node*p){
	if(p!=NULL){
		stack[top]=p->data;
		++top;
		if(p->lchild==NULL&&p->rchild==NULL){
			for(i=0;i<top;i++){
				printf('%d',stack[i]);
			}
		}
		path(p->lchild);
		path(p->rchild);
		--top
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值