观看记录
  • 我的观影记录
登录
测试首页二叉搜索树使用c++语言如何实现删除节点的操作

二叉搜索树使用c++语言如何实现删除节点的操作

在之前的经验傍边,我已经介绍了二叉搜刮树的插入与搜刮的操作。因为删除节点的操作,比拟较于搜刮与插入的操作,略显复杂,需要针对节点的子节点个数进行会商。是以,本章特意会商了二叉搜刮树的删除操作。

工具/原料

  • code::blocks
  • c++ 11编译器

方式/步骤

  1. 1

    第一步简单介绍一下什么是二叉搜刮树(Binary Search Tree)。二叉搜刮树是二叉树的一种,一个节点最多只有两个子节点,可是节点的左子节点的值要小于节点的值,节点右子节点的值要年夜于节点的值。

  2. 2

    删除操作需要针对子节点个数进行会商。

    1、若是一个节点的子节点个数为0,就可以直接删除这个节点

  3. 3

    2、若是这个节点的子节点个数是一个,那么就需要再删除该节点之后,将该节点的子节点和父节点毗连到一路。

  4. 4

    3、若是该节点的子节点个数为两个,那么这个环境比力复杂。这个时辰需要在该节点的右子树中找到最小的节点来替代该节点。这一步的操作需要经由过程递归来实现。具体代码看下一步。

  5. 5

    光说不做不可,这一步我们将展示上述三步的具体代码实现过程。下图所供给的代码是一个类的方式。仅供参考。

  6. 6

    为了整个法式的完整性,这一步调,我们供给整个二叉搜刮树的实现代码,包罗搜刮、插入、删除、寻找最年夜最小值。如下:

    #include <iostream>

    using namespace std;

    //tree node

    struct node

         {

          int key;

          struct node *left,*right;

         };

    //construct new node

    struct node* newnode(int item)

    {

          struct node* temp=new node;

          temp->key=item;

          temp->left=nullptr;

          temp->right=nullptr;

          return temp;

    };

    //inorder travel

    void inorder(struct node* root)

    {

          if (root!=nullptr)

          {

                inorder(root->left);

                cout<<root->key<<endl;

                inorder(root->right);

          }

    }

    class BinarySearchTree

    {

    public:

          BinarySearchTree(){root=nullptr;};

          //find the minimum value

          struct node *findmin(struct node*t)

          {

                if (t==nullptr)

                      return nullptr;

                if (t->left==nullptr)

                      return t;

                else

                      findmin(t->left);

          };

          //find a maximum value

          struct node*findmax(struct node*t)

          {

                if (t==nullptr)

                      return nullptr;

                if (t->right==nullptr)

                      return t;

                else

                      findmax(t->right);

          };

          //if a node in Binary search tree

          bool contains(struct node* t,int item)

          {

                if (t==nullptr)

                      return false;

                else if (item>t->key)

                      contains(t->right,item);

                else if (item<t->key)

                      contains(t->left,item);

                else

                      return true;

          }

          //delete a node

          struct node* deleteNode(struct node* t,int item)

          {

                      if (t==nullptr)

                            return t;

                      if (item<t->key)

                            t->left=deleteNode(t->left,item);

                      else if (item>t->key)

                            t->right=deleteNode(t->right,item);

                      else

                      {

                            if (t->left==nullptr)

                            {

                                  struct node *temp=t->right;

                                  delete t;

                                  return temp;

                            }

                            else if (t->right==nullptr)

                            {

                                  struct node*temp=t->left;delete t;

                                  return temp;

                            }

                            struct node* temp=findmin(t->right);

                            t->key=temp->key;

                            t->right=deleteNode(t->right,temp->key);

                      }

                      return t;

          };

          //insert a node

          struct node* insert(struct node* t,int item)

          {

                 if (t==nullptr&&root==nullptr)

                 {

                      root=newnode(item);

                      return root;

                 }

                 if (t==nullptr &&root!=nullptr)

                      return newnode(item);

                 if (item<t->key)

                      t->left=insert(t->left,item);

                 if (item>t->key)

                      t->right=insert(t->right,item);

                root=t;

                return root;

          }

          struct node* root;

    };

    int main()

    {

          BinarySearchTree tr;

          tr.insert(tr.root,60);

          tr.insert(tr.root,10);

          tr.insert(tr.root,20);

          tr.insert(tr.root,30);

          tr.insert(tr.root,500);

          tr.insert(tr.root,40);

          cout<<"inorder travel"<<endl;

          inorder(tr.root);

          cout<<"if contains 10:"<<endl;

          cout<<tr.contains(tr.root,10)<<endl;

          cout<<"findmin  "<<tr.findmin(tr.root)->key<<endl;

          cout<<"delete 40"<<endl;

          tr.deleteNode(tr.root,40);

          inorder(tr.root);

          return 0;

    }

注重事项

  • 若是有帮忙请点个赞或投个票吧,感激您!!

“二叉搜索树使用c++语言如何实现删除节点的操作”关联的文章

  • 如何关闭皮皮搞笑精彩内容消息通知

    皮皮搞笑是一款手机搞笑社区App,让用户笑到没心没肺,又忍不住感动流泪的温暖家园,那么如何关闭皮皮搞笑精彩内容消息通知以满足不同用户的需求呢?

    31分钟前0阅读

    如何关闭皮皮搞笑精彩内容消息通知
  • win7系统找不到宽带连接怎么办

    现如今很多用户都喜欢使用win7系统,而在使用win7系统的过程中做的最多的就是上网了。Win7系统上网离不开宽带连接,如果win7宽带连接找不到了,应该怎么办呢?下面就让小编为大家带来win7系统找不到宽带连接解决方法

    31分钟前0阅读

    win7系统找不到宽带连接怎么办
  • 新版QQ音乐怎么关闭底部的直播导航

    新版QQ音乐怎么关闭底部的直播导航?下面请大家随小编一起来看看操作的方法吧。

    31分钟前0阅读

    新版QQ音乐怎么关闭底部的直播导航
  • 怎样查询高速实时路况?

    要出行怎么查询高速实时路况?我们用地图就可以了,在地图上就可以看到实际的路况的,下面详细来看下。

    31分钟前0阅读

  • 六芒星手势密码教程

    31分钟前0阅读

    六芒星手势密码教程
  • 教师讲课过程评价标准

    教师是太阳底下最光辉的职业,但是成为教师之路也是要经历重重考验的,下面给大家说说教师讲课过程评价标准

    31分钟前0阅读

  • 酚醛铝箔夹芯板

    酚醛铝箔夹芯板是由酚醛泡沫与两层亚光铝箔经过特殊工艺复合而成。外膜材料为经过高温固化的高分子膜,可有效的防止紫外线及气体腐蚀,并与铝箔结合牢固,又能与酚醛泡沫形成聚合物,从而保证象圆酚醛铝箔夹芯板的质量稳定。

    31分钟前0阅读

  • Xperia XZ2 Premium配置如何

    Xperia XZ2 Premium是索尼在4月16日悄悄发布的新机,而且没进行预热,下面来简单了解一下配置。

    31分钟前0阅读

  • PLSQL破解,无需注册码和破解工具

    PL/SQL Developer过期了,又没有注册码,又不想花钱买,而且事情又非常急,这时候怎么办?不要着急,请随小编一起解决这种情况吧。

    31分钟前0阅读

  • Win11按capslock切换不了大小写怎么解决

    有朋友不知道在哪里设置,下面小编就给大家分享详细的设置方法,有需要帮助的朋友可以参考下这篇经验,希望能对大家有所帮助。

    1小时前0阅读

    Win11按capslock切换不了大小写怎么解决
切换深色外观
留言
视频编辑修改
回到顶部
首页
手机数码
医疗健康
金融管理
社交情感
无名