⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8-10.cpp

📁 大学计算机专业课程中数据结构各章的算法设计
💻 CPP
字号:
#include "Tree1.h"
TreeNode1* insert(char ch,TreeNode1 *p)
{
	if(p==NULL)
		p=new TreeNode1(ch);
	else
	{
		if(ch<=p->data)
			p->left=insert(ch,p->left);
			else
			p->right=insert(ch,p->right);
	}
	return p;
}
void create(Tree1 &t1,char str[])
{
	 t1.root=NULL;
     int i=0;
     cout<<"建立二叉排序树: ";
     while(str[i]!='\0')
	 {
	     cout<<str[i]<<" ";
	     t1.root=insert(str[i],t1.root);
	     i++;
	 }
	 cout<<"\n";
}
TreeNode1* search(Tree1 &t1,char ch)
{
	TreeNode1 *p=t1.root,*find=NULL;
	cout<<"Search "<<ch<<": ";
	while(p!=NULL&&find==NULL)
	{
		cout<<p->data<<" ";
		if(ch==p->data)
			find=p;
		else
			if(ch<p->data)
				p=p->left;
			else
				p=p->right;
	}
	return find;
}
void main()
{
	char *str="3258793146";
	Tree1 t1;
	create(t1,str);
	t1.inorder();
	TreeNode1 *find=search(t1,'1');
	if(find!=NULL)
		cout<<"find"<<find->data<<"\n";
	else
		cout<<"not found!\n";
	char ch='0';
	find=search(t1,ch);
	if(find!=NULL)
		cout<<"find"<<find->data<<"\n";
	else
		cout<<"not found!\n";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -