[완료] [C언어] AVL 트리 관련 질문 입니다.

lhs8421478의 이미지

요즘 C언어를 공부하고 있는 사람입니다.

AVL트리를 연습하고 있는데 책에서는 이중포인터를 쓰길래 좀 변형을 해서 작성을 해보았습니다.

그러다보니 궁금한게 몇가지 생기더군요... 우선 소스 입니다.

#include <stdio.h>
#include <stdlib.h>
#define max(a, b)	(((a) > (b)) ? (a) : (b))
 
typedef struct _node
{
	int data;
	struct _node *left_node;
	struct _node *right_node;
}tree_node;
 
tree_node *root;
 
tree_node *tree_ll(tree_node *p)
{
	tree_node *child;
 
	child = p->left_node;
	p->left_node = child->right_node;
	child->right_node = p;
	return child;
}
 
tree_node *tree_rr(tree_node *p)
{
	tree_node *child;
 
	child = p->right_node;
	p->right_node = child->left_node;
	child->left_node = p;
	return child;
}
 
tree_node *tree_lr(tree_node *p)
{
	tree_node *child;
 
	child = p->left_node;
	p->left_node = tree_rr(child);
	return tree_ll(p);
}
 
tree_node *tree_rl(tree_node *p)
{
	tree_node *child;
 
	child = p->right_node;
	p->right_node = tree_ll(child);
	return tree_rr(p);
}
 
int get_height(tree_node *node)
{
	int height;
 
	height = 0;
	if (node != NULL) {
		height = 1 + max(get_height(node->left_node), get_height(node->right_node));
	}
	return height;
}
 
int get_balance(tree_node *node)
{
	if (node == NULL) {
		return 0;
	}
	return get_height(node->left_node) - get_height(node->right_node);
}
 
tree_node *tree_balance(tree_node *node)
{
	int height;
 
	height = get_balance(node);
	if (height > 1) {
		if (get_balance(node->left_node) > 0) {
			printf("LL\n");
			node = tree_ll(node);
		}
		else {
			printf("LR\n");
			node = tree_lr(node);
		}
	}
	else if (height < -1) {
		if (get_balance(node->right_node) < 0) {
			printf("RR\n");
			node = tree_rr(node);
		}
		else {
			printf("RL\n");
			node = tree_rl(node);
		}
	}
	return node;
}
 
tree_node *tree_insert(tree_node *root,int data)
{
	if (root == NULL) {
		root = (tree_node*)malloc(sizeof(tree_node));
		if (root == NULL) {
			perror("메모리 할당 실패 \n");
			exit(-1);
		}
		root->data = data;
		root->left_node = root->right_node = NULL;
	}
	else if (root->data > data) {
		root->left_node = tree_insert(root->left_node,data);
		root = tree_balance(root);
	}
	else if (root->data < data) {
		root->right_node = tree_insert(root->right_node,data);
		root = tree_balance(root);
	}
	else {
		printf("중복된 data로 인하여 삽입 실패 \n");
	}
	return root;
}
 
void tree_print(tree_node *root)   //전위 순회로 구현
{
	if (root != NULL) {
		printf("%d ",root->data);
		tree_print(root->left_node);
		tree_print(root->right_node);
	}
}
 
tree_node *tree_search(tree_node *node, int data)
{
	if (node == NULL) {
		printf("data가 없습니다. \n");
		return;
	}
	if (data == node->data) {
		printf("%d \n",node->data);
		return node;
	}
	else if (data < node->data) {
		printf("left\n");
		return tree_search(node->left_node,data);
	}
	else if (data > node->data) {
		printf("right\n");
		return tree_search(node->right_node,data);
	}
}
 
int main()
{
 
	tree_search(root,54);
	root = tree_insert(root,57);
	root = tree_insert(root,58);
	root = tree_insert(root,59);
	root = tree_insert(root,55);
	root = tree_insert(root,54);
	tree_search(root,16);
	root = tree_insert(root,53);
	root = tree_insert(root,15);
	root = tree_insert(root,15);
	root = tree_insert(root,16);
	tree_search(root,16);
	root = tree_insert(root,18);
	root = tree_insert(root,21);
	root = tree_insert(root,99);
	root = tree_insert(root,87);
	root = tree_insert(root,64);
	tree_print(root);	
	printf("\n");
	tree_search(root,54);
 
	return 0;
}

소스코드가 좀 길어서 죄송합니다 ;;

각 함수들이 파라미터에서 node나 root를 받는데요, 파라미터에서 이것들을 받지 않고 data만 받아서 처리할수

있게 하는 방법이 없을까 해서 이것저것 뜯어 고쳐보고 있는데 도무지 안되네요...

도움좀 주셧으면 해서 이렇게 글을 올려봅니다...

익명 사용자의 이미지

재귀함수로 구현된 것에서 파라미터 값을 제외 하고 작성하신다고 하셨으니.

우신 재귀 구조 부터 바꾸셔야 될것 같네요..

루프문을 이용해서 재귀 구문부터 변경해 보시길..

변경하시면 가독성이 더떨어 질수도 있습니다.

lhs8421478의 이미지

상당히 복잡하더군요... 결국 처리는 했는데..... 소스는 드럽고

불필요한데도.. 안넣으면 에러뜨는 if문도 몇개 추가되고...

에효... 더 고쳐봐야죠뭐 ㅠㅠ

답변 감사합니다 ㅠㅠ

sacredone의 이미지

에테미션형 하이

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.