#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int head, tail;
typedef struct treenode
{
char data;
treenode *lchild;
treenode *rchild;
treenode *parent;
}node, *Tree;
node *q[100];
int n;
int m, k;
node *create_tree1(node *t)/// two branches
{
char a;
cin >> a;
if(a == '0')
return NULL;
t = new node;
t->data = a;
t->lchild = create_tree1(t->lchild);
t->rchild = create_tree1(t->rchild);
return t;
}
node *create_tree2(node *t)///three branches
{
queue<node *>q;
node *a;
t = create_tree1(t);
if(t)
{
t->parent = NULL;
while(!q.empty())
q.pop();
q.push(t);
while(!q.empty())
{
node *a = q.front();
q.pop();
if(a->lchild)
{
a->lchild->parent = a;
q.push(a->lchild);
}
if(a->rchild)
{
a->rchild->parent = a;
q.push(a->rchild);
}
}
}
return t;
}
void pre_order(node *t)
{
if(t)
{
cout << t->data;
pre_order(t->lchild);
pre_order(t->rchild);
}
}
void level_order(node *t)
{
if(t)
{
head = 0;
tail = 0;
q[tail++] = t;
while(head!=tail)
{
node *a;
a = q[head];
cout << a->data;
head++;
if(a->lchild)
{
q[tail++] = a->lchild;
}
if(a->rchild)
{
q[tail++] = a->rchild;
}
}
}
}
int leaves(node *t)
{
if(t)
{
if(t->lchild == NULL && t->rchild == NULL)
{
n++;
}
if(t->lchild)
{
leaves(t->lchild);
}
if(t->rchild)
{
leaves(t->rchild);
}
}
return n;
}
int depth(node *t)
{
int i, j;
if(t == NULL)
return 0;
if(t->lchild)
{
i = depth(t->lchild);
}
else
{
i = 0;
}
if(t->rchild)
{
j = depth(t->rchild);
}
else
{
j = 0;
}
return i>j?i+1:j+1;
}
bool is_Empty(Tree t)
{
if(t)
return false;
return true;
}
/*
node * destroy_tree1(Tree t)
{
if(t)
{
if(t->lchild)
destroy_tree(t->lchild);
if(t->rchild)
destroy_tree(t->rchild);
t = NULL;
delete t;
}
return t;
}*/
///销毁的时候写Tree t 和写 node *t 一样 都不能成功销毁 还得加返回值才行
void destroy_tree(Tree *t)
{
if(*t)
{
if((*t)->lchild)
destroy_tree(&(*t)->lchild);
if((*t)->rchild)
destroy_tree(&(*t)->rchild);
(*t) = NULL;
delete (*t);
}
}
node *point(node *t, char e)
{
if(t)
{
head = 0;
tail = 0;
q[tail++] = t;
while(head!=tail)
{
node *a = q[head];
head++;
if(a->data == e)
return a;
if(a->lchild)
q[tail++] = a->lchild;
if(a->rchild)
q[tail++] = a->rchild;
}
}
return NULL;
}
void insert_node(node *t, int LR, char e1, char e2)///插一个点算了
{
if(t)
{
node *q;
q = new node;
q->lchild = NULL;
q->rchild = NULL;
if(LR == 0)
{
node *p = point(t, e1);
q->data = e2;
node *tmp = p;
while(tmp->lchild)
{
tmp = tmp->lchild;
}
tmp->lchild = q;
}
if(LR == 1)
{
node *p = point(t, e1);
q->data = e2;
node *tmp = p;
while(tmp->rchild)
{
tmp = tmp->rchild;
}
tmp->rchild = q;
}
}
}
node* delete_node(node *t, int LR, node *p)
{
if(t)
{
if(LR == 0)
{
p->lchild = NULL;
}
else
{
p->rchild = NULL;
}
}
return t;
}
int main()
{
node *tree1, *tree2;
cout << "create a tree:" << endl;
/* cout << "二叉建立" << endl;
tree1 = create_tree1(tree1);
cout << "pre_order is:" << endl;
pre_order(tree1);
cout << endl;
cout << "level_order is:" << endl;
level_order(tree1);
cout << endl;*/
cout << "三叉建立" << endl;
tree2 = create_tree2(tree2);
cout << "level_order is:" << endl;
level_order(tree2);
cout << endl;
///接下来就是老朋友了
cout << "leaves number is:" << endl;
int sum = leaves(tree2);
cout << sum << endl;
cout << "the depth is:" << endl;
int d = depth(tree2);///*******************
cout << d << endl;
cout << "find xxx:" << endl;
node *p = point(tree2, 'e');
node *q;
cout << "find xxx's parent:" << endl;
if(p)
q = p->parent;
else
cout << "p is NULL" << endl;
if(q)
cout << q->data << endl;
else
cout << "q is NULL" << endl;///感觉也没方便太多
cout << "find xxx's leftsibling:" << endl;
node *s;
if(p!=q->lchild && q->lchild)
s = q->lchild;
cout << s->data << endl;
cout << "insert node ~" << endl;
insert_node(tree2, 1, 'b', 'g');
cout << "pre_order is:" << endl;
pre_order(tree2);
cout << endl;
cout << "delete child:" << endl;
/*node *ss;
ss->data = 'b';*这样是不行滴
node *ss;
ss = point(tree2, 'b');
tree2 = delete_node(tree2,1,ss);
cout << "pre_order is:" << endl;
pre_order(tree2);
cout << endl;
cout << "destroy tree:" << endl;
destroy_tree(&tree2);
cout << "IS EMPTY:" << endl;
if(is_Empty(tree2))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
/*
create a tree:
三叉建立
abd00e00c0f00
level_order is:
abcdef
leaves number is:
3
the depth is:
3
find xxx:
find xxx's parent:
b
find xxx's leftsibling:
d
insert node ~
pre_order is:
abdegcf
delete child:
pre_order is:
abdcf
destroy tree:
IS EMPTY:
YES
Process returned 0 (0x0) execution time : 7.131 s
Press any key to continue.
*/
三叉链表实现二叉树基本操作
最新推荐文章于 2024-04-20 12:16:51 发布