⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex11_1.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 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 + -