第五次上机

在这里插入图片描述

  • 上机5.1
  • 顺一遍:
/*2.4最大堆
 *
 * void BuildHeap();	//2.4-a 最大堆构建
 * void SiftDown(int left);	//2.4-b SiftDown函数从left向下调整堆,使序列成为堆
 * void SiftUp(int pos);	//2.4-c SiftUp函数从position向上调整堆,使序列成为堆
 * bool Remove(int pos, T& node);	//2.4-d 删除给定下标的元素
 * bool Insert(const T& newNode);	//2.4-e 从堆中插入新元素newNode
 *
 */

#include <iostream>
#include <stdlib.h>
using namespace std;

template <class T>
class MaxHeap
{
private:
    T *heapArray;    // 存放堆数据的数组
    int CurrentSize; /* 当前堆元素个数 */
    int MaxSize;     /* 堆中能存放的最大元素个数 */
public:
    MaxHeap(T *array, int num, int max)
    {
        this->heapArray = array;
        this->CurrentSize = num;
        this->MaxSize = max;
    }

    virtual ~MaxHeap(){};          //析构函数
    bool isLeaf(int pos) const;    //如果是叶结点,返回true
    int leftchild(int pos) const;  //返回左孩子位置
    int rightchild(int pos) const; //返回右孩子位置
    int parent(int pos) const;     //返回父结点位置
    void BuildHeap();              /* 2.4-a 最大堆构建 */
    void SiftDown(int left);       /* 2.4-b SiftDown函数从left向下调整堆,使序列成为堆(构建用) */
    void SiftUp(int pos);          /* 2.4-c SiftUp函数从position向上调整堆,使序列成为堆 */
    bool Remove(int pos, T &node); /* 2.4-d 删除给定下标的元素 */
    bool Insert(const T &newNode); /* 2.4-e 从堆中插入新元素newNode */
    T &RemoveMax();                /* 2.4-f 从堆顶删除最大值 */
    void visit();
};

/*
 * TODO:2.4-a 最大堆构建
 */
template <class T>
void MaxHeap<T>::BuildHeap()
{
    for (int i = CurrentSize / 2 - 1; i >= 0; i--)
        SiftDown(i);
}

template <class T>
bool MaxHeap<T>::isLeaf(int pos) const
{
    if (pos >= CurrentSize)
    {
        cout << "越界" << endl;
        return (false);
    }
    else if (pos > (CurrentSize - 1) / 2)
        return (true);
    else
        return (false);
}

template <class T>
int MaxHeap<T>::leftchild(int pos) const
{
    return (2 * pos + 1);
}

template <class T>
int MaxHeap<T>::rightchild(int pos) const
{
    return (2 * pos + 2);
}

template <class T>
int MaxHeap<T>::parent(int pos) const
{
    return ((pos - 1) / 2);
}

/*
 * TODO:2.4-d 删除给定下标的元素,并将该元素的值赋值给node变量。
 * 返回值说明:如果删除成功,则返回true,否则返回false
 * 重要说明:如果当前堆为空,则输出打印cout << "空堆" << endl;并返回false
 */
template <class T>
bool MaxHeap<T>::Remove(int pos, T &node)
{
    if (pos >= 0 && pos <= MaxSize - 1)
    {
        node = heapArray[pos];
        heapArray[pos] = heapArray[CurrentSize - 1];
        SiftDown(pos);
        CurrentSize--;
        return true;
    }
    else
    {
        cout << "空堆" << endl;
        return false;
    }
}

/*
 * TODO:2.4 - b SiftDown函数从left向下调整堆,使序列成为堆
 */
template <class T>
void MaxHeap<T>::SiftDown(int left)
{
    int parent = left;
    int child = 2 * parent + 1;
    T temp = heapArray[parent];
    while (child < CurrentSize)
    {
        if (child < CurrentSize - 1 && heapArray[child] < heapArray[child + 1])
        {
            child++;
        }
        if (temp < heapArray[child])
        {
            heapArray[parent] = heapArray[child];
            parent = child;
            child = 2 * child + 1;
        }
        else
        {
            break;
        }
    }
    heapArray[parent] = temp;
}

/*
 * TODO:2.4-c SiftUp函数从pos向上调整堆,使序列成为堆
 */
template <class T>
void MaxHeap<T>::SiftUp(int pos)
{
    int child = pos;
    int parent = (child - 1) / 2;
    T temp = heapArray[child];
    while (parent >= 0 && child >= 1)
    {
        if (temp > heapArray[parent])
        {
            heapArray[child] = heapArray[parent];
            child = parent;
            parent = (child - 1) / 2;
        }
        else
        {
            break;
        }
    }
    heapArray[child] = temp;
}

/*
 * TODO:2.4-e 从堆中插入新元素newNode, 如果插入成功,返回true,否则返回false。
 * 重要说明:如果堆中元素超过堆中元素最大个数值,则输出打印cout << "堆满" << endl;并返回false
 */
template <class T>
bool MaxHeap<T>::Insert(const T &newNode)
{
    CurrentSize++;
    if (CurrentSize > MaxSize)
    {
        cout << "堆满" << endl;
        return false;
    }
    else
    {
        heapArray[CurrentSize - 1] = newNode;
        SiftUp(CurrentSize - 1);
        return true;
    }
}

template <class T>
void MaxHeap<T>::visit()
{
    for (int i = 0; i < CurrentSize; i++)
        cout << heapArray[i] << " ";
    cout << endl;
}

/*
 * TODO:2.4-f 从堆顶删除最大值. 如果堆栈为空堆,则输出打印cout << "空堆" << endl;然后退出程序,退出码为1.
 * 否则,从堆顶删除最大值,并将其作为返回值进行返回。
 */
template <class T>
T &MaxHeap<T>::RemoveMax()
{
    if (CurrentSize <= 0)
    {
        cout << "空堆" << endl;
        exit;
    }
    else
    {
        T node;
        Remove(0, node);
        return node;
    }
}
/*
10 20
20  12   35   15   10   80   30    17    2   1
6  3   7
*/
int main()
{
    /* int a[10] = { 20,12,35,15,10,80,30,17,2,1 }; */
    int count, maxSize; /* 初始化堆中元素个数 */
    cin >> count >> maxSize;
    int iValue;
    int *a = new int[count];
    for (int i = 0; i < count; i++)
    {
        cin >> iValue;
        a[i] = iValue;
    }

    /* MaxHeap<int> maxheap(a, 10, 20); */
    MaxHeap<int> maxheap(a, count, maxSize);
    int temp;
    maxheap.BuildHeap();
    cout << "构建堆后为:";
    maxheap.visit();
    cin >> iValue; /* 输入一个整数,判断它是否是堆上的叶子节点 */
    if (maxheap.isLeaf(iValue))
        cout << "位置" << iValue << "是叶结点" << endl;
    else
        cout << "位置" << iValue << "不是叶结点" << endl;
    maxheap.visit();
    maxheap.RemoveMax();
    cout << "移除最大值后:";
    maxheap.visit();
    cin >> iValue; /* 输入一个整数,移除该下标所在的元素 */
    maxheap.Remove(iValue, temp);
    cout << "删除下标为" << iValue << "的元素之后为:";
    maxheap.visit();
    cout << "删除下标为" << iValue << "的元素为" << temp << endl;
    cin >> iValue; /* 输入一个整数,移除该下标所在的元素 */
    maxheap.Insert(iValue);
    cout << "插入" << iValue << "后为:";
    maxheap.visit();
    system("pause");
    return (0);
}
  • 上机5.2
  • 顺一遍:
/*2.5 课后习题

a.	设一棵二叉搜索树以二叉链表表示,试编写有关二叉树的递归算法
   int degree1(binarytreenode* p)							//2.5-a-i 统计度为1的结点
   int degree2(binarytreenode* p)							//2.5-a-ii 统计二叉树中度为2的结点个数
   int degree3(binarytreenode* p)							//2.5-a-iii 统计二叉树中度为0的结点个数
   int get_height(binarytreenode* p)						//2.5-a-iv 统计二叉树的高度
   void get_width(binarytreenode* p, int i, int wide[])	//2.5-a-v 统计各层结点数
   int get_max(binarytreenode* p)							//2.5-a-vi 计算二叉树中各结点中的最大元素的值
   void change_children(binarytreenode* p)					//2.5-a-vii 交换每个结点的左子树和右子树?
   void del_leaf(binarytreenode* p)						//2.5-a-viii 从二叉树中删去所有叶结点
   bool wanquan(binarytreenode* pRoot)						//2.5-b 编写算法判定给定二叉树是否为完全二叉树
*/
#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
class binarytreenode //二叉树结点
{
   int data;

public:
   binarytreenode *leftchild;
   binarytreenode *rightchild;
   binarytreenode(int &d) //构造函数
   {
       data = d;
       leftchild = NULL;
       rightchild = NULL;
   }
   int &get_data() { return data; }       //获取结点值data
   void change_data(int &n) { data = n; } //改变结点值data
   ~binarytreenode(){};                   //析构函数
};

//二叉树
class binarytree
{
   binarytreenode *root;
   static int times;
   int size;

public:
   binarytree()
   {
       size = 0;
       root = NULL;
   }
   int get_size() { return size; }             //获取结点数
   binarytreenode *get_root() { return root; } //获取二叉树根结点
   void creat()                                //创建二叉树
   {
       binarytreenode *prev;
       cout << "输入int型数据:(以0结束)";
       int temp;
       cin >> temp;
       while (temp != 0)
       {
           if (root == NULL)
           {
               root = new binarytreenode(temp);
               size++;
               prev = root;
           }
           else
           {
               prev = root;
               size++;
               for (;;)
               {
                   if (temp < prev->get_data())
                   {
                       if (prev->leftchild == NULL)
                       {
                           prev->leftchild = new binarytreenode(temp);
                           break;
                       }
                       prev = prev->leftchild;
                   }
                   else
                   {
                       if (prev->rightchild == NULL)
                       {
                           prev->rightchild = new binarytreenode(temp);
                           break;
                       }
                       prev = prev->rightchild;
                   }
               }
           }
           cin >> temp;
       }
   }

   void levelorder() //广度优先遍历
   {
       binarytreenode *p = get_root();
       queue<binarytreenode *> que;
       cout << "广度遍历结果: ";
       if (p != NULL)
           que.push(p);
       else
           cout << "二叉树为空!";
       while (!que.empty())
       {
           if (p != NULL)
           {
               cout << p->get_data() << " ";
               if (p->leftchild != NULL)
                   que.push(p->leftchild);
               if (p->rightchild != NULL)
                   que.push(p->rightchild);
               que.pop();
               if (!que.empty())
                   p = que.front();
           }
       }
       cout << endl;
   }
   /*
   TODO:统计并返回度为1的结点个数
    */
   int degree1(binarytreenode *p)
   {
       int count_1 = 0;
       binarytreenode *stack[100];
       int top = 0;
       while (p || top != 0)
       {
           while (p)
           {
               stack[top] = p;
               top++;
               if ((p->leftchild == NULL && p->rightchild != NULL) || (p->rightchild == NULL && p->leftchild != NULL))
               {
                   count_1++;
               }
               p = p->leftchild;
           }
           if (top)
           {
               p = stack[--top];
               p = p->rightchild;
           }
       }
       return count_1;
   }
   /*
   TODO:统计并返回度为2的结点个数
    */
   int degree2(binarytreenode *p)
   {
       int count_2 = 0;
       binarytreenode *stack[100];
       int top = 0;
       while (p || top != 0)
       {
           while (p)
           {
               stack[top] = p;
               top++;
               if ((p->leftchild != NULL && p->rightchild != NULL))
               {
                   count_2++;
               }
               p = p->leftchild;
           }
           if (top)
           {
               p = stack[--top];
               p = p->rightchild;
           }
       }
       return count_2;
   }
   /*
   TODO:统计并返回度为0的结点个数
    */
   int degree0(binarytreenode *p)
   {
       int count_0 = 0;
       binarytreenode *stack[100];
       int top = 0;
       while (p || top != 0)
       {
           while (p)
           {
               stack[top] = p;
               top++;
               if (p->leftchild == NULL && p->rightchild == NULL)
               {
                   count_0++;
               }
               p = p->leftchild;
           }
           if (top)
           {
               p = stack[--top];
               p = p->rightchild;
           }
       }
       return count_0;
   }
   /*
   TODO:统计并返回高度
    */
   int get_height(binarytreenode *p)
   {
       if (p == NULL)
       {
           return 0;
       }
       int count_h = 1;
       // binarytreenode *stack[100];
       // int top = 0;
       if (p->leftchild == NULL && p->rightchild == NULL)
       {
           count_h = 1;
       }
       else
       {
           count_h = count_h + ((get_height(p->leftchild) < get_height(p->rightchild)) ? get_height(p->rightchild) : get_height(p->leftchild));
       }
       return count_h;
   }
   void get_width(binarytreenode *p, int i, int wide[]) //统计各层结点数
   {
       wide[i++]++;
       if (p->leftchild != NULL)
           get_width(p->leftchild, i, wide);
       if (p->rightchild != NULL)
           get_width(p->rightchild, i, wide);
   }
   /*
   TODO:统计并返回二叉树的宽度
    */
   int get_max_width()
   {
       int height = get_height(root);
       int wide[height];
       for (int i = 0; i < height; i++) //把每层宽度都初始化为0
       {
           wide[i] = 0;
       }
       get_width(root, 0, wide);
       int max = wide[0];
       for (int i = 0; i < height; i++)
       {
           if (wide[i] > max)
           {
               max = wide[i];
           }
       }
       return max;
   }

   /*
   TODO: 计算并返回二叉树的最大值
    */
   int get_max(binarytreenode *p)
   {
       int max = p->get_data();
       if (p == NULL)
       {
           max = 0;
       }
       while (p->rightchild != NULL)
       {
           p = p->rightchild;
       }
       max = p->get_data();
       return max;
   }
   /*
   TODO:交换二叉树的左右孩子
    */
   void change_children(binarytreenode *p)
   {
       if (p->leftchild != NULL)
           change_children(p->leftchild);
       if (p->rightchild != NULL)
           change_children(p->rightchild);
       binarytreenode *temp;
       temp = p->leftchild;
       p->leftchild = p->rightchild;
       p->rightchild = temp;
   }
   int find_father(binarytreenode *num, binarytreenode *&fa) //寻找父节点
   {
       binarytreenode *p = root;
       int flag = 0;
       while (p != NULL)
       {
           if (p->get_data() > num->get_data())
           {
               fa = p;
               flag = 1;
               p = p->leftchild;
           }
           else if (p->get_data() <= num->get_data() && p != num)
           {
               fa = p;
               flag = 2;
               p = p->rightchild;
           }
           else
           {
               break;
           }
       }
       return flag;
   }
   /*
   TODO:删除叶节点,删除成功的时候输出打印cout << xxx << "删除成功!" << endl;其中xxx为叶子节点的值
    */
   int stackDel[100];
   int top = 0;
   void del_leaf(binarytreenode *p)
   {
       binarytreenode *ptr = p;
       if (ptr->leftchild != NULL)
       {
           if (ptr->leftchild->leftchild == NULL && ptr->leftchild->rightchild == NULL)
           {
               // cout << ptr->leftchild->get_data() << "删除成功!" << endl;
               stackDel[top++] = ptr->leftchild->get_data();
               delete ptr->leftchild;
               ptr->leftchild = NULL;
           }
           else
           {
               del_leaf(ptr->leftchild);
           }
       }
       if (ptr->rightchild != NULL)
       {
           if (ptr->rightchild->leftchild == NULL && ptr->rightchild->rightchild == NULL)
           {
               // cout << ptr->rightchild->get_data() << "删除成功!" << endl;
               stackDel[top++] = ptr->rightchild->get_data();
               delete ptr->rightchild;
               ptr->rightchild = NULL;
           }
           else
           {
               del_leaf(ptr->rightchild);
           }
       }
   }

   bool leaf(binarytreenode *pCur)
   {
       if (pCur->leftchild == NULL && pCur->rightchild == NULL)
           return true;
       return false;
   }

   /*
TODO:判断是否是完全二叉树,是则返回true,否则返回false。
*/
   bool wanquan(binarytreenode *pRoot) //是否完全二叉树
   {
       int height = get_height(root);
       int wide[height];
       for (int i = 0; i < height; i++) //把每层宽度都初始化为0
       {
           wide[i] = 0;
       }
       get_width(root, 0, wide);
       for (int i = 0; i < height - 1; i++)
       {
           if (wide[i] != pow(2, i))
               return false;
       }
       return true;
   }
};
int binarytree::times = 0;
int main()
{
   binarytree tree;
   tree.creat();
   tree.levelorder();
   cout << "度为1的结点个数: " << tree.degree1(tree.get_root()) << endl;
   cout << "度为2的结点个数: " << tree.degree2(tree.get_root()) << endl;
   cout << "度为0的结点个数: " << tree.degree0(tree.get_root()) << endl;
   cout << "二叉树高度: " << tree.get_height(tree.get_root()) << endl;
   cout << "二叉树宽度: " << tree.get_max_width() << endl;
   cout << "最大值:" << tree.get_max(tree.get_root()) << endl;
   cout << "删除叶节点:" << endl;
   tree.del_leaf(tree.get_root());
   for (tree.top = tree.top - 1; tree.top >= 0; tree.top--)
   {
       cout << tree.stackDel[tree.top] << "删除成功!" << endl;
   }
   tree.levelorder();
   cout << "交换左右孩子. . ." << endl;
   tree.change_children(tree.get_root());
   tree.levelorder();
   if (tree.wanquan(tree.get_root()))
       cout << "该二叉树是完全二叉树" << endl;
   else
       cout << "该二叉树不是完全二叉树" << endl;
   system("pause");
   return 0;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值