首页 > 其他分享 >山东大学数据结构实验9 二叉树操作

山东大学数据结构实验9 二叉树操作

时间:2023-04-26 12:55:57浏览次数:32  
标签:rightChild NULL return int binaryTreeNode leftChild 二叉树 数据结构 山东大学

描述

创建二叉树类。二叉树的存储结构使用链表。提供操作:前序遍历、中序遍历、后序遍历、层次遍历、计算二叉树结点数目、计算二叉树高度。

格式

输入格式

第一行为一个数字n (10<=n<=100000),表示有这棵树有n个节点,编号为1~n。
之后n行每行两个数字,第 i 行的两个数字a、b表示编号为 i 的节点的左孩子节点为 a,右孩子节点为 b,-1表示该位置没有节点。
保证数据有效,根节点为1。

输出格式

第一行,n个数字,表示该树的层次遍历。
第二行,n个数字,第i个数字表示以 i 节点为根的子树的节点数目。
第三行,n个数字,第i个数字表示以 i 节点为根的子树的高度。

样例1

输入

5
2 3
4 5
-1 -1
-1 -1
-1 -1

输出

1 2 3 4 5
5 3 1 1 1
3 2 1 1 1

样例2

输入

5
3 2
-1 -1
4 5
-1 -1
-1 -1

输出

1 3 2 4 5
5 1 3 1 1
3 1 2 1 1

样例3

输入

10
2 -1
4 3
6 -1
5 8
9 7
-1 -1
-1 -1
-1 -1
10 -1
-1 -1

输出

1 2 4 3 5 8 6 9 7 10 
10 9 2 6 4 1 1 1 2 1 
6 5 2 4 3 1 1 1 2 1 

Hint

请仔细读题,注意建树过程。

#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>

using namespace std;


const int N = 1e5 + 10;



template<class T>
struct binaryTreeNode {
    T element;
    binaryTreeNode<T> *leftChild, *rightChild; //指向右孩子节点的指针

// 3 个构造函数
    binaryTreeNode() // 没有参数
    { leftChild = rightChild = NULL; }

    binaryTreeNode(const T &theElement) //只有数据参数
    {
        element = theElement;
        leftChild = rightChild = NULL;
    }

    binaryTreeNode(const T &theElement, // 数据 + 指针参数
                   binaryTreeNode *theLeftChild,
                   binaryTreeNode *theRightChild) {
        element = theElement;
        leftChild = theLeftChild;
        rightChild = theRightChild;

    }
};

template<class T>
class linkedBinaryTree {
public:
    linkedBinaryTree() {
        root = NULL;
        treeSize = 0;
    }// 构造函数
    // ~linkedBinaryTree(){erase();}; // 析构函数
    bool empty() const { return treeSize == 0; } // 判断是否为空
    int size() const { return treeSize; } // 返回节点的数量
    void levelOrder(binaryTreeNode<T> *t); // 层次遍历
    void preOrder(binaryTreeNode<T> *t); // 前序遍历
    void inOrder(binaryTreeNode<T> *t); // 中序遍历
    void postOrder(binaryTreeNode<T> *t); // 后序遍历
    int height(binaryTreeNode<T> *t);

    int number(binaryTreeNode<T> *t) {
        int x = 0;
        if (t != NULL) {
            x = number(t->leftChild) + number(t->rightChild) + 1;
        }
        return x;
    }

private:
    binaryTreeNode<T> *root; // 指向根的指针
    int treeSize; // 树的节点个数
};

template<class T>
void linkedBinaryTree<T>::inOrder(binaryTreeNode<T> *t) {
    if (t == NULL)return;
    inOrder(t->leftChild);
    cout << t->element << endl;
    inOrder(t->rightChild);

}

template<class T>
void linkedBinaryTree<T>::preOrder(binaryTreeNode<T> *t) {
    if (t == NULL)return;
    cout << t->element << endl;
    preOrder(t->leftChild);
    preOrder(t->rightChild);

}

template<class T>
void linkedBinaryTree<T>::postOrder(binaryTreeNode<T> *t) {
    if (t == NULL)return;
    postOrder(t->leftChild);
    postOrder(t->rightChild);
    cout << t->element << endl;
}

template<class T>
int linkedBinaryTree<T>::height(binaryTreeNode<T> *t) {
    if (t == NULL)return 0;
    return max(height(t->rightChild), height(t->leftChild)) + 1;
}

template<class T>
void linkedBinaryTree<T>::levelOrder(binaryTreeNode<T> *t) {
    queue<binaryTreeNode<T> *> q;
    while (t != NULL) {
        cout << t->element << " ";
        if (t->leftChild != NULL)
            q.push(t->leftChild);
        if (t->rightChild != NULL)
            q.push(t->rightChild);
        if (q.empty()) { break; }
        t = q.front();
        q.pop();
    }
    cout << endl;
}
binaryTreeNode<int>* b[N];

int main() {

   int n;cin>>n;

    for (int i = 1; i <= n; i++) { b[i] = new binaryTreeNode<int>(i); }

    for (int i = 1; i <= n; i++) {
        int x, y = 0;
        cin >> x >> y;
        if (x != -1)b[i]->leftChild = b[x];
        else b[i]->leftChild = NULL;
        if (y != -1)b[i]->rightChild = b[y];
        else b[i]->rightChild = NULL;
    }
    linkedBinaryTree<int> a;
    a.levelOrder(b[1]);
    for (int i = 1; i <= n; i++) {
        cout << a.number(b[i]) << " ";

    }
    cout << endl;
    for (int i = 1; i <= n; i++) {
        cout << a.height(b[i]) << " ";

    }

    return 0;
}

描述

接收二叉树前序序列和中序序列(各元素各不相同),输出该二叉树的后序序列。

格式

输入格式

输入有三行:
第一行为数字n。
第二行有n个数字,表示二叉树的前序遍历。
第三行有n个数字,表示二叉树的中序遍历。

输出格式

输出一行,表示该二叉树的后序遍历序列。

样例

输入

5
1 2 4 5 3
4 2 5 1 3

输出

4 5 2 3 1

```cpp
#include <iostream>
using namespace std;

template<class T>
// 链表二叉树的节点结构
struct binaryTreeNode {
    T element;
    binaryTreeNode<T> *leftChild; // 左子树
    binaryTreeNode<T> *rightChild; // 右子树
    binaryTreeNode() { leftChild = rightChild = NULL; }// 初始化
    binaryTreeNode(const T &theElement) {
        element = theElement;
        leftChild = rightChild = NULL;
    }

    binaryTreeNode(const T &theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild) {
        element = theElement;
        leftChild = theLeftChild;
        rightChild = theRightChild;
    }


};

template<class T>
void postOrder(binaryTreeNode<T>*t){
    if(t != NULL){
        postOrder(t->leftChild);
        postOrder(t->rightChild);
        cout<<t->element<<" ";
    }
}

binaryTreeNode<int> *f(int pre[], int L1, int R1, int in[], int L2, int R2) {
    if (L1 > R1)return NULL;
    binaryTreeNode<int> *root = new binaryTreeNode<int>(pre[L1]);
    if (L1 == R1) { return root; }
    int find = L2;
    while (in[find] != pre[L1])find++;

    root->leftChild = f(pre, L1 + 1, L1 + find - L2, in, L2, find - 1);
    root->rightChild = f(pre, L1 + find - L2 + 1, R1, in, find + 1, R2);

    return root;
}

binaryTreeNode<int> *buildNewTree(int a[], int b[],int c) {
    return f(a,0,c-1,b,0,c-1);
}




int main() {
int n;
cin>>n;
int pre[n];
int in[n];
for(int i= 0;i<n;i++)
    cin>>pre[i];
for(int i=0;i<n;i++)
    cin>>in[i];
binaryTreeNode<int> *root = buildNewTree(pre,in,n);
    postOrder(root);
    return 0;
}

标签:rightChild,NULL,return,int,binaryTreeNode,leftChild,二叉树,数据结构,山东大学
From: https://www.cnblogs.com/lyz103/p/17355581.html

相关文章

  • 山东大学数据结构实验8 散列表
    要求使用线性开型寻址实现描述给定散列函数的除数D和操作数m,输出每次操作后的状态。有以下三种操作:插入x,若散列表已存在x,输出“Existed”,否则插入x到散列表中,输出所在的下标。查询x,若散列表不含有x,输出“-1”,否则输出x对应下标。删除x,若散列表不含有x,输出“NotFound”,否......
  • C++数据结构(树)
    树是一种递归定义的数据结构,如果树中节点的各子树从左到右是有次序的,不能互换,则称该树为有序树,否则叫无序树。关于树的节点:节点拥有的子树的个数叫做节点的度如果度为0,那么该节点叫做叶节点或终端节点,除了根节点外的分支节点称为内部节点树的度是各节点度的最大值。节点的子......
  • 什么是数据结构?
    数据结构研究计算机数据间关系,包括数据的逻辑结构和存储结构及其操作。我们接触一种数据结构,一定要掌握这三个方面基本概念1.数据(Data)数据即信息的载体,是能够输入到计算机中并且能被计算机识别、存储和处理的符号总称。2.数据元素(DataElement)数据......
  • 初识数据结构
    什么是数据结构,数据结构可以理解为我们规定数据元素之间具有某种关系或规则,程序员根据这些规则能够更好的管理和操作这些数据。数据元素的关系包括三种:线性关系——1:1线性关系即为数据是一对一的关系,即除了开头的数据元素和最后的数据元素,其他如何应该数据元素有......
  • 2023年电子科技大学ACM-ICPC暑假前集训-数据结构
    Preface学校针对大一新生的暑假前集训的第一个专题DS,由于要求集体写题解就顺便把写好的发上来了由于下面都写了题意所以直接看也能有很多收获,当然非电专的学生的话就没法交题了代码的话由于专题还没结束怕放上来然后被CV导致被爆破,所以应该在这周六专题结束后会放上来下面都是......
  • 山东大学数据结构实验七
    卡片游戏tips:这个题还要参考,同学要加油啦~~要求创建队列类,使用数组描述的循环队列实现卡片游戏描述假设桌上有一叠扑克牌,依次编号为1-n(从上至下)。当至少还有两张的时候,可以进行操作:把第一张牌扔掉,然后把新的第一张(原先扔掉的牌下方的那张牌,即第二张牌)放到整叠牌的最后。......
  • 山东大学数据结构实验六
    计算表达式tips:不要全文复制,会被查重哦注意因为精度问题,请使用double存数据。要求创建栈类,采用数组描述;计算数学表达式的值。输入数学表达式,输出表达式的计算结果。数学表达式由单个数字和运算符+、-、*、/、(、)构成,例如2+3*(4+5)-6/4。假定表达式输入格式合法。格式......
  • 山东大学数据结构实验一(2)
    题目描述现有一个有n个元素的序列\(a=[a_{1},a_{2},\cdots,a_{n}]\),定义其价值为\(\sum_{i=1}^{n}a_{i}\oplusi\)给出这样一个序列,求其所有排列的价值\(v_{i}\)的或\(v_{1}|v_{2}|\cdots|v_{n-1}|v_{n}\)其中\(|\)为位运算或操作,\(\oplus\)为位运算异......
  • 山东大学数据结构实验一(1)
    题目描述现有一个有\(n\)个元素的序列\(a=[a_1,a_2,\cdots,a_n]\),定义这个序列的价值为\(\sum_{i=1}^{n}i\timesa_i\)。空序列的价值为\(0\)。先给你一个长度为\(n\)的序列\(a\),求\(a\)中所有子集价值的异或和,要求子集中元素的相对位置保持不变。异或和:位运算的一种。如果a......
  • 数据结构(刷题)
                  ......