【C++】树/森林以孩子兄弟链表为存储结构,用非递归求其高度

算法思想:对树/森林逐层遍历,遍历的层数即为所求高度

#define maxlen 100
typedef int elem;
typedef struct forestnode {
	struct forestnode* firstson; //指向当前结点的第一个孩子
	struct forestnode* nextbrother; //指向当前结点的下一个兄弟
	elem data;
	int number; //结点序号
}Fnode;
typedef struct forest {
	Fnode* root;
	int size; 
}Forest;
int Fheight(Forest* t) //非递归求森林高度
{
	Fnode* node[maxlen];
	bool sign = false; //用来标记是否还有孩子
	int head = 0; //指向每一层的第一个结点
	int rear = 0; //指向数组中的最后一个结点
	int end = rear; //指向每一层的最后一个结点
	int aim = head;
	Fnode* p = t->root;
	int height = 0;
	node[end] = p;
	start: //开始遍历
	while (aim <= end) //遍历当前层所有剩余结点
	{
		p = node[aim];
		while (p->nextbrother) //遍历当前结点的所有兄弟结点
		{
			rear++;
			node[rear] = p->nextbrother;
			p = p->nextbrother;
		}
		aim++;
	}
	aim = head;
	end = rear;
	height++;
	while (aim <= end) //遍历当前层的每一个结点的第一个孩子结点
	{
		if (node[aim]->firstson)
		{
			sign = true; //判断通过,说明还有孩子,修改标记
			rear++;
			node[rear] = node[aim]->firstson;
		}
		aim++;
	}
	if (!sign) //判断通过,说明没有孩子了,返回高度
		return height;
	end = rear; //使end指向下一层的最后一个结点
	head = aim; //使head指向下一层的第一个结点
	sign = false; //重置标记
	goto start;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++实现的代码: ```c++ #include <iostream> #include <stack> #include <queue> using namespace std; // 二叉树结点的定义 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 根据输入的序列建立二叉链表存储结构 TreeNode* buildTree() { int val; cin >> val; if (val == -1) { // -1代表空节点 return NULL; } TreeNode* root = new TreeNode(val); root->left = buildTree(); root->right = buildTree(); return root; } // 输出二叉树的先序遍历结果(递归算法) void preOrder(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } // 输出二叉树的中序遍历结果(递归算法) void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } // 输出二叉树的后序遍历结果(递归算法) void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } // 先序遍历二叉树非递归算法) void preOrderNoRecursion(TreeNode* root) { stack<TreeNode*> s; TreeNode* p = root; while (p != NULL || !s.empty()) { if (p != NULL) { cout << p->val << " "; s.push(p); p = p->left; } else { p = s.top(); s.pop(); p = p->right; } } } // 借助队列实现二叉树的层次遍历 void levelOrder(TreeNode* root) { queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* p = q.front(); q.pop(); cout << p->val << " "; if (p->left != NULL) { q.push(p->left); } if (p->right != NULL) { q.push(p->right); } } } // 求二叉树高度 int getHeight(TreeNode* root) { if (root == NULL) { return 0; } int leftHeight = getHeight(root->left); int rightHeight = getHeight(root->right); return max(leftHeight, rightHeight) + 1; } // 求二叉树叶子结点个数 int getLeafCount(TreeNode* root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } return getLeafCount(root->left) + getLeafCount(root->right); } // 将二叉链表视为森林孩子兄弟链表,计算其叶子结点个数 int getLeafCount2(TreeNode* root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1 + getLeafCount2(root->right); } return getLeafCount2(root->left) + getLeafCount2(root->right); } int main() { // 建立二叉树 TreeNode* root = buildTree(); // 输出先序、中序、后序遍历结果(递归算法) cout << "Pre-order traversal: "; preOrder(root); cout << endl; cout << "In-order traversal: "; inOrder(root); cout << endl; cout << "Post-order traversal: "; postOrder(root); cout << endl; // 先序遍历二叉树非递归算法) cout << "Pre-order traversal (no recursion): "; preOrderNoRecursion(root); cout << endl; // 层次遍历二叉树 cout << "Level-order traversal: "; levelOrder(root); cout << endl; // 求二叉树高度 cout << "Height: " << getHeight(root) << endl; // 求二叉树叶子结点个数 cout << "Leaf count: " << getLeafCount(root) << endl; // 将二叉链表视为森林孩子兄弟链表,计算其叶子结点个数 cout << "Leaf count 2: " << getLeafCount2(root) << endl; return 0; } ``` 以上代码实现了对于任意给定的二叉树完成下列操作: 1. 根据输入的序列,建立二叉链表存储结构; 2. 输出该二叉树的先序、中序、后序遍历结果(递归算法); 3. 先序遍历二叉树非递归算法); 4. 借助队列实现二叉树的层次遍历; 5. 求二叉树高度; 6. 求二叉树叶子结点个数; 7. 将二叉链表视为森林孩子兄弟链表,计算其叶子结点个数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值