数据结构(二叉树)
来源:互联网 发布:淘宝店铺改名字怎么改 编辑:程序博客网 时间:2024/06/05 20:06
#ifndef TREE_H#define TREE_H#include <iostream>using namespace std;template<class T>struct BinaryTreeNode{ BinaryTreeNode *parent; T data; BinaryTreeNode* leftChild; BinaryTreeNode* rightChild; BinaryTreeNode() { parent = leftChild = rightChild = nullptr; data = (T)0; } BinaryTreeNode(T data, BinaryTreeNode *parent, BinaryTreeNode *leftChild, BinaryTreeNode *rightChild) { this->data = data; this->parent = parent; this->leftChild = leftChild; this->rightChild = rightChild; }};template<class T>class BinaryTree{public: BinaryTree() { root = new BinaryTreeNode<T>(); depth = 0; count = 0; } ~BinaryTree() { ClearBitTree(); delete root; } //清空二叉树,使之成为空二叉树 void ClearBitTree() { } //是否是空二叉树 bool IsEmptyBiTree() { return root->leftChild == nullptr; } //获取树的深度 int GetDepth() { if (IsEmptyBiTree()) { return 0; } return GetDepth(root->leftChild); } int GetDepth(BinaryTreeNode<T> *node) { int leftDepth = node->leftChild == nullptr ? 0 : GetDepth(node->leftChild); int ritghDepth = node->rightChild == nullptr ? 0 : GetDepth(node->rightChild); return (leftDepth > ritghDepth ? leftDepth : ritghDepth) + 1; } //获取树的大小 int GetCount() { return count; } //获取二叉树的跟 BinaryTreeNode<T> GetRoot() { if (IsEmptyBiTree()) { return nullptr; } return root->leftChild; } //获取节点的左兄弟 BinaryTreeNode<T> *GetLeftSibling(BinaryTreeNode<T> *node) { BinaryTreeNode<T> *parent = node->parent; if (parent == nullptr) { return nullptr; } if (parent->leftChild == node) { return nullptr; } return parent->leftChild; } //获取节点的右兄弟 BinaryTreeNode<T> *GetRightSibling(BinaryTreeNode<T> *node) { BinaryTreeNode<T> *parent = node->parent; if (parent == nullptr) { return nullptr; } if (parent->rightChild == node) { return nullptr; } return parent->rightChild; } //LR = 0左孩子, 1右孩子 void InsertChild(BinaryTreeNode<T> *node, int LR, T value) { if (node == nullptr) { return; } if (LR == 0) { BinaryTreeNode<T> *leftChild = new BinaryTreeNode<T>(value, node, nullptr, nullptr); node->leftChild = leftChild; } else if (LR == 1) { BinaryTreeNode<T> *rightChild = new BinaryTreeNode<T>(value, node, nullptr, nullptr); node->rightChild = rightChild; } } void DeleteChild(BinaryTreeNode<T> *node, int LR) { if (node == nullptr) { return; } if (LR == 0) { BinaryTreeNode<T> *leftChild = node->leftChild; if (leftChild == nullptr) { return; } node->leftChild = nullptr; delete leftChild; } else if (LR == 1) { BinaryTreeNode<T> *rightChild = node->rightChild; if (rightChild == nullptr) { return; } node->rightChild = nullptr; delete rightChild; } }protected: ////二叉树的头数据,左孩子为空则表示无数据,是空二叉树 BinaryTreeNode<T> *root; int depth; int count;};class BinaryTreePrint : public BinaryTree<int>{public: void InOrderTraverse(BinaryTreeNode<int> *node) { if (node == nullptr) { return; } InOrderTraverse(node->leftChild); InOrderTraverse(node->rightChild); cout << node->data << endl; } //中序输出树 void InOrderTraversePrint() { if (IsEmptyBiTree()) { cout << "Tree is Empty" << endl; } cout << "Tree Order-----------------------------" << endl; InOrderTraverse(root->leftChild); cout << "Depth = " << GetDepth() << endl; cout << "Count = " << GetCount() << endl; } //先序创建树 void CreateBiTreeNode(BinaryTreeNode<int> **node, BinaryTreeNode<int> *parent) { count++; int num; cin >> num; if (num == 0) { node = nullptr; count--; } else { *node = new BinaryTreeNode<int>(num, parent, nullptr, nullptr); CreateBiTreeNode(&(*node)->leftChild, *node); CreateBiTreeNode(&(*node)->rightChild, *node); } } void CreateBiTree() { CreateBiTreeNode(&root->leftChild, root); }};#endif
0 0
- 数据结构-树-二叉树
- 数据结构::树,二叉树
- 数据结构-二叉树
- 二叉树的数据结构
- 数据结构-二叉树算法
- java数据结构:二叉树
- 数据结构---二叉树
- 数据结构(C++)--二叉树
- JAVA 数据结构 二叉树
- 数据结构-二叉树 问题
- 转贴:数据结构:二叉树
- 二叉树(数据结构 c++)
- 数据结构-二叉树操作
- java数据结构----二叉树
- 数据结构二叉树
- 数据结构中的二叉树
- 数据结构--二叉树
- 数据结构二叉搜索树
- selenium WebDriver简单使用
- CSS选择器
- oop编程 探测器实例
- 【ZT】面试的65个技巧性回答
- Leetcode OJ contest24 543 Diameter of Binary Tree
- 数据结构(二叉树)
- Ubuntu安装genymotion
- 字符串长度及易错
- 学习运维——网络文件协议-CIFS与NFS
- 蓝桥杯-转方阵
- 当你从浏览器地址栏中输入URL回车后发生了什么
- 各大浏览器内核特性及对应的Browserhacks举例
- Cookie, LocalStorage 与 SessionStorage
- Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04