数据结构(树)

来源:互联网 发布:精华乳和精华液 知乎 编辑:程序博客网 时间:2024/06/05 15:17
描述:
    1、能够查找、显示当前结点的子、亲、兄弟。
    2、能够插入、删除结点。
    3、能前序、后续遍历树。
    4、能直接运行。

问题:
    1.前序遍历时,递归处的逻辑
    2.友元的条件


//*****************************源码************************************
//Tree.h
template<class T>//模板
struct TreeNode{
    TreeNode *firstchild;
    TreeNode *nextbrother;
    T data;
    TreeNode *ret_fir_child();//返回第一个子孩子结点
    TreeNode *ret_nex_brother();//返回下一个兄弟结点
};

template<class T>
class Tree{

public:
    TreeNode<T> *root;
    TreeNode<T> *curr;
public:
    Tree(){ root = curr = NULL; }
    ~Tree(){ Delete_node(root); }
    int Root();
    int Parent();
    int Child();
    int Brother();
    void Insert(T value);
    bool Delete_node(int i);
    bool Delete_child(int i);
    void Pre_show(TreeNode<T>* &t);
    void Pos_show(TreeNode<T>* &t);
    int Current(TreeNode<T> * &tree);
};



//Tree.cpp
#include"Tree.h"
template<class T>
int Tree<T>::Current(TreeNode<T> * &tree)
{
    if (tree == 0)
        return 0;
    curr = tree;
    cout << curr->data << endl;
    return 1;
}

template<class T>
TreeNode<T>* search(TreeNode<T>* &head, TreeNode<T>* &tail)
{

    if (head == NULL)
        return NULL;//返回NULL还是很有用的,可以把叶子的情况也包括进去。
    TreeNode* p = head->firstchild;
    if (head->firstchild == tail)
        return head;
    while (p->nextbrother != NULL){
        if (p->nextbrother == tail)
            return head;
        p = p->nextbrother;
    }
/*    while (p->firstchild != tail && p->firstchild != NULL){
        p = p;
        while (p->nextbrother != tail && p->nextbrother != NULL){
            p = p->nextbrother;
        }    
    }*///我这一段代码不对,我只考虑到了第一条子树,没有把所有的都考虑进去

    //前序遍历,
    if ((p = search(head->firstchild, tail)) != NULL)return p;//这一个也是有先后顺序的;是不是停不住??
    //不是,使能停住的。因为第一句的head?=NULL来判断的
    if ((p = search(head->nextbrother, tail)) != NULL)return p;
}

template<class T>
int Tree<T>::Root()
{
    if (root == NULL){
        curr = root;
        return 0;
    }
    return Current(root);
}

template<class T>
int Tree<T>::Parent()
{
    if (curr == NULL){
        curr = root;
        return 0;
    }
    TreeNode* p = search(root,curr);//从root开始找,一直找到curr
    if (p == NULL)return 0;
    else return Current(p);//把p变为current
}

template<class T>
int Tree<T>::Child()
{
    if ((curr == NULL) || (curr->firstchild == NULL))
        return 0;
    else
        return Current(curr->firstchild);

}

template<class T>
int Tree<T>::Brother()
{
    if ((curr == NULL) || curr->nextbrother == NULL)
        return 0;
    else
        return Current(curr->nextbrother);
}

template<class T>
void Tree<T>::Insert(T value)
{
    TreeNode<T>* newNode = new TreeNode<T>(value);//这个value是怎么插进去的
    if (root == NULL){
        root = curr = newNode;
        return;
    }
    if (curr->firstchild == NULL)
        curr->firstchild = newNode;
    else{
        TreeNode<T>* p = curr->firstchild;
        while (p->nextbrother != NULL)
            p = p->nextbrother;
        p->nextbrother = newNode;//插到当前结点的最后一个子节点中去
    }
    Current(newNode);
}

template<class T>
bool Tree<T>::Delete_node(int i)//删除当前结点的第i个孩子
{
    TreeNode<T>* r = NULL;
    if (i == 1){
        r = curr->firstchild;
        if (r == NULL)
            return 0;
        curr->firstchild = r->nextbrother;
    }
    else{
        int k = 1;
        TreeNode<T>* p = curr->firstchild;
        while ((p != NULL) && (k != i)){
            k++;
            r = p;
            p = p->nextbrother;
        }
        if (p != NULL)
            r->nextbrother = p->nextbrother;
        else
            return 0;
    }
}

template<class T>
bool Tree<T>::Delete_child(int i)
{
    if (root == NULL)return 0;
    TreeNode<T>* t = NULL, *p = root->firstchild;
    if (i == 1){
        if (p == NULL)
            return 0;
        root->firstchild = p->nextbrother;
    }
    else{
        int k = 1;
        while (p != NULL && k != i){
            k++;
            t = p;
            p = p->nextbrother;
        }
        if (p != NULL)
            r->nextbrother = p->nextbrother;
        else
            return 0;
    }
}

template<class T>
void Tree<T>::Pre_show(TreeNode<T>* &t)//当前结点开始,按前序排列来实现
{
    if (t == NULL)
        return;
    cout <<  t->data;
    if (t->firstchild != NULL)Pre_show(t->firstchild);//只有这个结束之后才会执行下一条语句
    if (t->nextbrother != NULL)Pre_show(t->nextbrother);
}

template<class T>
void Tree<T>::Pos_show(TreeNode<T>* &t)
{
/*    if (t == NULL)
        return;//返回值为void,return NULL;是不对的,NULL是有值的
    if (Pos_show(t->firstchild) == NULL)cout << setw(2) << t->data;
    if (Pos_show(t->nextbrother) == NULL)return;*///这段程序不对,原因是,void类型不能return NULL;且在结束的时候分不清。
    if (t == NULL)return;
    if (t->firstchild != NULL)Pos_show(t->firstchild);
    cout << t->data;
    if (t->nextbrother != NULL)Pos_show(t->nextbrother);
}



//main.cpp
#include<iostream>
#include"Tree.cpp"
using namespace std;

template<class T>
TreeNode<T>* init(T a)
{
    TreeNode<int>* nd = new TreeNode<int>[a];//定义了一个用于存储的

    nd[0].data = 0;
    nd[0].firstchild = &nd[1];
    nd[0].nextbrother = NULL;


    nd[1].data = 1;
    nd[1].firstchild = &nd[3];
    nd[1].nextbrother = &nd[2];

    nd[2].data = 2;
    nd[2].firstchild = &nd[5];
    nd[2].nextbrother = NULL;

    nd[3].data = 3;
    nd[3].firstchild = NULL;
    nd[3].nextbrother = &nd[4];

    nd[4].data = 4;
    nd[4].firstchild = NULL;
    nd[4].nextbrother = NULL;

    nd[5].data = 5;
    nd[5].firstchild = NULL;
    nd[5].nextbrother = &nd[6];

    nd[6].data = 6;
    nd[6].firstchild = NULL;
    nd[6].nextbrother = NULL;

    return nd;
}
int main()
{
    Tree<int>* family = new Tree<int>;
    int size = 7;
    TreeNode<int>* FML = init(size);//必须要用一个参数来
    family->root = FML;//
    family->curr = FML + 2;
    family->Child();
    family->Pos_show(FML);
    cout << endl;
    family->Pre_show(FML);
    system("pause");
    return 0;
}
0 0
原创粉丝点击