📄 wex13_5b.cpp
字号:
#include <iostream.h>
#pragma hdrstop
// traverse the array based tree by level and visit each node
template <class T>
void LevelScan(T A[], int n, void visit(T& item))
{
// keep track of last index in current level
int endLevel = 0;
// cycle through the elements A[0], A[1], ..., A[n-1]
for(int i=0;i < n;i++)
{
visit(A[i]);
if (i == endLevel)
{
// visited the last node at the current level.
// output a newline and determine the last node at
// the next level
cout << endl;
endLevel = 2*endLevel + 2;
}
}
// output a final newline if the tree is not full.
// the tree is full if the parent of endLevel is i
if (i != (endLevel-1)/2)
cout << endl;
}
void print(int& item)
{
cout << item << " ";
}
void main(void)
{
int a[63], n;
cout << "Enter the number of nodes (1 <= n <= 63): ";
cin >> n;
for (int i=0;i < n;i++)
a[i] = i+1;
LevelScan(a,n,print);
}
/*
<Run>
Enter the number of nodes (1 <= n <= 63): 35
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
32 33 34 35
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -