二叉树

头文件

#ifndef __BINTREE_H__
#define __BINTREE_H__

#define ElemType char
typedef struct BinTreeNode
{
    ElemType data;
    BinTreeNode *leftChild;
    BinTreeNode *rightChild;
}BinTreeNode;

typedef struct BinTree
{
    BinTreeNode *root;
    //ElemType refvalue;//stop value;
}BinTree;
#include"Stack.h"
#include<assert.h>
#include<iostream>

using namespace std;

//
//两个重名函数,一个负责接口,一个负责实现。
void InitBinTree(BinTree *t);
void CreatBinTree(BinTree *t);
void CreatBinTree(BinTreeNode *&t);
void CreatBinTree_1(BinTree *t);
BinTreeNode* CreatBinTree_1();
void CreatBinTree(BinTree *t,const char *str);
void CreatBinTree(BinTreeNode *&t,const char *&str);
//在这里我们实现了二叉树递归的前、中、后序遍历
void PreOrder(BinTree *t);
void PreOrder(BinTreeNode *t);
void InOrder(BinTree *t);
void InOrder(BinTreeNode *t);
void PostOrder(BinTree *t);
void PostOrder(BinTreeNode *t);
int Size(BinTree *t);
int Size(BinTreeNode *t);
int Height(BinTree *t);
int Height(BinTreeNode *t);
BinTreeNode* Find(BinTree *t,ElemType x);
BinTreeNode* Find(BinTreeNode *t,ElemType x);
BinTreeNode* LeftChild(BinTreeNode *t);
BinTreeNode* RightChild(BinTreeNode *t);
BinTreeNode* Parent(BinTree *t,ElemType x);
BinTreeNode* Parent(BinTreeNode *t,ElemType x);
BinTreeNode* Parent_1(BinTree *t,BinTreeNode *x);
BinTreeNode* Parent_1(BinTreeNode *t,BinTreeNode *x);
bool Equal(BinTree *t1,BinTree *t2);
bool Equal(BinTreeNode *t1,BinTreeNode *t2);
void Copy(BinTree *t,BinTree *t1);//Copy(t,t1);//t==>t1;
void Copy(BinTreeNode *t,BinTreeNode *&t1);
void ClearBinTree(BinTree *t);
void ClearBinTree(BinTreeNode *&t);
//

void InitBinTree(BinTree *t)
{
    t->root = NULL;
    //t->refvalue = '#';
}

void CreatBinTree(BinTree *t)
{
    CreatBinTree(t->root);
}

void CreatBinTree(BinTreeNode *&t)
{
    ElemType item;
    cin>>item;
    if(item == '#')
    {
        t = NULL;
    }
    else
    {
        t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert( t != NULL);
        t->data = item;
        CreatBinTree(t->leftChild);
        CreatBinTree(t->rightChild);
    }

}
void CreatBinTree_1(BinTree *t)
{
    t->root = CreatBinTree_1();
}

BinTreeNode* CreatBinTree_1()
{
    BinTreeNode *t;

    ElemType item;
    cin>>item;
    if(item == '#')
    {
        t = NULL;
    }
    else
    {
        t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert( t != NULL);
        t->data = item;
        t->leftChild = CreatBinTree_1();
        t->rightChild = CreatBinTree_1();
    }
    return t;
}
void CreatBinTree(BinTree *t,const char *str)
{
    CreatBinTree(t->root,str);
}
void CreatBinTree(BinTreeNode *&t,const char *&str)
{
    if(*str=='\0' ||  *str=='#')
    {
        t = NULL;
    }
    else
    {
        t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert( t != NULL);
        t->data = *str;
        CreatBinTree(t->leftChild,++str);
        CreatBinTree(t->rightChild,++str);
    }
}

void PreOrder(BinTree *t)
{
    PreOrder(t->root);
}
void PreOrder(BinTreeNode *t)
{
    if(t!=NULL)
    {
        cout<<t->data<<" ";
        PreOrder(t->leftChild);
        PreOrder(t->rightChild);
    }
}

void InOrder(BinTree *t)
{
    InOrder(t->root);
}
void InOrder(BinTreeNode *t)
{
    if(t!=NULL)
    {
        InOrder(t->leftChild);
        cout<<t->data<<" ";
        InOrder(t->rightChild);
    }
}
void PostOrder(BinTree *t)
{
    PostOrder(t->root);
}
void PostOrder(BinTreeNode *t)
{
    if(t!=NULL)
    {
        PostOrder(t->leftChild);
        PostOrder(t->rightChild);
        cout<<t->data<<" ";
    }
}
int Size(BinTree *t)
{
    return Size(t->root);
}

//int count = 0;
int Size(BinTreeNode *t)
{
    if(t == NULL)
    {
        return 0;
    }
    else
        return Size(t->leftChild)+ Size(t->rightChild)+1;
    /*
    if(t != NULL)
    {
    count++;
    Size(t->leftChild);
    Size(t->rightChild);
    }
    return count;
    */
}


int Height(BinTree *t)
{
    return Height(t->root);
}
int Height(BinTreeNode *t)
{
    if(t == NULL)
    {
        return 0;
    }
    else 
    {
        int left_height = Height(t->leftChild);
        int right_height = Height(t->rightChild);
        return (left_height > right_height ? left_height : right_height) + 1;
    }
}

BinTreeNode* LeftChild(BinTreeNode *t)
{
    if(t == NULL)
    {
        return NULL;
    }
    return t->leftChild;
}
BinTreeNode* RightChild(BinTreeNode *t)
{
    if(t == NULL)
    {
        return NULL;
    }
    return t->rightChild;
}

BinTreeNode* Find(BinTree *t,ElemType x)
{

    return Find(t->root,x);
}

BinTreeNode* Find(BinTreeNode *t,ElemType x)
{
    if(t == NULL)
    {
        return NULL;
    }
    if(t->data == x)
    {
        return t;
    }

    BinTreeNode *r = Find(t->leftChild,x);
    if(r != NULL)
    {
        return r;
    }
    return Find(t->rightChild,x);
}

BinTreeNode* Parent(BinTree *t,ElemType x)
{
    return Parent(t->root,x);
}
BinTreeNode* Parent(BinTreeNode *t,ElemType x)
{
    if(t == NULL)
    {
        return NULL;
    }
    BinTreeNode *p = Find(t,x);
    if(p == NULL)
    {
        return NULL;
    }
    if(t->leftChild == p || t->rightChild == p)
    {
        return t;
    }
    BinTreeNode *r = Parent(t->leftChild,x);
    if(r != NULL)
    {
        return r;
    }
    return Parent(t->rightChild,x);
}

BinTreeNode* Parent_1(BinTree *t,BinTreeNode *x)
{
    return Parent_1(t->root,x);
}
BinTreeNode* Parent_1(BinTreeNode *t,BinTreeNode *x)
{
    if(t==NULL || x == NULL || t==x)
    {
        return NULL;
    }

    if(t->leftChild == x || t->rightChild == x)
    {
        return t;
    }
    BinTreeNode *r = Parent_1(t->leftChild,x);
    if(r != NULL)
    {
        return r;
    }
    return Parent_1(t->rightChild,x);
}
bool Equal(BinTree *t1,BinTree *t2)
{
    return Equal(t1->root,t2->root);
}
bool Equal(BinTreeNode *t1,BinTreeNode *t2)
{
    if(t1==NULL && t2==NULL)
    {
        return true;
    }
    if(t1 != NULL && t2 != NULL && t1->data == t2->data && Equal(t1->leftChild,t2->leftChild) && Equal(t1->rightChild,t2->rightChild))
    {
        return true;
    }
    else
    {
        return false;
    }
}

void ClearBinTree(BinTree *t)
{
    ClearBinTree(t->root);
}
void ClearBinTree(BinTreeNode *&t)
{
    if(t!=NULL)
    {
        ClearBinTree(t->leftChild);
        ClearBinTree(t->rightChild);
        free(t);
        t = NULL;
    }
}

void Copy(BinTree *t,BinTree *t1)
{
    Copy(t->root,t1->root);
}
void Copy(BinTreeNode *t,BinTreeNode *&t1)
{
    if(t == NULL)
    {
        t1 = NULL;
    }
    else
    {
        t1 = (BinTreeNode*)malloc(sizeof(BinTreeNode));
        assert(t1 != NULL);
        t1->data = t->data;
        Copy(t->leftChild,t1->leftChild);
        Copy(t->rightChild,t1->rightChild);
    }
}


#endif

源文件

#include"BinTree.h"
#include<iostream>

using namespace std;



int main02()
{
    char *str = "ABC##DE##F##G#H##";
    //char *str = "ABDE##F##G#HK###C#JI###";
    BinTree bt;
    InitBinTree(&bt);
    CreatBinTree(&bt,str);
    cout<<"Size:"<<Size(&bt)<<endl;
    cout<<"Height:"<<Height(&bt)<<endl;
    BinTreeNode *p = Find(&bt,'E');
    system("pause");
    return 0;
}

int main01()
{
    char *str = "ABC##DE##F##G#H##";
    BinTree bt;
    InitBinTree(&bt);
    //CreatBinTree_1(&bt);
    CreatBinTree(&bt,str);
    cout<<"VLR:"<<endl;
    PreOrder(&bt);
    cout<<endl;
    cout<<"LVR:"<<endl;
    InOrder(&bt);
    cout<<endl;
    cout<<"LRV:"<<endl;
    PostOrder(&bt);
    cout<<endl;
    cout<<"Size:"<<Size(&bt)<<endl;
    //cout<<"Height:"<<Height(&bt)<<endl;
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值