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

📄 ordertre.c

📁 在turbo C 环境下运行调试成功实现二叉排序树和排序树的查找
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>

using namespace std;

typedef struct node
{
  int data;
  struct node *lchild, *rchild;
}Bnode, *BTree;

void insert(BTree *root, BTree node)
{
   BTree *p;
   if(root==NULL) return;
   else p=root;

   if((*p)->data==node->data)
   { printf("insert the same value\n");
     getchar();
     return;
   }

   if(*p==NULL)
     *p=node;
   else if((*p)->data>node->data)
     insert(&(*p)->lchild,node);
   else if((*p)->data<node->data)
     insert(&(*p)->rchild,node);
}

BTree find(Bnode* p, int key)
{

   if(p->data==key) return p;

   else if(p->data==-1) return NULL;/*if there is no data find*/
   else if(p->data>key)
   find(p->lchild,key);

   else if(p->data<key)
   find(p->rchild,key);

}

void Inorder(BTree p)
{
  if(p)
  {
    Inorder(p->lchild);
    printf("%5d",p->data);
    Inorder(p->rchild);
  }
}

int main()
{

  BTree p,s;
  BTree q;
  int key;
  char ch;

  printf("\nPlease Create the Tree(End:-1)\n");
  scanf("%d",&key);
  p=NULL;

  while(key!=-1)
  {
    s = (BTree)malloc(sizeof(Bnode));
    s->data = key;
    s->lchild = s->rchild =NULL;
    insert(&p,s);
    scanf("%d",&key);
  }

  printf("\nTree is Created\n");
  Inorder(p);

  do
  {
    printf("\nPlease enter the key you find\n");
    scanf("%d",&key);
    q=find(p,key);
     if (q!=NULL) printf("success,the value is %4d ",q->data);
      else         printf("unsuccess");
       printf("\ncontinue?y:n\n");getchar();
	ch=getchar();
  }while(ch=='y'||ch=='Y');
  getchar();

  return 0;
}

⌨️ 快捷键说明

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