Donghua University computer problem-calculating the number of leaf nodes

Donghua University computer problem-calculating the number of leaf nodes

Topic description:
From the definition of the tree in the data structure, we can see that except for the root node, each node in the tree has a unique parent node. According to this feature, a continuous set of storage space (one-dimensional array) can be used to store each node in the tree. In addition to saving the information of its own node, the node in the tree also saves the position of its parent node in the array (that is, the subscript in the array. The parent's information is -1 means that the node is the root node) This method of representing the tree is called the parental notation.
The data type of each node in the tree is defined as follows:

struct PTNode{
	char data;//结点数据域
	int parent;//结点双亲在数组中的位置
};

The data type of the tree is defined as follows:

#define MAX_TREE_SIZE 100
struct PTree {
	PTNode nodes[MAX_TREE_SIZE];//存储树中的所有结点
	int n;//树中的结点数,n不超过100
};

Then, in the tree shown in the figure below, the storage structure is based on the parent notation, and it is stored in the form shown in the figure below (n is 10).
Insert picture description here
Insert picture description here
Knowing that a tree has stored the above form, please write the function GetLeavesCount to calculate the number of leaf nodes.
The function prototype of GetLeavesCount is: int GetLeavesCount (PTree T)
Among them, the parameter T saves the number of nodes in the tree and the node array shown in Figure b, and the function returns the number of leaf nodes.
For example: call the function GetLeavesCount (T) on the number in the figure, the return result is 6
The first number n input means the number of nodes in the tree, then there are n lines of input, each line represents the information of a node, the first The information is the data of the node, and the second information is the position of the parent node of the node in the array.
For example, enter:
10
a -1
b 0
c 0
d 0
e 1
f 1
g 1
h 2
i 3
h 3
to create the corresponding number in the figure.
Call the function GetLeavesCount (T) on this tree and return the result as 6.
If you enter:
8
a -1
b 0
e 1
h 2
c 0
d 0
f 5
g 5
Call GetLeavesCount (T) on this tree and the result is 4

Algorithm idea:
traverse the node array. When the node number of the node points to another node, the node is a non-leaf node. When the node is traversed, there is no pointer to the node, then This node is a leaf node. Let's switch the way of thinking here, and it will be easier to count the number of non-leaf nodes.

#include<iostream>
using namespace std;
struct PTNode{
	char data;//结点数据域
	int parent;//结点双亲在数组中的位置
};
#define MAX_TREE_SIZE 100
struct PTree {
	PTNode nodes[MAX_TREE_SIZE];//存储树中的所有结点
	int n;//树中的结点数,n不超过100
};
int GetLeavesCount(PTree T) {
	int count = 0;//用来存储非叶子结点的个数
	for (int i = 0;i < T.n;i++) {
		for (int j = 0;j < T.n;j++) {
			if (i == j)
				continue;//自己不和自己比较
			if (i == T.nodes[j].parent) {//检测到该结点有叶子结点
				count++;//非叶子结点数增加
				break;//不重复计算该结点
			}
		}
	}
	return T.n - count;//返回叶子结点数
}
int main() {
	int n;
	cout << "输入结点个数:";
	cin >> n;
	PTree T;
	T.n = n;
	cout << "输入结点信息:" << endl;
	for (int i = 0;i < n;i++) {
		cin >> T.nodes[i].data >> T.nodes[i].parent;
	}
	cout << "叶子结点个数为:" << GetLeavesCount(T) << endl;
	return 0;
}

Running test results:
Insert picture description here
Insert picture description here

Published 54 original articles · Liked 54 · Visits 3073

Guess you like

Origin blog.csdn.net/weixin_45295612/article/details/105409713