数据结构——普通树的定义与C++实现
来源:互联网 发布:无心整合捏脸数据 编辑:程序博客网 时间:2024/06/05 05:39
github地址:https://github.com/lining91/NormalTree
实现了树的兄弟孩子表示法,插入新数据,查询某个数据,前序遍历,中序遍历,后序遍历。
相关概念:
每一棵树都有一个根节点。
每个节点可以有多个儿子节点,没有儿子的结点叫做叶子节点。
具有相同父亲的节点叫做兄弟节点。
对于任意一个节点ni,ni的深度为从根到ni的唯一路径的长。因此,根的深度是0。
ni的高是从ni到一片树叶的最长路径的长。因此叶子节点的高是0。一棵树的高等于它的根的高。
一棵树的深度等于它的最深的树叶的深度,该深度等于这棵树的高。
树的实现:
使用树的兄弟孩子表示法。
typedef struct STreeNode* pSTreeNode;
struct STreeNode
{
int nValue;
pSTreeNode pFirstChild;
pSTreeNode pNextBrother;
STreeNode(){ nValue = 0; pFirstChild = NULL; pNextBrother = NULL;}
};
其中pFirstChild(左孩子节点)表示第一个儿子节点,pNextBrother(右孩子节点)表示下一个兄弟节点。
一颗普通树表示如下:
使用兄弟孩子表示法表示如下:
树的遍历:
前序遍历(先根节点,后左孩子节点,再右孩子结点)
中序遍历(先左孩子结点,后根节点,再右孩子结点)
后序遍历(先做孩子节点,后右孩子结点,再根节点)
代码如下:
#include <iostream>using namespace std;typedef struct STreeNode* pSTreeNode;typedef int TreeDataType;struct STreeNode{ TreeDataType data; pSTreeNode pFirstChild; pSTreeNode pNextBrother; STreeNode( TreeDataType Value ) { data = Value; pFirstChild = NULL; pNextBrother = NULL; }}; class CTree{public: CTree(); CTree( TreeDataType Value ); ~CTree();public: void Insert( TreeDataType parentValue, TreeDataType Value ); // parentValue:该节点的父结点;Value:该节点的值 void InsertBrother( pSTreeNode pParentNode, TreeDataType Value ); pSTreeNode Search( pSTreeNode pNode, TreeDataType Value ); void Preorder( pSTreeNode pNode ); // 前序遍历 void Inorder( pSTreeNode pNode ); // 中序遍历 void postorder( pSTreeNode pNode ); // 后续遍历 void PrintNode( pSTreeNode pNode ); void FreeMemory( pSTreeNode pNode ); // 释放内存 public: pSTreeNode pRoot;};CTree::CTree(){ pRoot = NULL;}CTree::CTree( TreeDataType Value ){ pRoot = new STreeNode( Value ); if ( pRoot == NULL ) return;}CTree::~CTree(){ if (pRoot == NULL ) return; FreeMemory( pRoot );}void CTree::FreeMemory( pSTreeNode pNode ){ if ( pNode == NULL ) return; if ( pNode->pFirstChild != NULL ) FreeMemory( pNode->pFirstChild ); if ( pNode->pNextBrother != NULL ) FreeMemory( pNode->pNextBrother ); delete pNode; pNode = NULL;}void CTree::Insert( TreeDataType parentValue, TreeDataType Value ){ if ( pRoot == NULL ) return; pSTreeNode pFindNode = Search( pRoot, parentValue ); if ( pFindNode == NULL ) return; if ( pFindNode->pFirstChild == NULL ) { pFindNode->pFirstChild = new STreeNode( Value ); return; } else { InsertBrother( pFindNode->pFirstChild, Value ); return; }}void CTree::InsertBrother( pSTreeNode pBrotherNode, TreeDataType Value ){ if ( pBrotherNode->pNextBrother != NULL ) InsertBrother( pBrotherNode->pNextBrother, Value ); else { pBrotherNode->pNextBrother = new STreeNode( Value ); return; }}pSTreeNode CTree::Search( pSTreeNode pNode, TreeDataType Value ){ if ( pNode == NULL ) return NULL; if ( pNode->data == Value ) return pNode; if ( pNode->pFirstChild == NULL && pNode->pNextBrother == NULL ) return NULL; else { if ( pNode->pFirstChild != NULL ) { pSTreeNode pNodeTemp = Search( pNode->pFirstChild, Value ); if ( pNodeTemp != NULL ) return pNodeTemp; else { return Search( pNode->pNextBrother, Value ); } } else return Search( pNode->pNextBrother, Value ); }}void CTree::Preorder( pSTreeNode pNode ){ if (pNode == NULL) return; cout << " " << pNode->data << " "; Preorder( pNode->pFirstChild ); Preorder( pNode->pNextBrother );}void CTree::Inorder( pSTreeNode pNode ){ if ( pNode == NULL ) return; Inorder( pNode->pFirstChild ); cout << " " << pNode->data << " "; Inorder( pNode->pNextBrother );}void CTree::postorder( pSTreeNode pNode ){ if ( pNode == NULL ) return; postorder( pNode->pFirstChild ); postorder( pNode->pNextBrother ); cout << " " << pNode->data << " ";}int main(){ CTree* pTree = new CTree( 1 ); if ( pTree == NULL ) return 0; pTree->Insert( 1, 2 ); pTree->Insert( 1, 3 ); pTree->Insert( 1, 4 ); pTree->Insert( 1, 5 ); pTree->Insert( 1, 6 ); pTree->Insert( 1, 7 ); pTree->Insert( 4, 8 ); pTree->Insert( 5, 9 ); pTree->Insert( 5, 10 ); pTree->Insert( 6, 11 ); pTree->Insert( 6, 12 ); pTree->Insert( 6, 13 ); pTree->Insert( 10, 14 ); pTree->Insert( 10, 15 ); cout << "前序遍历:" << endl; pTree->Preorder( pTree->pRoot ); cout << endl; cout << "中序遍历:" << endl; pTree->Inorder( pTree->pRoot ); cout << endl; cout << "后序遍历:" << endl; pTree->postorder( pTree->pRoot ); cout << endl; delete pTree; pTree = NULL; system("pause"); return 1;}
运行结果如下:
阅读全文
0 0
- 数据结构——普通树的定义与C++实现
- 数据结构与算法——普通树的定义与C++实现
- 数据结构与算法的实现 —— 结点定义与数据结构的选择
- 《C语言接口与实现》——头文件数据结构定义研究
- 【数据结构】普通二叉树的实现
- C语言数据结构——树的定义
- 【数据结构】普通单链表的实现
- 数据结构线性表顺序结构的定义与实现C语言
- 数据结构与算法专题之树——树与二叉树的定义与性质
- 函数式宏定义与普通函数的区别?C语言宏定义和宏定义函数 ?
- 函数式宏定义与普通函数的区别?C语言宏定义和宏定义函数 ?
- C语言数据结构----栈的定义及实现
- 数据结构——串的定义与基本操作
- 【数据结构—1】线性表的概念与定义
- C语言中宏定义函数与普通函数的区别
- C语言数据结构之普通树篇
- 数据结构入门(四)-栈的定义与实现
- 数据结构入门(五)-队列的定义与实现
- webapi 一个控制器,多个函数名之间的调用
- 查看Java的守护进程PID以及JVM运行情况
- 判断一个数组是一维数组还是二维数组
- JAVA反射强转创建对象
- 身份证检验信息
- 数据结构——普通树的定义与C++实现
- Java中如何使用Redis做缓存应用
- 一个demo类型的监测进程脚本模板
- 线程的调度
- BCD码
- Snackbar
- 解决安卓7.0拍照,相册选择崩溃的问题(包括压缩图片在内)
- 喷水装置(一)||南阳oj
- 美团 ViewPager+GridView实现左右滑动查看更多分类的功能