004 -- Binary search Tree

000 - search tree 

#include <stdio.h>
#include <stdlib.h>

#define datatype int
typedef struct Node{
datatype data;
struct Node *lchild;
struct Node *rchild;
}bintree;

//create tree

bintree* createbinarytree(data)
{
bintree* t = (bintree*)malloc(sizeof(bintree));
if(!t)
{
printf("Not enough space\n");
return NULL;
  }
t->lchild = NULL;
t->rchild = NULL;
t->data = data;
return t;
}

bintree search_tree(bintree t, datatype x){
if(!t) {return NULL;} if(t->data == x) return t; else: { if(!search_tree(t->lchild,x)) {return search_tree(t->rchild,x);} return t; } }

 

002 --  count the nodes in the tree 

int count_tree(bintree t)
{
  if(t)
  {
     return (count_tree(t->lchild)+ count_tree(t->rchild)+1);
  }

  return 0;


}

 

003 -- compare 2 binary trees 

int is_equal(bintree t1, bintree t2)
{
  if(!t1 && !t2)
  { return 1 ;} // both tree is empty, equal 

  if(t1 && t2 && t1-->data == t2->data) 
//one tree is empty or there is one data is different , then break 
  {
     if(is_equal(t1->lchild,t2->lchild))
       if(is_equal(t1->rchild, t2->rchild))
          { return 1;}
  }

   return 0;


}

 

004 - degree check of the tree 

int hight_tree(bintree t)
{
  int h, left, right;
  if(!t)
  {
    return 0;
  }

  left = high_tree(t->lchild);
  right = high_tree(t->rchild);
  h = (left>right ? left: right) +1;

  return h;
}

 

005- find a element in the binary search tree 

bintree* Find(datatype x, bintree t)
{
  while(t)
  {
    if(x>t->data)
      t = t->rchild;
    else if(x < t->data)
      t = t->lchild;
    else
      return t;
  }

  return NULL:
}

 

006 - find the min data in the binary search tree

bintree* FindMin(bintree* t)
{
  if(t)
  {
    while(t->lchild)
      t = t->lchild;
  }
  return t;

}

// same for the max

bintree* FindMax(bintree* t)
{
if(t)
{
while(t->rchild)
t = t->rchild;
}
return t;

}

 

007 -- insert a node to the binary search tree 

bintree* Insert(bintree* t, datatype x)
{
  if(!t)
  {
    t = createbinarytree(x);
    t->lchild = NULL;
    t->rchild = NULL;
  }
  
  else
  {
    if(x>t->data)
      t ->rchild = Insert(x, t->rchild);
    else if 
      t->lchild = Insert(x, t->lchild);
  }

  return t;

}

 

008 -- delete a node 

BinaryTree* Delete(ElementType x, BinaryTree* BST) {  
    BinaryTree* Temp;  
    if (!BST)  
        printf("\ndidn't find the node to delete%d", x);  
    else if (x < BST->data)  
        BST->leftSubTree = Delete(x, BST->leftSubTree);  
    else if (x > BST->data)  
        BST->rightSubTree = Delete(x, BST->rightSubTree);  
    else { 
// find the node to be deleted
 
        if (BST->leftSubTree && BST->rightSubTree) { 
//the node has lchild and rchild
            Temp = FindMin(BST->rightSubTree); 
//find the min rchild to replace the deleted node
            BST->data = Temp->data;  
            BST->rightSubTree = Delete(BST->data, BST->rightSubTree);  //delete the rchild of the deleted node 
        }  
        else { //the deleted node has 1 or no child node 
            Temp = BST;  
            if (!BST->leftSubTree) //has rchild or no child 
                BST = BST->rightSubTree;  
            else if (!BST->rightSubTree) //has lchild or no child
                BST = BST->leftSubTree;  
            free(Temp);  
        }  
    }  
    return BST;  
}  
  

 

 

  
int main(int argc, const char * argv[]) {  
    bintree* t = createbinarytree(18);  
    Insert(10, t);  
    Insert(20, t);  
    Insert(7, t);  
    Insert(15, t);  
    Insert(9, t);  
    Insert(22, t);  
      
    bintree* findBT1 = Find(15, t);  
    if (findBT1) printf("find1:%d", findBT1->data);  
      
    bintree* findBT2 = Find(22, root);  
    if (findBT2) printf("\nfind2:%d", findBT2->data);  
      
    Delete(10, root);  
    bintree* findBT3 = Find(10, root);  
    if (findBT3) printf("\nfind3:%d", findBT3->data);  
      
    bintree* findBT4 = Find(9, root);  
    if (findBT4) printf("\nfind4:%d", findBT4->data);  
      
    Delete(9, root);  
    bintree* findBT5 = Find(9, root);  
    if (findBT5) printf("\find5:%d", findBT5->data);  
      
    return 0;  
}  

 

 

More details:

http://www.cnblogs.com/elaron/archive/2013/04/11/3015155.html

 

http://www.cnblogs.com/fu11211129/p/4214047.html

转载于:https://www.cnblogs.com/Shareishappy/p/7718859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园悬赏任务平台对字典管理、论坛管理、任务资讯任务资讯公告管理、接取用户管理、任务管理、任务咨询管理、任务收藏管理、任务评价管理、任务订单管理、发布用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行校园悬赏任务平台程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。校园悬赏任务平台的开发让用户查看任务信息变得容易,让管理员高效管理任务信息。 校园悬赏任务平台具有管理员角色,用户角色,这几个操作权限。 校园悬赏任务平台针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理任务信息,管理任务资讯公告信息等内容。 校园悬赏任务平台针对用户设置的功能有:查看并修改个人信息,查看任务信息,查看任务资讯公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看任务,删除任务操作,新增任务操作,修改任务操作。任务资讯公告信息管理页面提供的功能操作有:新增任务资讯公告,修改任务资讯公告,删除任务资讯公告操作。任务资讯公告类型管理页面显示所有任务资讯公告类型,在此页面既可以让管理员添加新的任务资讯公告信息类型,也能对已有的任务资讯公告类型信息执行编辑更新,失效的任务资讯公告类型信息也能让管理员快速删除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值