#include <stdio.h>//writer 5396952 이현민
#include <stdlib.h>
#include <memory.h>
#define MAX_QUEUE_SIZE 100
typedef struct treeNode{
int data;
struct treeNode *left;
struct treeNode *right;
} treeNode;
//the names are up to the order when it checks current node's data
typedef treeNode *element;
typedef struct {
element queue[MAX_QUEUE_SIZE];
int front;
int rear;
}queueType;
void error(char *message){
fprintf(stderr,"%s\n", message);
exit(1);
}
void init(queueType *q)
{
q->front = q->rear = 0;
}
int isEmpty(queueType *q)
{
return (q->front == q->rear);
}
int isFull(queueType *q)
{
return ( (q->rear+1) % (MAX_QUEUE_SIZE) == q->front);
//checking if front is ascended rear = means rear arrived to unused queue cell, full
}
void enqueue(queueType *q ,element item)
{
if(isFull(q))
{
error("Queue is full\n");
}
q->rear = (q->rear+1) % MAX_QUEUE_SIZE;//simply it means next cell
q->queue[q->rear] = item;
}
element dequeue(queueType *q)
{
if(isEmpty(q))//get into the block if the result is true
{
error("Queue is empty\n");
}
q->front = (q->front+1) % MAX_QUEUE_SIZE;//means move front index to next cell in circular queue
return q->queue[q->front];//?????????????????
}
element peek(queueType *q)
{
if(isEmpty(q))
{
error("Queue is empty\n");
}
return q->queue[(q->front+1) % MAX_QUEUE_SIZE];//?????????????????
}
//////////////////////////order
void inOrder(treeNode *root){
if (root){//donno yet exactly what it means
inOrder(root->left);//access to the most lefted node first
printf("%d", root->data);
//then print from the highest level's left node and where the tracker visited
inOrder(root->right);//before it goes back where it was, print right node
}
}
void preOrder(treeNode *root){
if(root){
printf("%d", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void postOrder(treeNode *root){
if(root){
postOrder(root->left);
postOrder(root->right);
printf("%d", root->data);
}
}
treeNode n1 ={1, NULL, NULL};
treeNode n2 ={4, &n1, NULL};
treeNode n3 ={16, NULL, NULL};
treeNode n4 ={25, NULL, NULL};
treeNode n5 ={20, &n3, &n4};
treeNode n6 ={15, &n2, &n5};
treeNode *root = &n6;
void levelOrder(treeNode *tracker)//should be root node
{
queueType q;
init(&q);
if(root == NULL)
return ;
enqueue(&q, tracker);
while(!isEmpty(&q)){
tracker = dequeue(&q);
printf("%d", tracker->data); // root node
if(tracker->left)//if it is not a null
{
enqueue(&q, tracker->left );
}
if(tracker->right)//if it is not a null
{
enqueue(&q, tracker->right );
}
}
}
/*
15
4 20
1 16 25
*/
void main(){
printf("inOrder\n");
inOrder(root);
printf("\n\npreOrder\n");
preOrder(root);
printf("\n\npostOrder\n");
postOrder(root);
printf("\n\nlevelOrder\n");
levelOrder(root);
}/*don't sleep on me im always working*/
#include <stdlib.h>
#include <memory.h>
#define MAX_QUEUE_SIZE 100
typedef struct treeNode{
int data;
struct treeNode *left;
struct treeNode *right;
} treeNode;
//the names are up to the order when it checks current node's data
typedef treeNode *element;
typedef struct {
element queue[MAX_QUEUE_SIZE];
int front;
int rear;
}queueType;
void error(char *message){
fprintf(stderr,"%s\n", message);
exit(1);
}
void init(queueType *q)
{
q->front = q->rear = 0;
}
int isEmpty(queueType *q)
{
return (q->front == q->rear);
}
int isFull(queueType *q)
{
return ( (q->rear+1) % (MAX_QUEUE_SIZE) == q->front);
//checking if front is ascended rear = means rear arrived to unused queue cell, full
}
void enqueue(queueType *q ,element item)
{
if(isFull(q))
{
error("Queue is full\n");
}
q->rear = (q->rear+1) % MAX_QUEUE_SIZE;//simply it means next cell
q->queue[q->rear] = item;
}
element dequeue(queueType *q)
{
if(isEmpty(q))//get into the block if the result is true
{
error("Queue is empty\n");
}
q->front = (q->front+1) % MAX_QUEUE_SIZE;//means move front index to next cell in circular queue
return q->queue[q->front];//?????????????????
}
element peek(queueType *q)
{
if(isEmpty(q))
{
error("Queue is empty\n");
}
return q->queue[(q->front+1) % MAX_QUEUE_SIZE];//?????????????????
}
//////////////////////////order
void inOrder(treeNode *root){
if (root){//donno yet exactly what it means
inOrder(root->left);//access to the most lefted node first
printf("%d", root->data);
//then print from the highest level's left node and where the tracker visited
inOrder(root->right);//before it goes back where it was, print right node
}
}
void preOrder(treeNode *root){
if(root){
printf("%d", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void postOrder(treeNode *root){
if(root){
postOrder(root->left);
postOrder(root->right);
printf("%d", root->data);
}
}
treeNode n1 ={1, NULL, NULL};
treeNode n2 ={4, &n1, NULL};
treeNode n3 ={16, NULL, NULL};
treeNode n4 ={25, NULL, NULL};
treeNode n5 ={20, &n3, &n4};
treeNode n6 ={15, &n2, &n5};
treeNode *root = &n6;
void levelOrder(treeNode *tracker)//should be root node
{
queueType q;
init(&q);
if(root == NULL)
return ;
enqueue(&q, tracker);
while(!isEmpty(&q)){
tracker = dequeue(&q);
printf("%d", tracker->data); // root node
if(tracker->left)//if it is not a null
{
enqueue(&q, tracker->left );
}
if(tracker->right)//if it is not a null
{
enqueue(&q, tracker->right );
}
}
}
/*
15
4 20
1 16 25
*/
void main(){
printf("inOrder\n");
inOrder(root);
printf("\n\npreOrder\n");
preOrder(root);
printf("\n\npostOrder\n");
postOrder(root);
printf("\n\nlevelOrder\n");
levelOrder(root);
}/*don't sleep on me im always working*/
댓글
댓글 쓰기