이 진 트 리 의 C 언어 구현

5062 단어 이 진 트 리
거의http://blog.csdn.net/hopeyouknow/article/details/6740616그 중 미세한 부분 을 수정 해 운영 할 수 있 었 다.
bitree.h
typedef int Item;
typedef struct node
{
	struct node *lchild;
	struct node *rchild;
	Item	data;
}BiTNode, *BiTree;

BiTree InitBiTree(BiTNode *root);

BiTNode *MakeNode(Item item, BiTNode *lchild, BiTNode *rchild);

void FreeNode(BiTNode *pnode);

void ClearBiTree(BiTree tree);

void DestroyBiTree(BiTree tree);

int IsEmpty(BiTree tree);

int GetDepth(BiTree tree);

BiTree GetRoot(BiTree tree);

Item GetItem(BiTNode *pnode);

void SetItem(BiTNode *pnode, Item item);

BiTree SetLChild(BiTree parent, BiTree lchild);

BiTree SetRChild(BiTree parent, BiTree rchild);

BiTree GetLchild(BiTree tree);

BiTree GetRchild(BiTree tree);

BiTree InsertChild(BiTree parent, int lr, BiTree child);

void DeleteChild(BiTree parent, int lr);

void PreOrderTraverse(BiTree tree, void (*visit)());

void InOrderTraverse(BiTree tree, void (*visit)());

void PostOrderTraverse(BiTree tree, void (*visit)());

bitree.c
#include "bitree.h"
#include <malloc.h>
#include <stdlib.h>

BiTree InitBiTree(BiTNode *root)
{
	BiTree tree = root;
	return tree;
}

BiTNode *MakeNode(Item item, BiTNode *lchild, BiTNode *rchild)
{
	BiTNode *pnode = (BiTNode *)malloc(sizeof(BiTNode));
	if(pnode){
		pnode->data = item;
		pnode->lchild = lchild;	
		pnode->rchild = rchild;	
	}
	return pnode;
}

void FreeNode(BiTNode *pnode)
{
	if(pnode != NULL)
			free(pnode);
}

void ClearBiTree(BiTree tree)
{
	BiTNode *pnode = tree;
	if(pnode->lchild != NULL)
			ClearBiTree(pnode->lchild);

	if(pnode->rchild != NULL)
			ClearBiTree(pnode->rchild);

	FreeNode(pnode);
}

void DestroyBiTree(BiTree tree)
{
	if(tree)
			ClearBiTree(tree);
}

IsEmpty(BiTree tree)
{
	if(tree == NULL)
			return 0;
	else
			return 1;
}

int GetDepth(BiTree tree)
{
	int cd, ld, rd;
	cd = ld = rd = 0;
	if(tree){
		ld = GetDepth(tree->lchild);	
		rd = GetDepth(tree->rchild);	
		cd = (ld > rd ? ld : rd);
		return cd+1;
	}else
			return 0;
}

BiTree GetRoor(BiTree tree)
{
	return tree;
}

Item GetItem(BiTNode *pnode)
{
	return pnode->data;		
}

void SetItem(BiTNode *pnode, Item item)
{
	pnode->data = item;
}

BiTree SetLChild(BiTree parent, BiTree lchild)
{
	parent->lchild = lchild;
	return lchild;
}

BiTree SetRChild(BiTree parent, BiTree rchild)
{
	parent->rchild = rchild;
	return rchild;
}

BiTree GetLchild(BiTree tree)
{
	if(tree)
			return tree->lchild;
	return NULL;
}

BiTree GetRchild(BiTree tree)
{
	if(tree)
			return tree->rchild;
	return NULL;
}

BiTree InsertChild(BiTree parent, int lr, BiTree child)
{
	if(parent){
		if(lr == 0 && parent->lchild == NULL){
			parent->lchild = child;
			return child;	
		}	
		if(lr == 1 && parent->lchild == NULL){
			parent->rchild = child;
			return child;	
		}	
	}
	return NULL;
}

void DeleteChild(BiTree parent, int lr)
{
	if(parent){
		if(lr == 0 && parent->lchild != NULL){
			parent->lchild = NULL;
			FreeNode(parent->lchild);	
		}	
		if(lr == 1 && parent->rchild != NULL){
			parent->rchild = NULL;
			FreeNode(parent->rchild);	
		}	
	}
}

void PreOrderTraverse(BiTree tree, void (*visit)())
{
	BiTNode *pnode = tree;
	if(pnode){
		visit(pnode->data);
		PreOrderTraverse(pnode->lchild, visit);	
		PreOrderTraverse(pnode->rchild, visit);	
	}
}

void InOrderTraverse(BiTree tree, void (*visit)())
{
	BiTNode *pnode = tree;
	if(pnode){
		InOrderTraverse(pnode->lchild, visit);	
		visit(pnode->data);
		InOrderTraverse(pnode->lchild, visit);	
	}
}

void PostOrderTraverse(BiTree tree, void (*visit)())
{
	BiTNode *pnode = tree;
	if(pnode){
		PostOrderTraverse(pnode->lchild, visit);	
		PostOrderTraverse(pnode->lchild, visit);	
		visit(pnode->data);
	}
}

test.c
컴 파일: gcc test. c bitree. c
실행:. / a. out
실행 결과:
#include "bitree.h"
#include <stdio.h>

void print(Item item)
{
	printf("%d ", item);
}

main()
{
	BiTNode *n1 = MakeNode(10, NULL, NULL);
	BiTNode *n2 = MakeNode(20, NULL, NULL);
	BiTNode *n3 = MakeNode(30, n1, n2);
	BiTNode *n4 = MakeNode(40, NULL, NULL);
	BiTNode *n5 = MakeNode(50, NULL, NULL);
	BiTNode *n6 = MakeNode(60, n4, n5);
	BiTNode *n7 = MakeNode(70, NULL, NULL);

	BiTree tree = InitBiTree(n7);
	SetLChild(tree, n3);
	SetRChild(tree, n6);

	printf("the depth of the tree is %d.
", GetDepth(tree)); printf("preorderTraverse:
"); PreOrderTraverse(tree, print); putchar('
'); printf("inorderTraverse:
"); InOrderTraverse(tree, print); putchar('
'); printf("postorderTraverse:
"); PostOrderTraverse(tree, print); putchar('
'); DeleteChild(tree, 1); printf("postorderTraverse:
"); PostOrderTraverse(tree, print); DestroyBiTree(tree); if(IsEmpty(tree)) printf("the tree is empty!
"); return 0; }

좋은 웹페이지 즐겨찾기