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

📄 erchashuchazhao.cpp

📁 关于数据结构的各章节的c原代码实现
💻 CPP
字号:
// erchashuchazhao.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define  MAX 10
struct node
{
	int data;
	struct node* lchild;
	struct node* rchild;
};
typedef struct node tnode;
typedef tnode* link;
link createtree(int data[],int n)
{
   link root=NULL;
   for (int i=0;i<n;i++)
   {
	   link newnode,currentnode,parentnode;
	   newnode=(link)malloc(sizeof(tnode));
	   newnode->data=data[i];
	   newnode->lchild=NULL;
	   newnode->rchild=NULL;
	   currentnode=root;
	   if(root==NULL)root=newnode;
	   else
	   {
    
		while (currentnode!=NULL)
		{       parentnode=currentnode;
			if(newnode->data<currentnode->data)
				currentnode=currentnode->lchild;
			else
				currentnode=currentnode->rchild;
		}
		if (newnode->data<parentnode->data)
			parentnode->lchild=newnode;
		else
			parentnode->rchild=newnode;
	   }
   }
return root;
}
link search(link head,int e)
{
  link p;
  p=head;
  while (p!=NULL)
  {
	  if(p->data=e)return p;
	  else if (p->data<e)p=p->rchild;
	  else p=p->lchild;
	  
  }
  return 0;
}

int main(int argc, char* argv[])
{   
	link head;
    int data[MAX];
	int e;
	link ptr;
	printf("\n输入树的数据:");
	for (int i=0;i<MAX;i++)
	{
		scanf("%d",&data[i]);
	}
    head=createtree(data,MAX);
	printf("input the data you want to find:");
    scanf("%d",&e);
	ptr=search(head,e);
	if(ptr)printf("the founded value is %d\n",ptr->data);
	else printf("not found!\n");
	return 0;
}

⌨️ 快捷键说明

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