数据结构-以二叉链表存储的二叉树

(1)二叉树采用二叉链表存储,编写计算二叉树最大宽度的算法(二叉树的最大宽度是指二叉树所有层中结点个数的最大值)。

(2)以二叉链表作存储结构,设计算法求二叉树中叶子结点的数目。

头文件

/*BTNode.h*/
#include<iostream>
using namespace std;
template <class DataType>
class BTNode {
public:
    DataType data;			//数据域
    BTNode<DataType>* lChild;	//左孩子指针域
    BTNode<DataType>* rChild; 	//右孩子指针域
    BTNode() 		//无参构造函数,创建空节点
    {
        lChild = rChild = NULL;
    }
    //有参构造函数,创建包含数据元素的节点
    BTNode(const DataType& e, BTNode<DataType>* leftChild = NULL, BTNode<DataType>* rightChild = NULL)
    {
        data = e;
        lChild = leftChild;
        rChild = rightChild;
    }
};
/*BinaryTree.h*/
#pragma once
#include"BTNode.h"

template <class DataType>
class BinaryTree {
protected:
    BTNode<DataType>* root; 		//二叉树根指针
    void Destroy(BTNode<DataType>*& r);	//删除以r为根的二叉树
    
public:
    BinaryTree() :root(NULL) {}			//构造函数
    BinaryTree(const DataType& e);      //构造以e为根的二叉树
    virtual ~BinaryTree() { Destroy(root); } 	//析构函数
    BTNode<DataType>* GetRoot() { return root; }   //返回根指针
    bool IsEmpty() { return root == NULL ? true : false; }	//判断二叉树是否为空
    void InsertLeftChild(BTNode<DataType>* p, DataType& e);        //插入e作为节点p的左孩子
    void InsertRightChild(BTNode<DataType>* p, DataType& e);//插入e作为节点p的右孩子	
    int CountWidth() const;   //计算二叉树最大宽度
    int CountLeaf(const BTNode<DataType>* r) const;   //求叶子结点数
};
/*Node.h*/
#include<iostream>
using namespace std;

template <class DataType>
class Node
{
public:
	DataType data;
	Node<DataType>* next;
	Node() { next = NULL; }
	Node(DataType e, Node<DataType>* link = NULL)
	{
		data = e; next = link;
	}
};
/*LinkQueue.h*/
#pragma once
#ifndef _LINKQUEUE_H
#define _LINKQUEUE_H
#include "Node.h"	
template<class DataType>
class LinkQueue {
protected:
	Node<DataType>* front, * rear;	//队头与队尾指针
public:
	LinkQueue();				//构造函数
	virtual ~LinkQueue();			//析构函数
	bool IsEmpty() const;			//判断队列是否为空
	void Clear();				//清空队列
	int EnQueue(const DataType e);	//入队
	int DelQueue(DataType& e);	//出队
};
#endif

函数实现

/*BinaryTree.cpp*/
#include"BinaryTree.h"
#include"LinkQueue.cpp"

template <class DataType>
BinaryTree<DataType>::BinaryTree(const DataType& e )
{
	root = new BTNode<DataType>(e);
}

template <class DataType>
void BinaryTree<DataType>::Destroy(BTNode<DataType>*& r) {
	if (r) {
		Destroy(r->lChild);
		Destroy(r->rChild);
		delete r;
		r = NULL;
	}
}

template <class DataType>
void BinaryTree<DataType>::InsertLeftChild(BTNode<DataType>* p, DataType& e) {
	if (p == NULL)
		return;
	else
	{
		BTNode<DataType>* child = new BTNode<DataType>(e);
		if (p->lChild != NULL)
			child->lChild = p->lChild;
		p->lChild = child;
		return;
	}
}

template <class DataType>
void BinaryTree<DataType>::InsertRightChild(BTNode<DataType>* p, DataType& e) {
	if (p == NULL )
		return;
	else
	{
		BTNode<DataType>* child = new BTNode<DataType>(e);
		if (p->rChild != NULL)
			child->rChild = p->rChild;
		p->rChild = child;
		return;
	}
}

template <class DataType>
int BinaryTree<DataType>::CountWidth() const
{
	LinkQueue<BTNode<DataType>*> q;
	BTNode<DataType>* p;
	int width = 0, n = 0, m;  //width为求到的宽度,n为当前层结点数,m为下一层结点数
	if (root != NULL)
	{
		q.EnQueue(root);
		width = 1;
		n = 1;
	}
	while (!q.IsEmpty())
	{
		m = 0;
		for (int i = 0; i < n; i++)
		{
			q.DelQueue(p);
			if (p->lChild != NULL)
			{
				q.EnQueue(p->lChild);
				m++;
			}
			if (p->rChild != NULL)
			{
				q.EnQueue(p->rChild);
				m++;
			}
		}
		n = m;
		if (m > width)
			width = m;
	}
	return width;
}

template <class DataType>
int BinaryTree<DataType>::CountLeaf(const BTNode<DataType>* r) const
{
	if (r == NULL)
		return 0;
	else
	{
		if (r->lChild == NULL && r->rChild == NULL)
			return 1;
		else
			return CountLeaf(r->lChild) + CountLeaf(r->rChild);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值