第五次上机

在这里插入图片描述

  • 上机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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值