二叉树及树的操作

在这里插入图片描述
1.2题

#include<iostream>
#include<vector>
using namespace std;
vector<char> v;
#include<queue>
#define max(a,b) a>b?a:b
struct BinTreeNode
{
    
    
	char data;
	BinTreeNode* leftChild;//左孩子指针
	BinTreeNode* rightChild;//右孩子指针
	
};
class BinaryTree
{
    
    
public:
	BinTreeNode* root;
	BinTreeNode* Create(BinTreeNode* bt);

	BinaryTree();
	void LeverOrder();   // 层序遍历二叉树
};
BinTreeNode* BinaryTree:: Create(BinTreeNode* bt)
{
    
    
	char data;
	cin >> data;
	if (data == '#') {
    
    
		v.push_back(data);
		bt = NULL;
	}
	else {
    
    
		bt = new BinTreeNode;
		bt->data = data;
		v.push_back(data);
		bt->leftChild = Create(bt->leftChild);
		bt->rightChild = Create(bt->rightChild);
	}
	return bt;
}
BinaryTree::BinaryTree()
{
    
    
	root = Create(root);
}
void BinaryTree::LeverOrder()   // 层序遍历二叉树
{
    
    
	if (root == NULL)
		return;
	queue<BinTreeNode*> Q1, Q2;        // 定义一个队列
	int mx = 0, cnt;
	BinTreeNode* q;
	Q1.push(root);
	int count = 0;
	while (!Q1.empty())
	{
    
    
		cnt=0;
		while (!Q1.empty())
		{
    
    
			cnt++;
			q = Q1.front();
			//cout << Q1.front()->data << endl;
			if (q->leftChild != NULL)
			{
    
    
				Q2.push(q->leftChild);
				//cout << "Q2.data= "  << " "<<Q2.front()->data;
			}
			if (q->rightChild != NULL)
			{
    
    
				Q2.push(q->rightChild);
				//cout << "Q2.data= "  << " " << Q2.front()->data;
			}
			if (Q1.front()->leftChild == NULL && Q1.front()->rightChild==NULL)
			{
    
    
				count++;
			}
			//cout <<"       Q2的大小"<< Q2.size() << endl;
			//cout <<"cnt:"<< cnt << " ";
			Q1.pop();
		}
		//cout << endl<< "cnt:" << cnt << endl;
		mx = max(mx, cnt);
		//cout << mx << endl;
		Q1 = Q2;
		while (!Q2.empty())
		{
    
    
			//cout << "Q2.data= " << Q2.front()->data << endl;
			Q2.pop();
		}   
	}
	cout << "最大宽度:"<<mx << endl;
	cout <<"叶子节点个数:"<< count << endl;
}

int main()
{
    
    
	BinaryTree* bitree = new BinaryTree();
	bitree->LeverOrder();
	cout << endl<<"层序遍历:";
	for (auto i : v) {
    
    
		cout << i << " ";
	}
	system("pause");
	return 0;
}

3.4题
孩子兄弟表示法

#include<iostream>
using namespace std;
struct childSibling   //节点类
{
    
    
	char data; 
	childSibling* firstChild; //第一个孩子节点
	childSibling* nextSibling;//下一个兄弟节点
	childSibling();//构造函数
	childSibling(char d, childSibling* fChild = NULL, childSibling* nSibling = NULL);
};
childSibling::childSibling()//构造函数
{
    
    
	firstChild = NULL;
	nextSibling = NULL;
}
childSibling::childSibling(char d, childSibling* fChild, childSibling* nSibling)
{
    
    
	data = d;
	firstChild = fChild;
	nextSibling = nSibling;
}
class childSiblingTree//节点数类
{
    
    
public:
	childSibling* root;
	childSiblingTree();//构造函数
	childSiblingTree(char item[], int parent[], int n);//通过双亲表示法转化成孩子兄弟表示法
	childSibling* childSiblingTreeHelp(char item[], int parent[], int r, int n);//有助于构造函数的实现
	childSibling* FirstChild(childSibling* cur);//返回cur节点的第一个孩子节点
	childSibling* NextSibling(childSibling* cur);//返回cur节点的下一个兄弟节点
	int getHeight(childSibling* r);
	int getHeight();//求高度
	int getDegree(childSibling* r);
	int getDegree();//求深度
};
childSiblingTree::childSiblingTree()//构造函数
{
    
    
	root = NULL;
}
childSibling* childSiblingTree::childSiblingTreeHelp(char item[], int parent[], int r, int n)//有助于构造函数的实现
{
    
    
	if (r >= 0 && r < n)
	{
    
    
		childSibling* rt = new childSibling(item[r]);
		childSibling* subRoot=NULL, * cur=NULL;
		for (int i = 0; i < n; i++)
		{
    
    
			if (parent[i] == r)
			{
    
    
				subRoot = childSiblingTreeHelp(item, parent, i, n);
				if (rt->firstChild == NULL)
				{
    
    
					rt->firstChild = subRoot;
					cur = subRoot;
				}
				else
				{
    
    
					cur->nextSibling = subRoot;
					cur = subRoot;
				}
			}
		}
		return rt;
	}
	else
	{
    
    
		return NULL;
	}
}
childSiblingTree::childSiblingTree(char item[], int parent[], int n)
{
    
    
	root = childSiblingTreeHelp(item, parent, 0, n);
}
childSibling* childSiblingTree::FirstChild(childSibling* cur)
{
    
    
	if (cur == NULL)
	{
    
    
		return NULL;
	}
	else
	{
    
    
		return cur->firstChild;
	}
}
childSibling* childSiblingTree::NextSibling(childSibling* cur)
{
    
    
	if (cur == NULL)
	{
    
    
		return NULL;
	}
	else
	{
    
    
		return cur->nextSibling;
	}

}
int childSiblingTree::getHeight(childSibling * r)
{
    
    
		childSibling* p;
		if (r == NULL)
		{
    
    
			return 0;
		}
		else
		{
    
    
			int max = 0, h;
			for (p = FirstChild(r); p != NULL; p = NextSibling(p))
			{
    
    
				h = getHeight(p);
				max = (max < h) ? h : max;
			}
			return max + 1;
		}
}
int childSiblingTree::getHeight()
{
    
    
	return getHeight(root);
}
int childSiblingTree::getDegree(childSibling* r)
{
    
    
	if (r == NULL)
	{
    
    
		return 0;
	}
	else
	{
    
    
		childSibling* p;
		int max = 0, d = 0;
		for (p = FirstChild(r); p != NULL; p = NextSibling(p))
		{
    
    
			d++;
			int sub = getDegree(p);
			max = (sub < max) ? max : sub;
		}
		return (d < max) ? max : d;
	}
}
int childSiblingTree::getDegree()
{
    
    
	return getDegree(root);
}
int main()
{
    
    
	char item[] = {
    
     'A','B','C','D','E','F','G','H' };
	int parent[] = {
    
     -1,0,0,0,1,1,3,3 };
	int n = 8;
	childSiblingTree t(item, parent, n);
	cout << "height:" << t.getHeight() << endl;;
	cout << "degree:" << t.getDegree()<<endl;
	system("pause");
	return 0;
}

おすすめ

転載: blog.csdn.net/qq_53336225/article/details/121069621