#include <stdio.h>
#include <stdlib.h>
#define MaxQueueSize 20
typedef char Data;
typedef struct Node
{
Data sj;
struct Node* rchild;
struct Node* lchild;
}Tree;
typedef struct
{
Data queue[MaxQueueSize];
int rear; //隊尾指標
int front; //隊頭指標
int count; //計數器
}SeqCQueue;
void QueueInitiate(SeqCQueue* Q)
{
Q->rear = 0;
Q->front = 0;
Q->count = 0;
}
void QueueAppend(SeqCQueue* Q, Data x)
{
if (Q->count > 0 && Q->rear == Q->front)
{
printf("佇列已滿無法插入! \n");
//return 0;
}
else
{
Q->queue[Q->rear] = x;
Q->rear = (Q->rear + 1) % MaxQueueSize;
Q->count++;
//return 1;
}
}
void QueueDelete(SeqCQueue* Q, Data* d)
{
if (Q->count == 0)
{
printf("佇列已空無資料元素出佇列! \n");
//return 0;
}
else
{
*d = Q->queue[Q->front];
Q->front = (Q->front + 1) % MaxQueueSize;
Q->count--;
//return 1;
}
}
//初始化建立二叉樹的頭結點
void Initiate(Tree** root)
{
*root = (Tree*)malloc(sizeof(Tree));
(*root)->lchild = NULL;
(*root)->rchild = NULL;
}
Tree* InsertLeftNode(Tree* curr, DataTree* InsertLeftNode(Tree* curr, Data x)
{
Tree* s, *t;
if (curr == NULL)
return NULL;
t = curr->lchild; //保存原curr所指結點的左子樹指標
s = (Tree*)malloc(sizeof(Tree));
s->sj = x;
s->lchild = t; //新插入結點的左子樹為原curr的左子樹
s->rchild = NULL;
curr->lchild = s; //新結點為curr的左子樹
return curr->lchild;
}
Tree* InsertRightNode(Tree* curr, Data x)
{
Tree* s, *t;
if (curr == NULL) return NULL;
t = curr->rchild; //保存原curr所指結點的右子樹指標
s = (Tree*)malloc(sizeof(Tree));
s->sj = x;
s->rchild = t; //新插入結點的右子樹為原curr的右子樹
s->lchild = NULL;
curr->rchild = s; //新結點為curr的右子樹
return curr->rchild;
}
int chanci(Tree* tree, SeqCQueue* q, Data* d)
{
if ((tree->sj )== NULL)
{
return 0;
}
else
{
//void QueueAppend(SeqCQueue * Q, Data x);
QueueAppend(q, tree->sj);
//void QueueDelete(SeqCQueue * Q, Data * d);
QueueDelete(q, d);
printf("%c ", *d);int temp1;
temp1 = chanci(tree->lchild, q, d);
int temp2;
temp2 = chanci(tree->rchild, q, d);
return 1 + temp1+ temp2;
}
}
//輸出樹T的深度
int deep(Tree* tree)
{
if (tree == NULL || tree->sj == NULL)
{
return 0;
}
else
{
return 1 + deep(tree->lchild);
}
}
//葉子
int yezi(Tree* tree)
{
if (tree == NULL || tree->sj == NULL)
{
return 1;
}
else
{
return 0 + yezi(tree->lchild) + yezi(tree->rchild);
}
}
int noyezi(Tree* tree)
{
if (tree->sj = NULL)
{
return 0;
}
else
{
return 1 + yezi(tree->lchild) + yezi(tree->rchild);
}
}
int main()
{
//建立一個二叉樹
Tree* root, *p;
SeqCQueue* Q;
SeqCQueue Qdate;
Q = &Qdate;
Data d;
void Initiate(Tree **root);
void QueueInitiate(SeqCQueue * Q);
QueueInitiate(Q);
Initiate(&root);
root->sj = ' ';
p = InsertLeftNode(root, 'A');
p = InsertLeftNode(p, 'B');
InsertRightNode(p, 'E');
InsertRightNode(p->rchild, 'K');
InsertLeftNode(p->rchild, 'J');
p = InsertLeftNode(p, 'D');
InsertLeftNode(p, 'H');
p = InsertRightNode(p, 'I');
p = InsertRightNode(root->lchild, 'C');
InsertLeftNode(p, 'F');
InsertRightNode(p, 'G');
printf("%c\n", root->lchild->sj);
printf("%c\n", root->lchild->lchild->sj);
//層次遍歷
int chanci(Tree *tree, SeqCQueue *q, Data *d);
int n;
n = chanci(root->lchild, Q, &d);
printf("%d\n", n);
/*//輸出樹T的深度;
int deep(Tree * tree);
int m;
m = deep(root->lchild);
printf("深%d\n", m);
//輸出樹T的葉子結點和非葉子結點
int yezi(Tree * tree);
int noyezi(Tree * tree);
int y, no;
y = yezi(root->lchild);
printf("葉子%d ", y);
no = noyezi(root->lchild);
printf("非葉子%d ", no);*/
}
在函式chanci那tree->sj有個nulltrp,不會解決,還是遞回沒有把所以的遞回完

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/284816.html
標籤:C語言
上一篇:秋梨膏