22计算机408考研—数据结构—二叉树的插入和删除

118 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第22天,点击查看活动详情

手把手教学考研大纲范围内树定义,遍历,Huffman,并查集 22考研大纲数据结构要求的是C/C++,笔者以前使用的都是Java,对于C++还很欠缺, 如有什么建议或者不足欢迎大佬评论区或者私信指出 初心是用最简单的语言描述数据结构

Talk is cheap. Show me the code. 理论到处都有,代码加例题自己练习才能真的学会

二叉树的基本操作详细解释

每个结点有两个子结点的树

在这里插入图片描述

二叉树的插入

以下代码的插入,进入插入方法,从根结点开始,由用户选择左子树和右子树,一直到空结点的位置,根据提示确定插入


插入代码:

  //插入结点
bool treeInsert(Tree &tree, int data) {
    if (tree == NULL) {     //如果根结点为空,把插入的值插入到根结点
        tree = new TreeNode;    //添加结点,设置左子结点和右子结点为空
        tree->data = data;
        tree->leftchild = NULL;
        tree->rightchild = NULL;
        cout << "根节为点空, 插入成功\n";
        return true;
    }
    Tree temp = tree;
    while (temp) {
        cout << "输入 1 选择左子结点,";
        cout << "输入 2 选择右子结点,";
        cout << "输入 3 返回上一结点,输入 其他 退出插入操作\n";
        int selected;
        cin >> selected;
        if (selected == 1) {
            if (temp->leftchild == NULL) {  //如果左子结点为空,将插入的值放到左子结点
                temp->leftchild = new TreeNode;
                temp->leftchild->data = data;
                temp->leftchild->leftchild = NULL;
                temp->leftchild->rightchild = NULL;
                cout << "插入成功\n";
                return true;
            } else {    //如果左子结点不为空,找到左子结点,继续循环
                temp = temp->leftchild;
            }
        } else if (selected == 2) {
            if (temp->rightchild == NULL) { //右子结点为空,将值插入到右子结点
                temp->rightchild = new TreeNode;
                temp->rightchild->data = data;
                temp->rightchild->leftchild = NULL;
                temp->rightchild->rightchild = NULL;
                cout << "插入成功\n";
                return true;
            } else {    //右子结点不为空,指向右子结点
                temp = temp->rightchild;
            }
        } else {    //输入其他,自己退出插入操作
            return false;
        }
    }
}

二叉树的删除

和插入差不多的,从根结点开始,用户选择左子树,右子树,或者删除当初结点
删除当前结点,当前结点的子结点会被全部删除
-
顺序存储的是用BFS方法把子树全部删除
链式存储把删除结点的父结点对当前结点的方向的子树附空


删除代码:

 //删除子树
bool treeDelete(Tree &tree) {
    if (tree == NULL) {     //第一次进来tree为根结点
        cout << "根节点为空,无法删除\n";
        return false;
    }
    Tree temp = tree;
    Tree fathertemp;    //记录一下父结点,删除时,把删除结点的父结点的左或者右结点附空
    while (true) {
        if (temp->leftchild != NULL) {  //删除左子结点或者右子结点的时候判断一下是否为空
            cout << "输入 1 选择左子结点,";
        }
        if (temp->rightchild != NULL) {
            cout << "输入 2 选择右子结点,";
        }
        cout << "输入 3 删除此结点,输入 其他 退出删除操作\n";
        int selected;
        cin >> selected;
        if (selected == 1 && temp->leftchild != NULL) { //选择左子结点或者右子结点的时候需要判断左子结点或者右子结点是否为空
            fathertemp = temp;  //转向左子结点或者右子结点的时候,记录一下父结点
            temp = temp->leftchild;
        } else if (selected == 2 && temp->leftchild != NULL) {
            fathertemp = temp;
            temp = temp->rightchild;
        } else if (selected == 3) {
            if (fathertemp->leftchild == temp) {    //删除结点如果是父结点的左结点,就把父结点的左结点附空
                fathertemp->leftchild = NULL;
            }
            if (fathertemp->rightchild == temp) {   //右结点同理
                fathertemp->rightchild = NULL;
            }
            Tree t = temp;  //保存一下待删除结点,把待删除结点delete,释放空间
            temp = NULL;//直接指向NULL为什么不能删除????????????

            delete t;
            return true;
        } else {
            return false;   //输入其他,退出删除操作
        }

    }
}