c++数据结构二叉树
来源:互联网 发布:字体识别在线软件 编辑:程序博客网 时间:2024/05/21 00:55
#pragma once#include<iostream>using namespace std;template<class T>class BinaryNode{public:T data;//二叉数元素的数据BinaryNode *pLchild;BinaryNode *pRchild;//左孩子,右孩子,};template<class T>class BinaryTree{public:BinaryTree();//二叉树的构造函数~BinaryTree();//析构函数void Destroy(BinaryNode<T>*&Q);//销毁函数void _Create(BinaryNode<T>*&Q,T ch[],const T &c,int &i );//先序输入void Create(T ch[], const T &c);//先序输入的接口函数int _Depth(BinaryNode<T>*&Q);//深度函数int Depth();//深度的接口函数void visit(BinaryNode<T>root);//访问结点数据void _PreoderTraverse(BinaryNode<T>*Q, void(*visit)(const T &e));//先序遍历void PreoderTraverse(void(*visit)(const T &e));//先序遍历的接口函数void _InoderTraverse(BinaryNode<T>*Q, void(*visit)(const T &e));//中序遍历void InoderTraverse(void(*visit)(const T &e));//中序遍历的接口函数private:BinaryNode<T> *m_root;//根结点};template <class T>BinaryTree<T>::BinaryTree()//构造函数创建一个根结点{m_root = new BinaryNode<T>//从堆中申请空间m_root->pLchild = NULL;//将左右孩子设置为空指针m_root->pRchild = NULL;}/*析构函数*/template <class T>BinaryTree<T>::~BinaryTree(){Destroy(m_root);//销毁二叉树}/*销毁二叉链表建立的树*/template<class T>void BinaryTree<T>::Destroy(BinaryNode<T>*&Q){if (Q){Destroy(Q->pLchild);Destroy(Q->pRchild);delete Q;}Q = NULL;}/*先序的方式建立二叉树*/template<class T>void BinaryTree<T>::_Create(BinaryNode<T>*&Q, T ch[],const T &c, int &i)//ch为数组变量用于先序建立二叉树的存储{if (ch[i] == c)//c为空指针的表示符Q = NULL;else{Q = new BinaryNode<T>;Q->data = ch[i];//传入数据_Create(Q->pLchild, ch,c,++i);_Create(Q->pRchild, ch,c,++i);}}/*先序建立二叉树的接口函数*/template<class T>void BinaryTree<T>::Create(T ch[], const T &c){int i = 0;_Create(m_root, ch, c, i);}/*深度函数*/template<class T>int BinaryTree<T>::_Depth(BinaryNode<T>*&Q){if (!Q)return 0;int h1, h2;h1 = _Depth(Q->pLchild);h2 = _Depth(Q->pRchild);return h1 > h2 ? h1 + 1: h2 + 1;}/*深度的接口函数*/template<class T>int BinaryTree<T>::Depth(){return _Depth(m_root);}/*visit函数*/template<class T>void BinaryTree<T>::visit(BinaryNode<T>root){if (root != NULL){cout << root->data;}}/*先序遍历*/template<class T>void BinaryTree<T>::_PreoderTraverse(BinaryNode<T>*Q, void(*visit)(const T &e)){if (Q){visit(Q->data);_PreoderTraverse(Q->pLchild, visit);_PreoderTraverse(Q->pRchild, visit);}}template<class T>void BinaryTree<T>::PreoderTraverse(void(*visit)(const T &e)){_PreoderTraverse(m_root,visit);}/*中序遍历的接口函数*/template<class T>void BinaryTree<T>::_InoderTraverse(BinaryNode<T>*Q, void(*visit)(const T &e)){if(Q)//if语句不能少,少了则发生溢出{ _InoderTraverse(Q->pLchild, visit);visit(Q->data);_InoderTraverse(Q->pRchild, visit);}}template<class T>void BinaryTree<T>::InoderTraverse(void(*visit)(const T &e)){_InoderTraverse(m_root, visit);}二叉树中奇数结点函数的个数的
//奇数结点的个数template<class T>int BinaryTree<T>::OddCount(){int i = 0;return _OddCount(m_root,i);}//奇数结点的接口函数template<class T>int BinaryTree<T>::_OddCount(BinaryNode<T>*&Q,int &i){if (Q == NULL) return 0;else if (Q->data % 2 == 1)++i;_OddCount(Q->pLchild, i);_OddCount(Q->pRchild, i);return i;}
主函数
#include<iostream>#include"SqStack.h"#include"LinkStack.h"#include"SqQueue.h"#include"BinaryTree.h"#include"prcatice.h"using namespace std;void Print(const char&c){cout << c << " ";}int main(){cout << "---二叉树部分基本操作的演示实例---" << endl << endl;cout << "<模板是二叉链表类>" << endl;BinaryTree<char> bt1;char c = '#';char ch1[100];cout << "请按先序方式输入所需建树的数据(此处空指针用#表示,数据用以建立对象):" << endl;cin >> ch1;bt1.Create(ch1, c);cout << endl;cout << "层数是" << bt1.Depth()<<endl;cout << "先序遍历的结果"<<endl;bt1.PreoderTraverse(Print);cout << endl << endl;cout << "中序遍历的结果" << endl;bt1.InoderTraverse(Print); return 0;}
堆
堆是一种特殊的类型的二叉树,具有以下两个性质:(1)每个节点的值大于等于每个子节点的值(2)该树完全平衡后,最后一层的叶子都处于最左侧的位置确切的说上面两个定义的是最大堆(max heap)如果将第一个性质中的“大于”替换为“小于”,则定义的就是最小堆(min heap)这意味 最大堆的根节点包含最大的元素,最小堆的根节点包含最小的元素。根据第二个条件,树中层次的数目是 lg n将堆作为优先队列
堆非常适合于实现优先队列伪代码添加元素的算法如下heapEnqueue(el)将el放在堆的末尾:while el不位于根部,并且el>parent(el)el与其父节点交换;删除元素的算法如下heapDequeue(el)从根节点中提取元素:将最后一个叶子点中的元素放在要删除元素的位置;删除最后一个叶子节点;//根的两个子树都是堆;p=根节点;while p 不是叶子结点,并且p<他的任何子节点交换p与其较大的子节点;
0 0
- 数据结构(C++)--二叉树
- 二叉树(数据结构 c++)
- 【数据结构】二叉树(c++)
- 数据结构---二叉树(C#)
- 数据结构-二叉树 链式 c
- C语言-数据结构-二叉树
- 数据结构--二叉树(C++)
- 《数据结构(C语言版)》- 树和二叉树
- 二叉树排序 --C语言数据结构
- c#-二叉树数据结构及算法
- 数据结构的C实现_二叉树
- C语言数据结构之二叉树
- (C语言)二叉树实现(数据结构十三)
- C 实现数据结构二叉查找树
- [数据结构]C语言二叉树的实现
- 数据结构-------------线索二叉树(c语言)
- 数据结构 二叉树的实现 c语言版
- 数据结构C语言二叉树的遍历
- Revit开发AddinManager原理探索
- Map阶段分析之Spill阶段
- 欢迎使用CSDN-markdown编辑器
- hdu1789 简单贪心算法
- 关键字【一】
- c++数据结构二叉树
- C语言实现的密码输入功能
- 服务迁移总结
- U-boot主Makefile详尽分析
- [心得] Thrift协议以及相关压测经验踩坑总结
- HDFS中高可用性HA的讲解
- 嵌入式学习心得(十)变量和常量以及内存
- hadoop笔记1-MR执行过程
- html课程表页面