用二叉树表示k叉树(left-child-right-sibling)

IV. Given a tree represented by left-child-right-sibling structure, please describe an algorithm that counts the number of leaf nodes on every level.(15 points)

    给出一个用左孩子右邻居结构表示的树,请描述一个计算每层叶子节点个数的算法。

    题目中用到的结构体定义:

typedef struct TreeNode *Tree;
struct TreeNode {
 Tree Child;
 int key;
 Tree Sibling;
};
int Counter[MAX_LEVEL] ;
/* Counter[i] stores the number of leaves on the i-th level.*/
/* The root is on the level 0. */
void Count_Leaves( Tree T )


    以下图为例 (其中A~M用数字1~13表示,验证结果)


           此树等价于:

                 

#include<bits/stdc++.h>

#define MAX_LEVEL 10
enum{child,sibling,null};

typedef struct TreeNode *Tree;
struct TreeNode{
	Tree Child;
	int key;
	Tree Sibling;
};

Tree root;

int Counter[MAX_LEVEL];


void Count_Leaves(Tree T)
{
	static int layer=0;
	
	if(!T->Child){
		Counter[layer]++;
	}
	
	else if(T->Child){
		layer++;
		Count_Leaves(T->Child);
		layer--;		
	}
	
	if(T->Sibling){
		Count_Leaves(T->Sibling);
	} 
}

void Insert(Tree &T,int type,int x)
{
	if(!T){
		T=(Tree)malloc(sizeof(struct TreeNode));
		T->key=x;
		T->Sibling=NULL;
		T->Child=NULL;
		root=T;
	}
	else{
		Tree p;
		p=(Tree)malloc(sizeof(struct TreeNode));
		p->key=x;
		p->Child=NULL;
		p->Sibling=NULL;
		if(type==child)T->Child=p;
		else if(type==sibling)T->Sibling=p;
	}
}

int main()
{
	int i;
	memset(Counter,0,sizeof(Counter));
	Tree T=NULL;
	Insert(T,null,1);
	Insert(T,child,2);
	Insert(T->Child,sibling,3);
	Insert(T->Child->Sibling,sibling,4);
	Insert(T->Child->Sibling,child,7);
	Insert(T->Child,child,5);
	Insert(T->Child->Child,child,11);
	Insert(T->Child->Child,sibling,6);
	Insert(T->Child->Child->Child,sibling,12);
	Insert(T->Child->Sibling->Sibling,child,8);
	Insert(T->Child->Sibling->Sibling->Child,child,13);
	Insert(T->Child->Sibling->Sibling->Child,sibling,9);	
	Insert(T->Child->Sibling->Sibling->Child->Sibling,sibling,10);
	Count_Leaves(T);
	
	for(i=0;i<MAX_LEVEL;i++){
		printf("%d ",Counter[i]);
	}
	return 0;
}


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树的定义: ```c++ struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; ``` 前序遍历: ```c++ void preOrder(TreeNode* root) { if (root == nullptr) return; cout << root->val << " "; preOrder(root->left); preOrder(root->right); } ``` 中序遍历: ```c++ void inOrder(TreeNode* root) { if (root == nullptr) return; inOrder(root->left); cout << root->val << " "; inOrder(root->right); } ``` 后序遍历: ```c++ void postOrder(TreeNode* root) { if (root == nullptr) return; postOrder(root->left); postOrder(root->right); cout << root->val << " "; } ``` 计算的深度: ```c++ int depth(TreeNode* root) { if (root == nullptr) return 0; int leftDepth = depth(root->left); int rightDepth = depth(root->right); return max(leftDepth, rightDepth) + 1; } ``` 计算叶子节点个数: ```c++ int countLeafNodes(TreeNode* root) { if (root == nullptr) return 0; if (root->left == nullptr && root->right == nullptr) return 1; return countLeafNodes(root->left) + countLeafNodes(root->right); } ``` 查找双亲节点: ```c++ TreeNode* getParent(TreeNode* root, TreeNode* child) { if (root == nullptr || root == child) return nullptr; if (root->left == child || root->right == child) return root; TreeNode* parent = getParent(root->left, child); if (parent != nullptr) return parent; return getParent(root->right, child); } ``` 查找兄弟节点: ```c++ TreeNode* getSibling(TreeNode* root, TreeNode* child) { if (root == nullptr || root == child) return nullptr; if (root->left == child) return root->right; if (root->right == child) return root->left; TreeNode* sibling = getSibling(root->left, child); if (sibling != nullptr) return sibling; return getSibling(root->right, child); } ``` 注意:以上代码仅供参考,实际使用中需要根据具体情况进行修改和适配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值