C语言 二叉查找树性质详解及实例代码

2020-01-06 17:10:15刘景俊

查找树的插入和删除

插入和删除不能破坏查找树的性质,因此只需要根据性质,在树中找到相应的位置就可以进行插入和删除操作。


struct list
{
  struct list *left;//左子树
  struct list *right;//右子树
  int a;//结点的值
};
void insert(struct list *root,struct list * k)
{
  struct list *y,*x;
  x=root;
  while(x!=NULL)
  {
    y=x;
    if(k->a<x->a)
    {
      x=x->left;
    }
    else
      x=x->right;
  }
  if(y==NULL)
    root=k;
  else if(k->a<y->a)
    y->left=k;
  else
    y->right=k;

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


注:相关教程知识阅读请移步到C++教程频道。