二叉树

来源:互联网 发布:华为机顶盒安装软件 编辑:程序博客网 时间:2024/06/06 00:29
#include"My_Tree_wxx.h"






BinaryTree * Buy_Node()
{
BinaryTree *s = (BinaryTree *)malloc(sizeof(BinaryTree));
if (NULL == s)  exit (1);
memset(s, 0, sizeof(BinaryTree));
return s;
}
void  Free_Node(BinaryTree *phead)
{
free(phead);
}
void  PreOrder(BinaryTree *str)
{
while (str != NULL)
{
printf("%c ", str->data);
PreOrder(str->leftchild);
PreOrder(str->rightchild);
}
}
void  InOrder(BinaryTree *str)
{
while (NULL != str)
{
InOrder(str->leftchild);
printf("%c ", str->data);
InOrder(str->rightchild);
}
}
void PastOrder(BinaryTree *str)
{
while (NULL != str)
{
PastOrder(str->leftchild);
PastOrder(str->rightchild);
printf("%c ", str->data);
}
}
BinaryTree *Create_Tree1()
{
BinaryTree *s=NULL;
ElemType item;
scanf_s("%c", &item);
if (item != '#')
{
s=Buy_Node();
s->data = item;
s->leftchild = Create_Tree1();
s->rightchild = Create_Tree1();
}
return s;
}
////////////////////////////////
BinaryTree * CreateTree2(char *&str)
{
BtNode *s = NULL;
if (str != NULL && *str != '#')
{
s = Buynode();
s->data = *str;
s->leftchild = CreateTree2(++str);
s->rightchild = CreateTree2(++str);
}
return s;
}


BinaryTree * CreateTree3(char ** const pstr)
{
BtNode *s = NULL;
if (pstr != NULL && *pstr != NULL && **pstr != '#')
{
s = Buynode();
s->data = **pstr;
s->leftchild = CreateTree3(&++*pstr);
s->rightchild = CreateTree3(&++*pstr);
}
return s;
}
int FindIs(char *is, int n, ElemType x)
{
for (int i = 0; i<n; ++i)
{
if (is[i] == x)
return i;
}
return -1;
}
BinaryTree * Create(char *ps, char *is, int n)
{
BtNode *s = NULL;
if (n > 0)
{
s = Buynode();
s->data = ps[0];
int pos = FindIs(is, n, ps[0]);
if (pos == -1) exit(1);
s->leftchild = Create(ps + 1, is, pos);
s->rightchild = Create(ps + pos + 1, is + pos + 1, n - pos - 1);
}
return s;
}
BinaryTree * CreatePI(char *ps, char *is)
{
if (ps == NULL || is == NULL)
{
return NULL;
}
else
{
int n = strlen(ps);
return Create(ps, is, n);
}
}
BinaryTree * Create2(char *is, char *ls, int n)
{
BtNode *s = NULL;
if (n > 0)
{
int pos = FindIs(is, n, ls[n - 1]);
if (pos == -1) exit(1);
s = Buynode();
s->data = ls[n - 1];
s->leftchild = Create2(is, ls, pos);
s->rightchild = Create2(is + pos + 1, ls + pos, n - pos - 1);
}
return s;
}
BinaryTree * CreateIL(char *is, char *ls, int n)
{
if (is == NULL || ls == NULL || n < 1)
return NULL;
else
return Create2(is, ls, n);
}


BinaryTree * FindValue(BtNode *ptr, ElemType x)
{
if (ptr == NULL || ptr->data == x)
{
return ptr;
}
else
{
BtNode *p = FindValue(ptr->leftchild, x);
if (NULL == p)
{
p = FindValue(ptr->rightchild, x);
}
return p;
}
}


BinaryTree * Parent(BtNode *ptr, BtNode *child)
{
if (ptr == NULL || ptr->leftchild == child || ptr->rightchild == child)
{
return ptr;
}
else
{
BtNode *p = Parent(ptr->leftchild, child);
if (NULL == p)
{
p = Parent(ptr->rightchild, child);
}
return p;
}
}
BinaryTree * FindParent(BtNode *ptr, BtNode *child)
{
if (ptr == NULL || child == NULL || ptr == child)
{
return NULL;
}
else
{
return Parent(ptr, child);
}
}
BinaryTree * FindNearParent(BtNode *ptr, BtNode *child1, BtNode *child2)
{
if (ptr == NULL || child1 == NULL || child2 == NULL ||
child1 == ptr || child1 == ptr)
{
return NULL;
}
else
{
return NULL;
//return NearParent(ptr,child1,child2);
}
}


int SizeLeaf(BtNode *ptr)
{
if (ptr == NULL)
return 0;
else if (ptr->leftchild == NULL && ptr->rightchild == NULL)
{
return 1;
}
else
{
return SizeLeaf(ptr->leftchild) + SizeLeaf(ptr->rightchild);
}
}
int Size(BtNode *ptr)
{
if (ptr == NULL)
return 0;
else
return 1 + Size(ptr->leftchild) + Size(ptr->rightchild);
}


int Depth(BtNode *ptr)
{
if (ptr == NULL)
{
return 0;
}
else
{
return Max(Depth(ptr->leftchild), Depth(ptr->rightchild)) + 1;
}
}
bool Equal(BtNode *pa, BtNode *pb)
{
return (pa == NULL && pb == NULL) ||
(pa != NULL && pb != NULL && pa->data == pb->data &&
Equal(pa->leftchild, pb->leftchild) &&
Equal(pa->rightchild, pb->rightchild));
}


void NicePerOrder(BtNode *ptr)
{
if (ptr == NULL) return; // if(NULL == ptr) return ;
Stack st;
Init_Stack(&st);
push(&st, ptr);
while (!empty(&st))
{
ptr = top(&st); pop(&st);
printf("%c ", ptr->data);
if (ptr->rightchild != NULL)
push(&st, ptr->rightchild);
if (ptr->leftchild != NULL)
push(&st, ptr->leftchild);
}
}


void NiceInOrder(BtNode *ptr)
{
if (ptr == NULL) return;
Stack st; // BtNode *;
Init_Stack(&st);


while (ptr != NULL || !empty(&st))
{
while (ptr != NULL)
{
push(&st, ptr);
ptr = ptr->leftchild;
}
ptr = top(&st); pop(&st);
printf("%c ", ptr->data);
ptr = ptr->rightchild;
}
}


void NicePastOrder(BtNode *ptr)
{
if (ptr == NULL) return;
Stack st; // BtNode *;
Init_Stack(&st);
BtNode *tag = NULL;


while (ptr != NULL || !empty(&st))
{
while (ptr != NULL)
{
push(&st, ptr);
ptr = ptr->leftchild;
}
ptr = top(&st); pop(&st);
if (ptr->rightchild == NULL || ptr->rightchild == tag)
{
printf("%c ", ptr->data);
tag = ptr;
ptr = NULL;
}
else
{
push(&st, ptr);
ptr = ptr->rightchild;
}
}
}
void main()
{
BinaryTree *root = NULL;
root = Create_Tree1();
PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PastOrder(root);
printf("\n");
}