VC++ 树的孩子兄弟表示法
2013-04-11 14:42
316 查看
树的孩子兄弟表示法,又叫二叉树表示法、二叉链表表示法,它是以二叉链表作为存储结构。 这个主要是因为在工作中要将一个三级菜单处理成需要的格式。我会在汽车电子中将这个程序列出来。
Tree.h struct TreeNode { public: struct TreeNode *firstChild; struct TreeNode *nextSibling; int tagParent; int tagSelf; CString data; }; class CTree { public: CTree(); virtual ~CTree(); TreeNode *root; TreeNode *curr; int tag; void InsertChild(CString strValue); BOOL FirstChild(); BOOL NextSibling(); BOOL CreateOptionTree(CString strValue); void PreOrderTree(TreeNode *parent); int GetTreeDepth(TreeNode *parent); int GetChildNums(TreeNode *parent); void CreateTag(TreeNode *parent); void DeleteTree(TreeNode *parent); void SetCurrent(TreeNode *t); }; Tree.cpp CTree::CTree() { tag = 1; root = new TreeNode; root->firstChild = NULL; root->tagParent = 0; root->tagSelf = tag; curr = root; } CTree::~CTree() { } /************************************************************************ 函数名: FirstChild 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-7 作 用: 使当前节点的第一个孩子节点为当前节点 形参数: 返回值: TRUE:成功 FALSE:失败 修改记录: ************************************************************************/ BOOL CTree::FirstChild() { if (curr != NULL && curr->firstChild != NULL) { curr = curr->firstChild; return TRUE; } else { return FALSE; } } /************************************************************************ 函数名: NextSibling 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-7 作 用: 使当前节点的兄弟节点为当前节点 形参数: 返回值: TRUE:成功 FALSE:失败 修改记录: ************************************************************************/ BOOL CTree::NextSibling() { if (curr != NULL && curr->nextSibling != NULL) { curr = curr->nextSibling; return TRUE; } else { return FALSE; } } /************************************************************************ 函数名: InsertChild 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-7 作 用: 将strValue插入到当前节点的最后一个孩子节点 形参数: 返回值: 修改记录:2013-3-8,增加tagParent,tagSelf ************************************************************************/ void CTree::InsertChild(CString strValue) { TreeNode *newNode = new TreeNode; strValue.TrimLeft(); strValue.TrimRight(); newNode->data = strValue; newNode->firstChild = NULL; newNode->nextSibling = NULL; if (curr->firstChild == NULL) { newNode->tagParent = curr->tagSelf; newNode->tagSelf = ++tag; curr->firstChild = newNode; curr = curr->firstChild; } else { TreeNode *p = curr->firstChild; while(p->data != strValue) { if (p->nextSibling != NULL) { p = p->nextSibling; } else { break; } } if (p->data == strValue) { curr = p; } else { newNode->tagParent = p->tagParent; newNode->tagSelf = ++tag; p->nextSibling = newNode; curr = p->nextSibling; } } } /************************************************************************ 函数名: CreateOptionTree 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-7 作 用: 形参数: 返回值: TRUE:成功 FALSE:失败 修改记录: ************************************************************************/ BOOL CTree::CreateOptionTree(CString strValue) { if (root == NULL) { root = new TreeNode; root->firstChild = NULL; root->tagParent = 0; root->tagSelf = tag; curr = root; } if (strValue == "") { return TRUE; } InsertChild(strValue); return TRUE; } /************************************************************************ 函数名: GetTreeDepth 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-8 作 用: 形参数: 返回值: 修改记录:未完成 ************************************************************************/ int CTree::GetTreeDepth(TreeNode *parent) { int num = 1; if (parent->firstChild == NULL) { return num; } else { TreeNode *p = parent; } } /************************************************************************ 函数名: PreOrderTree 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-8 作 用: 形参数: 返回值: 修改记录: ************************************************************************/ void CTree::PreOrderTree(TreeNode *parent) { if (parent->firstChild != NULL) { PreOrderTree(parent->firstChild); } if (parent->nextSibling != NULL) { PreOrderTree(parent->nextSibling); } } /************************************************************************ 函数名: GetChildNums 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-8 作 用: 得到节点的孩子数 形参数: 返回值: 修改记录: ************************************************************************/ int CTree::GetChildNums(TreeNode *parent) { if (parent->firstChild == NULL) { return 0; } else { int num = 1; TreeNode *p = parent->firstChild; while(p->nextSibling != NULL) { num++; p = p->nextSibling; } return num; } } /************************************************************************ 函数名: DeleteTree 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-11 作 用: 删除以parent为根结点的树 形参数: 返回值: 修改记录: ************************************************************************/ void CTree::DeleteTree(TreeNode *parent) { if (parent == NULL) { return; } TreeNode *p = parent->firstChild, *q; while(p != NULL) { q = p->nextSibling; DeleteTree(p); p = q; } delete parent; } /************************************************************************ 函数名: SetCurrent 作 者: 谭友亮(Charles Tan) 日 期: 2013-3-11 作 用: 设置当前结点 形参数: 返回值: 修改记录: ************************************************************************/ void CTree::SetCurrent(TreeNode *t) { curr = t; }
相关文章推荐
- 树的孩子兄弟表示法
- DELPHI 孩子兄弟表示法 (递归实现)
- 树的孩子兄弟表示法及遍历操作
- UVa 11732 strcmp()函数(左孩子右兄弟表示法)
- 数据结构——树的孩子兄弟表示法
- poj 1192 最优连通子集 树的孩子兄弟表示法+简单树型dp
- 树的孩子表示法,树的兄弟表示法,树的存储结构详解,数据结构-树的学习(2)
- 数据结构之通用树(孩子兄弟表示法)
- 软件设计师2006年11月下午试题5(C语言 树及其孩子-兄弟表示)
- 数据结构:树--孩子兄弟表示法
- C语言数据结构——孩子兄弟表示法
- 孩子兄弟表示法(二叉链表树) 时间 2014-06-02 16:32:17 CSDN博客 原文 http://blog.csdn.net/chdjj/article/details/2811164
- 树的左孩子 右兄弟表示法的建立过程 (后序遍历)
- UVa11732 "strcmp()" Anyone?(Trie树+孩子兄弟表示法)
- 6-7-树的二叉链表(孩子-兄弟)存储表示-树和二叉树-第6章-《数据结构》课本源码-严蔚敏吴伟民版
- 树的孩子兄弟表示法 及遍历实现
- 求 以孩子-兄弟链表表示的树 的度、深度、叶结点和边
- 树-孩子兄弟表示法的实现
- 设计递归算法,求以“孩子-兄弟链表”表示的度
- <数据结构>孩子兄弟表示法家谱