当前位置:编程学习 > VC++ >>

VC++ 树的孩子兄弟表示法

print?树的孩子兄弟表示法,又叫二叉树表示法、二叉链表表示法,它是以二叉链表作为存储结构。 
这个主要是因为在工作中要将一个三级菜单处理成需要的格式。我会在汽车电子中将这个程序列出来。 

树的孩子兄弟表示法,又叫二叉树表示法、二叉链表表示法,它是以二叉链表作为存储结构。
这个主要是因为在工作中要将一个三级菜单处理成需要的格式。我会在汽车电子中将这个程序列出来。[cpp] view plaincopyprint?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; 
        } <

补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,