10个数据结构课程设计例子(C语言完整源码)二叉排序树、快速排序、冒泡排序、二叉树非递归遍历(2)

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

1、查找.c

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
我的信息: \*
编程ID
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include <stdio.h>
void search(int a[],int n,int k)
{
	int i=n;
	a[0]=k;
	while(a[i]!=k)
		i--;
	if(i==0)
		printf("查找失败!\n");
	else 
		printf("数据是数组中的第%d个数\n",i);
}
void binary\_search(int a[],int n,int k)
{
	int low,mid,high;
	int time=0;
	low=0;
	high=n-1;
	while(low<=high)
	{
		mid=(low+high)/2;
		time=time+1;
		if(a[mid]==k)
		{
			printf("数据是数组中的第%d个数\n",mid);
			printf("查找次数为%d次\n",time);
			break;
		}
		else if(a[mid]<k)
			low=mid+1;
		else 
			high=mid-1;
	}
}
void main()
{
	int a[100];
	int i,key,n;
	printf("the length of number:");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	printf("输入要查找的数据:");
	scanf("%d",&key);
	binary\_search(a,n,key); 
	search(a,n,key);  
}

2、二叉排序树.c

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
编程ID
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 500
typedef struct Bnode
{
	int key;
	struct Bnode \*left;
	struct Bnode \*right;
}Bnode;
Bnode \*btlnsert(int x,Bnode \*root)
//root为二叉排排序树的根指针,x为新节点的关键字值
{
	Bnode \*p,\*q;
	int flag=0;//是否完成插入的标志
	p=(Bnode \*)malloc(sizeof(Bnode));
	p->key=x;//为新节点关键字赋值
	p->right=NULL;//新节点要作为叶子结点插入
	p->left=NULL;
	if(root==NULL)
	{
		root=p;
		return p;
	}
	q=root;
	while(flag==0)//标志完成插入
	{
		if(q->key>x)
		{
			if(q->left!=NULL)
				q=q->left;
			else
			{
				q->left=p;//在左子数插入
				flag=1;
			}
		}
		else
		{
			if(q->right!=NULL)
				q=q->right;
			else
			{
				q->right=p;//在右子树插入
				flag=1;
			}
		}
	}
	return root;
}
void Inorder(struct Bnode \*BD)
{
	if(BD!=NULL)
	{
		Inorder(BD->left);
		printf("%5d", BD->key);
		Inorder(BD->right);
	}

}
void main()
{
	int i,length;
	int a[MAX];
	Bnode \*root=NULL;
	printf("输入数组大小:");
	scanf("%d",&length);
	for(i=0;i<length;i++)
	{
		scanf("%d",&a[i]);
		root=btlnsert(a[i],root);
	}
	printf("输出所给排序为:\n");
	Inorder(root);
}


3、二叉树层次遍历.c

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
我的信息: \*
编程ID
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MS 50
struct BTreeNode
{
	char date;
	struct BTreeNode \*lchild;
	struct BTreeNode \*rchild;
};
typedef struct BTreeNode TNODE;
TNODE \*creat(int n)
{
	int i,j;
	char x;
	TNODE \*narr[100],\*p,\*t;
	for(j=1;j<=n;j++)
	{
		printf("input i,x:\n");
		scanf("%d,%c",&i,&x);
		p=(TNODE\*)malloc(sizeof(TNODE));
		p->date=x;
		p->lchild=NULL;
		p->rchild=NULL;
		narr[i]=p;
		if(i==1)
			t=p;
		else
		{
			if(i%2==0)
				narr[i/2]->lchild=p;
			else 
				narr[i/2]->rchild=p;
		}
	}
	return(t);
}
void Inorder(struct BTreeNode \*BT)
{
	if(BT!=NULL)
	{	
		Inorder(BT->lchild);
		printf("%5c", BT->date);
		Inorder(BT->rchild);
	}

}
void levelerder(struct BTreeNode \*BT )
{
	struct BTreeNode \*q[MS];
	int front=0,rear=0;
	if(BT!=NULL)
	{
		rear=(rear+1)%MS;
		q[rear]=BT;
	}
	while(front!=rear)
	{
		struct BTreeNode \*p;
		front=(front+1)%MS;
		p=q[front];
		printf("%5c",p->date);
		if(p->lchild!=NULL)
		{
			rear=(rear+1)%MS;
			q[rear]=p->lchild;
		}
		if(p->rchild!=NULL)
		{
			rear=(rear+1)%MS;
			q[rear]=p->rchild;
		}
	}
}
void main()
{
	TNODE \*t;
	int a;
	printf("input the number of BTreeNode\n");
	scanf("%d",&a);
	t=creat(a);
	Inorder(t);
	levelerder(t);
}

4、二叉树非递归遍历.c

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
我的信息: \*
编程ID
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MS 50
struct BTreeNode
{
	char date;
	struct BTreeNode \*lchild;
	struct BTreeNode \*rchild;
};
typedef struct BTreeNode TNODE;
TNODE \*creat(int n)
{
	int i,j;
	char x;
	TNODE \*narr[100],\*p,\*t;
	for(j=1;j<=n;j++)
	{
		printf("input i,x:\n");
		scanf("%d,%c",&i,&x);
		p=(TNODE\*)malloc(sizeof(TNODE));
		p->date=x;
		p->lchild=NULL;
		p->rchild=NULL;
		narr[i]=p;
		if(i==1)
			t=p;
		else
		{
			if(i%2==0)
				narr[i/2]->lchild=p;
			else 
				narr[i/2]->rchild=p;
		}
	}
	return(t);
}
void Preorder(struct BTreeNode \*BT)
{
	if(BT!=NULL)
	{	
		printf("%5c", BT->date);
		Preorder(BT->lchild);
		Preorder(BT->rchild);
	}

}
void Inorder(struct BTreeNode \*BT)
{
	if(BT!=NULL)
	{	
		Inorder(BT->lchild);
		printf("%5c", BT->date);
		Inorder(BT->rchild);
	}

}
void Postorder(struct BTreeNode \*BT)
{
	if(BT!=NULL)
	{	
		Postorder(BT->lchild);
		Postorder(BT->rchild);
		printf("%5c", BT->date);
	}

}
void PreorderN(struct BTreeNode \*BT)
{
	struct BTreeNode \*s[20];
	int top=-1;
	struct BTreeNode \*p=BT;
	while(top!=-1||p!=NULL)
	{
		while(p!=NULL)
		{
			top++;
			s[top]=p;
			printf("%5c",p->date);
			p=p->lchild;
		}
		if(top!=-1)
		{
			p=s[top];
			top--;
			p=p->rchild;
		}
	}
}
void InorderN(struct BTreeNode \*BT)
{
	struct BTreeNode \*s[20];
	int top=-1;
	struct BTreeNode \*p=BT;
	while(top!=-1||p!=NULL)
	{
		while(p!=NULL)
		{
			top++;
			s[top]=p;
			p=p->lchild;
			
		}
		if(top!=-1)
		{
			p=s[top];
			top--;
			printf("%5c",p->date);
			p=p->rchild;
		}
	}
}
void PostorderN(struct BTreeNode \*BT)
{
	struct BTreeNode \*s[20];
	int top=-1,flag=1;
	struct BTreeNode \*p=BT,\*q;
	do{
    	while(p!=NULL)
			{
				top++;
				s[top]=p;
			    p=p->lchild;
			}

		q=NULL;
		flag=1;
			while(top!=-1&&flag)
			{
				p=s[top];	
				if(p->rchild==q)
				{
					printf("%5c",p->date);	          
					top--;
					q=p;
				}
				else
				{
				    p=p->rchild;	
					flag=0;
				}	
			}
	}while(top!=-1);
}
void main()
{
	TNODE \*t;
	int a;
	printf("input the number of BTreeNode\n");
	scanf("%d",&a);
	t=creat(a);
	printf("中序遍历:");
	Inorder(t);
	printf("\n");
	InorderN(t);
    printf("\n");
	printf("先序遍历:");
	Preorder(t);
	printf("\n");
	PreorderN(t);
	printf("\n");
	printf("后序遍历:");
	Postorder(t);
	printf("\n");
	PostorderN(t);
	printf("\n");
}

5、二叉树建立.c

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
我的信息: \*
编程ID
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
void preorder1(bitree \*root)
{
	bitree \*p,\*s[100];
	int top=0;
	p=root;
	while((p!=NULL)||(top>0))
	{
		while(p!=NULL)
			(cout<<p->data<<" ";
		s[++top]=p;//
		p=p->lchild;
	}
	p=s[top--];
	p=p->rchild;
}
}
void inorder1(bitree \*root)
{
	bitree \*p,\*s[100];
	int top=0;
	p=root;
while((p!=NULL)||(top>0))
{
	while(p!=NULL)
	{
		s[++top]=p;p=p->lchild;
	}
	{
		p=s[top--];
		cout<<p->data<<"";
		p=p->rchild;
	}
}
}
void postorder1(bitree \*root)
{
	bitree \*p,\*s1[100];
	ints2[100],top=0,b;
	p=root;
	do
	{
		while(p!=NULL)
		{
			s1[top]=p;s2[top++]=0;
			p=p->lchild;
		}
		if(top>0)
		{
			b=s2[--top];
			p=s1[top];
			if(b==0)
			{
				s1[top]=p;
				s2[top++]=1;
				p=p->rchild;
			}
			else
			{
				cout<<p->data<<" ";
				p=NULL;
			}
		}
	}while(top>0);
}
void main()
{
	bitree \*root;
	root=create();
	cout<<"先序遍历的结果"<<endl;
	preorder1(root);cout<<end1;
	cout<<"中序遍历的结果"<<end1;
	inorder1(root);cout<<end1;


![img](https://img-blog.csdnimg.cn/img_convert/28396043f4d5f57ebd4831b3e6b5f6d1.png)
![img](https://img-blog.csdnimg.cn/img_convert/f81501253241dda4e60d57743e6ca1bb.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

 ";
				p=NULL;
			}
		}
	}while(top>0);
}
void main()
{
	bitree \*root;
	root=create();
	cout<<"先序遍历的结果"<<endl;
	preorder1(root);cout<<end1;
	cout<<"中序遍历的结果"<<end1;
	inorder1(root);cout<<end1;


[外链图片转存中...(img-71vnvrIU-1715499117880)]
[外链图片转存中...(img-vPYM1zyh-1715499117880)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来简单介绍一下二排序的概念和实现思路。 二排序又称为二搜索,是一种特殊的二叉树,它满足以下条件: 1. 对于任意结点,它的左子中的所有结点的值都小于它的值; 2. 对于任意结点,它的右子中的所有结点的值都大于它的值; 3. 左右子都是二排序。 根据这个定义,我们可以用二链表来实现一棵二排序。具体实现思路如下: 1. 定义二叉树的结点类型,包括结点的值、左右子结点指针; 2. 定义一个函数,用于向二排序中插入一个结点; 3. 读入一组序列,依次插入到二排序中; 4. 中序遍整棵二排序,输出结点的值。 下面是具体的代码实现,你可以参考一下: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树结点类型 typedef struct node { int value; struct node* left; struct node* right; } Node; // 向二排序中插入一个结点 Node* insert(Node* root, int value) { if (root == NULL) { // 如果当前结点为空,就创建一个新的结点 Node* node = (Node*)malloc(sizeof(Node)); node->value = value; node->left = NULL; node->right = NULL; return node; } else if (value < root->value) { // 如果插入的值小于当前结点的值,就插入到左子中 root->left = insert(root->left, value); } else { // 如果插入的值大于等于当前结点的值,就插入到右子中 root->right = insert(root->right, value); } return root; } // 中序遍排序 void inorder(Node* root) { if (root == NULL) { return; } inorder(root->left); printf("%d ", root->value); inorder(root->right); } int main() { // 读入一组序列,依次插入到二排序中 int n, value; scanf("%d", &n); Node* root = NULL; for (int i = 0; i < n; i++) { scanf("%d", &value); root = insert(root, value); } // 中序遍整棵二排序,输出结点的值 inorder(root); return 0; } ``` 这样就完成了二排序的建立和中序遍。当然,这只是一个简单的实现,如果你想进一步了解二排序的相关知识,可以参考一些经典的算法教材。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值