📄 pex11_1.cpp
字号:
#include <iostream.h>
#pragma hdrstop
#include "treenode.h"
#include "treelib.h"
// count the number of edges in a binary tree
template <class T>
int CountEdges(TreeNode<T> *t)
{
// contains count of the number of edges from node t
int edges = 0;
// if node t has a left subtree, assign edges
// the number of edges on the left subtree + 1
if (t->Left() != NULL)
edges = 1 + CountEdges(t->Left());
// if node t has a right subtree, increment edges
// by the number of edges on the right subtree + 1
if (t->Right() != NULL)
edges += 1 + CountEdges(t->Right());
return edges;
}
void main(void)
{
TreeNode<char> *root;
// "root" is root of character Tree_1
MakeCharTree(root,1);
// test CountEdges by applying it to Tree_1
cout << "Tree_1 has " << CountEdges(root) << " edges" << endl;
}
/*
<Run>
Tree_1 has 8 edges
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -