实验9.1 二叉树基础

实验9.1 二叉树基础

描述

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

格式

输入格式

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

输出格式

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

思路和探讨

二叉树相关知识

笔记补充——第十一章:二叉树和其他树,尤其关注11.6

整体思路描述

定义及实现建立树、前序遍历、中序遍历、后序遍历、计算二叉树高度、二叉树节点数目等函数。实验9.1是完全基于二叉树最基本遍历知识的对应实验。

细节思路描述

前序遍历

先访问一个节点,再访问该节点的左右子树(根、左、右)

在这里插入图片描述

中序遍历

先访问一个节点的左子树,然后访问该节点,最后访问右子树(左、根、右)

在这里插入图片描述

后序遍历

先访问一个节点的左右子树,再访问该节点(左、右、根)

在这里插入图片描述

层次遍历

从上往下逐层,同层从左到右的次序访问各元素

借助队列来实现,核心思想:每次出队一个元素,就将该元素的孩子节点加入队列中,直至队列中元素个数为0时,出队的顺序就是该二叉树的层次遍历结果

在这里插入图片描述


若已看懂思路,试着自己写~


实现代码

#include <iostream>
#include <queue>
using namespace std;
 
template<class T>
struct binaryTreeNode
{// 链表二叉树的节点结构
    T element;
    binaryTreeNode<T> *leftChild; // 左子树
    binaryTreeNode<T> *rightChild; // 右子树
    //第一个构造函数——无参数
    binaryTreeNode(){leftChild = rightChild = NULL;}
    //第二个构造函数——有一个参数用来初始化element,而指针域被置为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(){}; //析构函数
        
        binaryTreeNode<T>* find(binaryTreeNode<T>*t, int k);//查找 
        void makeTree(int n); //建立树
        
        void visit(binaryTreeNode<T> *x);//遍历辅助
        void preOrder(binaryTreeNode<T>*t);//前序遍历
        void inOrder(binaryTreeNode<T>*t);//中序遍历
        void postOrder(binaryTreeNode<T>*t);//后序遍历
        void levelOrder(); //层次遍历
        
        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;
        }
		void out(int n);//输出节点数和高度 
    private:
        binaryTreeNode<T> *root;//指向根的指针
        int treeSize;//树的节点个数
};

//查找
template<class T>
binaryTreeNode<T>*linkedBinaryTree<T>::find(binaryTreeNode<T>*t, int k)
{
    queue <binaryTreeNode<T>*>q;
	while (t!=NULL)
	{
		if (t->element==k)
			return t;
		if (t->leftChild!=NULL)
			q.push(t->leftChild);
		if (t->rightChild!=NULL)
			q.push(t->rightChild);
		if (q.empty())
			return NULL;
		t=q.front();
		q.pop();
	}
}
 
//建立树
template<class T>
void linkedBinaryTree<T>::makeTree(int n)
{
    root = new binaryTreeNode<T>(1);
    for(int i = 1; i <= n; i++)
	{
        binaryTreeNode<T>*p = find(root, i);
        int a,b;
	  	cin>>a>>b;
	  	if (a!=-1)
	  		p->leftChild=new binaryTreeNode<T> (a); 
		if (b!=-1)
			p->rightChild=new binaryTreeNode<T> (b);
    }
}

//遍历辅助 
template <class T>
void linkedBinaryTree<T>::visit(binaryTreeNode<T> *x) 
{//访问节点*x,仅输出element域
	cout << x->element <<' ';
}

//前序遍历
template<class T>
void linkedBinaryTree<T>::preOrder(binaryTreeNode<T>*t)
{
    if(t != NULL)
	{
        linkedBinaryTree<T>::visit(t);//访问树根
        preOrder(t->leftChild);//前序遍历左子树
        preOrder(t->rightChild);//前序遍历右子树
    }
}
 
//中序遍历
template<class T>
void linkedBinaryTree<T>::inOrder(binaryTreeNode<T>*t)
{
    if(t != NULL)
	{
        inOrder(t->leftChild);//中序遍历左子树
        linkedBinaryTree<T>::visit(t);//访问树根
        inOrder(t->rightChild);//中序遍历右子树
    }
}
 
// 后序遍历
template<class T>
void linkedBinaryTree<T>::postOrder(binaryTreeNode<T>*t)
{
    if(t != NULL)
	{
        postOrder(t->leftChild);//后序遍历左子树
        postOrder(t->rightChild);//后序遍历右子树
        linkedBinaryTree<T>::visit(t);//访问树根
    }
}
 
// 层次遍历
template <class T>
void linkedBinaryTree<T>::levelOrder()
{
    queue<binaryTreeNode<T>*>q;
    binaryTreeNode<T>*t = root;
    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;
}

//计算树的高度
template<class T>
int linkedBinaryTree<T>::height(binaryTreeNode<T>*t)
{
    if(t == NULL)
	{
        return 0;
    }
    int h1 = height(t->leftChild);
    int h2 = height(t->rightChild);
    if(h1 > h2)
        return ++h1;
    else
        return ++h2;
}

//输出节点数和高度
template<class T>
void linkedBinaryTree<T>::out(int n)
{
    int a[n], b[n];
    for(int i = 1; i <= n; i++)
	{
        binaryTreeNode<T>*y;
        y = find(root, i);
        a[i] = height(y);
        b[i] = number(y);
    }
    for(int i = 1; i <= n; i++)
	{
        cout << b[i] << " ";
    }
    cout << endl;
    for(int i = 1; i <= n; i++)
	{
        cout << a[i] << " ";
    }
    cout << endl;
}

int main()
{
    int n;
    cin >> n;
    linkedBinaryTree<int>q;
    q.makeTree(n);
    q.levelOrder();
    q.out(n);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啦啦右一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值