C++多态实现二叉树

C++多态实现二叉树


前言

本篇文章为笔者的读书笔记,未经允许请勿转载。如果对你有帮助记得点个赞(●’◡’●)
本文主要利用c++的多态特性来实现二叉树,遍历利用了四种方式:前序遍历,中序遍历,后序遍历,层序遍历(队列实现),还有利用栈来析构节点对象;
这些遍历步骤的详细解释已在笔者前几篇博文中讲到,有兴趣的朋友可以参考一下;


源码如下:

stdafx.h

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO:  在此处引用程序需要的其他头文件
#include<iostream>
#include<string>
#include"BinNode.h"
#include"BinTree.h"
using namespace std;

main

// 3二叉树和完全二叉树.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

void text()
{
	auto a = new BinNode<string>("A");
	auto b = new BinNode<string>("B");
	auto c = new BinNode<string>("C");
	auto d = new BinNode<string>("D");
	auto e = new BinNode<string>("E");
	auto f = new BinNode<string>("F");
	auto g = new BinNode<string>("G");
	//给根节点添加子节点
	a->left = b;
	a->right = c;

	b->left = d;
	b->right = e;

	c->left = f;
	c->right = g;
	BinTree<string> binTree(a);
	cout << "二叉树的节点数量:" << binTree.size() << endl;
	cout << "二叉树的深度:" << binTree.depth() << endl;
	cout << "前序遍历:"; 
	binTree.preorder(); 
	cout << "中序遍历:";
	binTree.inorder(); 
	cout << "后序遍历:";
	binTree.postorder();
	cout << "层序遍历:";
	binTree.depthorder();
}
int _tmain(int argc, _TCHAR* argv[])
{
	text();
	system("pause");
	return 0;
}

BinNode.hpp

#pragma once
#include<string>
using std::string;
//节点
template <class T>
class BinNode
{
	
	T val;
public:
	BinNode* left;
	BinNode* right;
	BinNode(T val) :val(val), left(nullptr), right(nullptr){}
	T getName()
	{
		return val;
	}
};

BinTree.hpp

#pragma once
#include<queue>
#include<stack>
#include "BinNode.h"
//树的管理者
template <class T>
class BinTree
{
	typedef BinNode<T> Node;
	Node* root;
	int _size(Node* node)
	{
		if (node == nullptr) return 0;
		return _size(node->left) + _size(node->right) + 1;
	}
	int _depth(Node* node)
	{
		if (node == nullptr) return 0;
		int left = _depth(node->left) + 1;//+1代表节点自身的存在
		int right = _depth(node->right) + 1;
		return left > right ? left : right;
	}
	void _preorder(Node* node)
	{
		if (node == nullptr) return;
		cout << node->getName() << " ";
		_preorder(node->left);
		_preorder(node->right);
	}
	void _inorder(Node* node)
	{
		if (node == nullptr) return;
		_inorder(node->left);
		cout << node->getName() << " ";
		_inorder(node->right);
	}
	void _postorder(Node* node)
	{
		if (node == nullptr) return;
		_postorder(node->left);
		_postorder(node->right);
		cout << node->getName() << " ";
	}
public:
	BinTree(Node* root) :root(root) {}
	//递归获取大小
	int size()
	{
		return _size(root);
	}
	//获取节点深度
	int depth()
	{
		return _depth(root);
	}
	//前序遍历
	void preorder()
	{
		_preorder(root);
		cout << endl;
	}
	//中序遍历
	void inorder()
	{
		_inorder(root);
		cout << endl;
	}
	//后序遍历
	void postorder()
	{
		_postorder(root);
		cout << endl;
	}
	//层序遍历,利用队列实现
	void depthorder()
	{
		queue<Node*> que;
		que.push(root);
		while (!que.empty())
		{
			Node* node = que.front();
			que.pop();
			cout << node->getName() << " ";
			if (node->left)
			{
				que.push(node->left);
			}
			if (node->right)
			{
				que.push(node->right);
			}
		}
		cout << endl;
	}
	//利用栈实现析构
	~BinTree()	{
		stack<Node*> st;
		st.push(root);
		while (!st.empty())
		{
			Node*node = st.top();
			st.pop();
			cout << node->getName() << "~ ";
			if (node->left)
			{
				st.push(node->left);
			}
			if (node->right)
			{
				st.push(node->right);
			}
			delete node;
		}
		cout << endl;
	}
};

测试结果:

二叉树的节点数量:7
二叉树的深度:3
前序遍历:A B D E C F G
中序遍历:D B E A F C G
后序遍历:D E B F G C A
层序遍历:A B C D E F G
A~ C~ G~ F~ B~ E~ D~
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值