求二叉树高度(更新:用C++14编写)

因为树是递归定义的,所以用递归算法很方便。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
using namespace std;

struct Node {
	char data;
	Node *lchild;
	Node *rchild;
};

void High(Node *T, int &h)
{
	if (T == NULL)
		h = 0;
	else {
		int left_h;
		High(T->lchild, left_h);
		int right_h;
		High(T->rchild, right_h);

		h = 1 + max(left_h, right_h);
	}
}

Node *CreateBiTree(Node *&T) {  // 算法6.4
	// 按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,
	// 构造二叉链表表示的二叉树T。
	char ch;
	cin >> ch;
	if (ch == '#')
		T = NULL;
	else {
		if (!(T = (Node *)malloc(sizeof(Node))))
			return 0;
		T->data = ch;              // 生成根结点
		CreateBiTree(T->lchild);   // 构造左子树
		CreateBiTree(T->rchild);   // 构造右子树
	}
	return T;
} // CreateBiTree

void Free(Node *&T)
{
	if (T == NULL)
		return;

	Free(T->lchild);
	//	T->lchild = NULL;
	Free(T->rchild);
	//	T->rchild = NULL;
	free(T);
	T = NULL;
}

int main(int argc, char **argv)
{
	freopen("cin.txt", "r", stdin);

	Node *T = NULL;
	CreateBiTree(T);

	int height;
	High(T, height);
	cout << height << endl;

	Free(T);

	return 0;
}

/* cin.txt:
A
B
C
#
#
D
E
#
G
#
#
F
#
#
#
*/


构造的树:

输出为5。


2015.06.03更新

下面用 C++14 标准重写了程序,定义了 Node 和 Tree 两个类。

#ifndef TREE_H
#define TREE_H

#include <memory>

template <class DataType>
class Node {
	private:
		DataType data_;
		std::shared_ptr<Node> left_;
		std::shared_ptr<Node> right_;

	public:
		Node(const DataType &data = DataType(),
				const std::shared_ptr<Node> &left = nullptr,
				const std::shared_ptr<Node> &right = nullptr)
			: data_(data), left_(left), right_(right) {}

		auto data() const {
			return data_;
		}

		/*
		 * deep copy
		 */
		std::shared_ptr<Node> copy() const {
			auto left = (left_ == nullptr ? nullptr : left_->copy());
			auto right = (right_ == nullptr ? nullptr : right_->copy());

			return std::make_shared<Node>(data_, left, right);
		}
};

template <class DataType>
class Tree {
	private:
		std::shared_ptr<Node<DataType>> root_;

	public:
		Tree(const std::shared_ptr<Node<DataType>> &root = nullptr) : root_(root) {}

		/*
		 * deep copy
		 */
		Tree(const Tree &that) : root_(that.root_->copy()) {}

		/*
		 * deep assignment
		 */
		Tree & operator=(const Tree &that) {
			if (this != &that) {
				root_ = that.root_->copy();
			}

			return *this;
		}

		auto root() const {
			return root_;
		}
};

#endif

下面是测试程序。

#include <iostream>
#include "tree.h"

int main() {
	auto pnode = std::make_shared<Node<int>>(2, nullptr, nullptr);
	auto pnode2 = pnode->copy();
	std::cout << pnode->data() << std::endl;
	std::cout << pnode2->data() << std::endl;

	Tree<int> t(pnode);
	auto t2 = t;
	std::cout << t2.root()->data() << std::endl;
	std::cout << t.root()->data() << std::endl;

	return 0;
}


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值