본문내용
, *n5, *n6;
n1 = (tnode_str*)malloc(sizeof(tnode_str));
n2 = (tnode_str*)malloc(sizeof(tnode_str));
n3 = (tnode_str*)malloc(sizeof(tnode_str));
n4 = (tnode_str*)malloc(sizeof(tnode_str));
n5 = (tnode_str*)malloc(sizeof(tnode_str));
n6 = (tnode_str*)malloc(sizeof(tnode_str));
n1->data = 10;
n1->left_child = n2;
n1->right_child = n3;
n2->data = 20;
n2->left_child = n4;
n2->right_child = n5;
n3->data = 30;
n3->left_child = n6;
n3->right_child = NULL;
n4->data = 40;
n4->left_child = NULL;
n4->right_child = NULL;
n5->data = 50;
n5->left_child = NULL;
n5->right_child = NULL;
n6->data = 60;
n6->left_child = NULL;
n6->right_child = NULL;
root = n1;
inorder (root);
printf("\n");
free (n1);
free (n2);
free (n3);
free (n4);
free (n5);
free (n6);
}
8.다음의 후위와 전위 순회하는 함수를 5번 코드에 추가하여 트리에 존재하는 노드의 내용을 출력하라.
(1) 전위순회
#include
#include
#include
typedef struct tree_node
{
int data;
struct tree_node *left_child, *right_child;
} tnode_str;
tnode_str *root;
void inorder (tnode_str *ptr)
{
if (ptr)
{
printf("<%d>", ptr->data);
inorder (ptr->left_child);
inorder (ptr->right_child);
}
}
void main()
{
tnode_str *n1, *n2, *n3, *n4, *n5, *n6;
n1 = (tnode_str*)malloc(sizeof(tnode_str));
n2 = (tnode_str*)malloc(sizeof(tnode_str));
n3 = (tnode_str*)malloc(sizeof(tnode_str));
n4 = (tnode_str*)malloc(sizeof(tnode_str));
n5 = (tnode_str*)malloc(sizeof(tnode_str));
n6 = (tnode_str*)malloc(sizeof(tnode_str));
n1->data = 10;
n1->left_child = n2;
n1->right_child = n3;
n2->data = 20;
n2->left_child = n4;
n2->right_child = n5;
n3->data = 30;
n3->left_child = n6;
n3->right_child = NULL;
n4->data = 40;
n4->left_child = NULL;
n4->right_child = NULL;
n5->data = 50;
n5->left_child = NULL;
n5->right_child = NULL;
n6->data = 60;
n6->left_child = NULL;
n6->right_child = NULL;
root = n1;
inorder (root);
printf("\n");
free (n1);
free (n2);
free (n3);
free (n4);
free (n5);
free (n6);
}
(2) 후위순회
#include
#include
#include
typedef struct tree_node
{
int data;
struct tree_node *left_child, *right_child;
} tnode_str;
tnode_str *root;
void inorder (tnode_str *ptr)
{
if (ptr)
{
inorder (ptr->left_child);
inorder (ptr->right_child);
printf("<%d>", ptr->data);
}
}
void main()
{
tnode_str *n1, *n2, *n3, *n4, *n5, *n6;
n1 = (tnode_str*)malloc(sizeof(tnode_str));
n2 = (tnode_str*)malloc(sizeof(tnode_str));
n3 = (tnode_str*)malloc(sizeof(tnode_str));
n4 = (tnode_str*)malloc(sizeof(tnode_str));
n5 = (tnode_str*)malloc(sizeof(tnode_str));
n6 = (tnode_str*)malloc(sizeof(tnode_str));
n1->data = 10;
n1->left_child = n2;
n1->right_child = n3;
n2->data = 20;
n2->left_child = n4;
n2->right_child = n5;
n3->data = 30;
n3->left_child = n6;
n3->right_child = NULL;
n4->data = 40;
n4->left_child = NULL;
n4->right_child = NULL;
n5->data = 50;
n5->left_child = NULL;
n5->right_child = NULL;
n6->data = 60;
n6->left_child = NULL;
n6->right_child = NULL;
root = n1;
inorder (root);
printf("\n");
free (n1);
free (n2);
free (n3);
free (n4);
free (n5);
free (n6);
}
n1 = (tnode_str*)malloc(sizeof(tnode_str));
n2 = (tnode_str*)malloc(sizeof(tnode_str));
n3 = (tnode_str*)malloc(sizeof(tnode_str));
n4 = (tnode_str*)malloc(sizeof(tnode_str));
n5 = (tnode_str*)malloc(sizeof(tnode_str));
n6 = (tnode_str*)malloc(sizeof(tnode_str));
n1->data = 10;
n1->left_child = n2;
n1->right_child = n3;
n2->data = 20;
n2->left_child = n4;
n2->right_child = n5;
n3->data = 30;
n3->left_child = n6;
n3->right_child = NULL;
n4->data = 40;
n4->left_child = NULL;
n4->right_child = NULL;
n5->data = 50;
n5->left_child = NULL;
n5->right_child = NULL;
n6->data = 60;
n6->left_child = NULL;
n6->right_child = NULL;
root = n1;
inorder (root);
printf("\n");
free (n1);
free (n2);
free (n3);
free (n4);
free (n5);
free (n6);
}
8.다음의 후위와 전위 순회하는 함수를 5번 코드에 추가하여 트리에 존재하는 노드의 내용을 출력하라.
(1) 전위순회
#include
#include
#include
typedef struct tree_node
{
int data;
struct tree_node *left_child, *right_child;
} tnode_str;
tnode_str *root;
void inorder (tnode_str *ptr)
{
if (ptr)
{
printf("<%d>", ptr->data);
inorder (ptr->left_child);
inorder (ptr->right_child);
}
}
void main()
{
tnode_str *n1, *n2, *n3, *n4, *n5, *n6;
n1 = (tnode_str*)malloc(sizeof(tnode_str));
n2 = (tnode_str*)malloc(sizeof(tnode_str));
n3 = (tnode_str*)malloc(sizeof(tnode_str));
n4 = (tnode_str*)malloc(sizeof(tnode_str));
n5 = (tnode_str*)malloc(sizeof(tnode_str));
n6 = (tnode_str*)malloc(sizeof(tnode_str));
n1->data = 10;
n1->left_child = n2;
n1->right_child = n3;
n2->data = 20;
n2->left_child = n4;
n2->right_child = n5;
n3->data = 30;
n3->left_child = n6;
n3->right_child = NULL;
n4->data = 40;
n4->left_child = NULL;
n4->right_child = NULL;
n5->data = 50;
n5->left_child = NULL;
n5->right_child = NULL;
n6->data = 60;
n6->left_child = NULL;
n6->right_child = NULL;
root = n1;
inorder (root);
printf("\n");
free (n1);
free (n2);
free (n3);
free (n4);
free (n5);
free (n6);
}
(2) 후위순회
#include
#include
#include
typedef struct tree_node
{
int data;
struct tree_node *left_child, *right_child;
} tnode_str;
tnode_str *root;
void inorder (tnode_str *ptr)
{
if (ptr)
{
inorder (ptr->left_child);
inorder (ptr->right_child);
printf("<%d>", ptr->data);
}
}
void main()
{
tnode_str *n1, *n2, *n3, *n4, *n5, *n6;
n1 = (tnode_str*)malloc(sizeof(tnode_str));
n2 = (tnode_str*)malloc(sizeof(tnode_str));
n3 = (tnode_str*)malloc(sizeof(tnode_str));
n4 = (tnode_str*)malloc(sizeof(tnode_str));
n5 = (tnode_str*)malloc(sizeof(tnode_str));
n6 = (tnode_str*)malloc(sizeof(tnode_str));
n1->data = 10;
n1->left_child = n2;
n1->right_child = n3;
n2->data = 20;
n2->left_child = n4;
n2->right_child = n5;
n3->data = 30;
n3->left_child = n6;
n3->right_child = NULL;
n4->data = 40;
n4->left_child = NULL;
n4->right_child = NULL;
n5->data = 50;
n5->left_child = NULL;
n5->right_child = NULL;
n6->data = 60;
n6->left_child = NULL;
n6->right_child = NULL;
root = n1;
inorder (root);
printf("\n");
free (n1);
free (n2);
free (n3);
free (n4);
free (n5);
free (n6);
}
추천자료
c로배우는알고리즘에서 ROBOT프로그램을 C언어로 바꾸자.
C언어 야구 게임
C언어를 이용해 하노이탑 구현 프로그램
C언어를 이용한 분수의 덧셈과 곱셈을 하는 프로그램
C언어를 이용해 문자열을 추출하는 프로그램
C언어를 이용한 야구게임(숫자맞추기) 프로그램
C언어를 이용한 성적처리(성적관리) 프로그램
C언어를 이용한 [3][3]행렬(3*3행렬), 행렬의 덧셈과 행렬의 곱셈
C언어 처음공부하는 사람들에게 도움이 되는 코딩들(기초부터 포인터까지)
C언어 학생성적의 총점과 평균 구하는 프로그램
c언어로 만든 typedef 구조체 개념 프로그램
c언어 계산기
c언어로 애니메이션을 만들어 주세요
c언어에서의 포인터(pointer)
소개글