두 갈래 나무의 뿌리 노드를 입력하여 이 나무의 깊이를 구하다

5282 단어
알고리즘 설명:
두 갈래 나무의 뿌리 노드를 입력하여 이 나무의 깊이를 구하고 뿌리 노드에서 잎 노드까지 순서대로 지나가는 결점은 하나의 경로를 형성하며 가장 긴 경로의 길이는 나무의 깊이이다
알고리즘 구현:
/*************************************************************************
	> File Name: main.c
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016 04 14    21 04 09 
 ************************************************************************/

#include "TreeDepth.h"
#include "BinaryTree.h"
void Test(struct BinaryTreeNode *pRoot, int expected)
{
	int result = TreeDepth(pRoot);
	if (result == expected)
		printf("test pass!
"); else printf("test fail!
"); } void Test1() { printf("begin:
"); struct BinaryTreeNode *pNode1 = CreateBinaryTreeNode(1); struct BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2); struct BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3); struct BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4); struct BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5); struct BinaryTreeNode *pNode6 = CreateBinaryTreeNode(6); struct BinaryTreeNode *pNode7 = CreateBinaryTreeNode(7); ConnectTreeNodes(pNode1, pNode2, pNode3); ConnectTreeNodes(pNode2, pNode4, pNode5); ConnectTreeNodes(pNode3, NULL, pNode6); ConnectTreeNodes(pNode5, pNode7, NULL); Test(pNode1, 4); DestroyTree(pNode1); printf("end!
"); } int main() { Test1(); return 0; }
/*************************************************************************
	> File Name: Tree.h
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016 04 14    20 34 23 
 ************************************************************************/

#ifndef _TREE_H
#define _TREE_H
#include <stdio.h>
#include <stdlib.h>
struct BinaryTreeNode
{
	int value;
	struct BinaryTreeNode *left;
	struct BinaryTreeNode *right;
};
struct BinaryTreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight);
void PrintTreeNode(struct BinaryTreeNode *pNode);
void PrintTree(struct BinaryTreeNode *pRoot);
void DestroyTree(struct BinaryTreeNode *pRoot);

#endif
/*************************************************************************
	> File Name: Tree.c
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016 04 14    20 38 43 
 ************************************************************************/

#include "BinaryTree.h"
struct BinaryTreeNode *CreateBinaryTreeNode(int value)
{
	struct BinaryTreeNode *pNode = (struct BinaryTreeNode *)malloc(sizeof(struct BinaryTreeNode));
	pNode->value = value;
	pNode->left = NULL;
	pNode->right = NULL;
	return pNode;
}

void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight)
{
	if (pRoot != NULL)
	{
		pRoot->left = pLeft;
		pRoot->right = pRight;
	}
}
void PrintTreeNode(struct BinaryTreeNode *pNode)
{
	if (pNode != NULL)
	{
		printf("the value of this node is :%d
", pNode->value); if (pNode->left != NULL) { printf("the value of left child is %d
",pNode->left->value); } else { printf("the left child is NULL
"); } if (pNode->right != NULL) { printf("the value of right child is %d
",pNode->right->value); } else { printf("the right child is NULL
"); } } printf("
"); } void PrintTree(struct BinaryTreeNode *pRoot) { PrintTreeNode(pRoot); if (pRoot != NULL) { if (pRoot->left != NULL) PrintTree(pRoot->left); if (pRoot->right != NULL) PrintTree(pRoot->right); } } void DestroyTree(struct BinaryTreeNode *pRoot) { if (pRoot != NULL) { struct BinaryTreeNode *pLeft = pRoot->left; struct BinaryTreeNode *pRight = pRoot->right; free(pRoot); pRoot == NULL; DestroyTree(pLeft); DestroyTree(pRight); } }
/*************************************************************************
	> File Name: TreeDepth.h
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016 04 14    20 55 04 
 ************************************************************************/

#ifndef _TREEDEPTH_H
#define _TREEDEPTH_H
#include "BinaryTree.h"
int TreeDepth(struct BinaryTreeNode *pRoot);

#endif
/*************************************************************************
	> File Name: TreeDepth.c
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016 04 14    20 58 54 
 ************************************************************************/
#include "BinaryTree.h"
#include "TreeDepth.h"
/*
 *  , , , 
 * */
int TreeDepth(struct BinaryTreeNode *pRoot)
{
	if ( pRoot == NULL )
		return 0;
	int numLeft = TreeDepth(pRoot->left);
	int numRight = TreeDepth(pRoot->right);

	return (numLeft > numRight)?(numLeft + 1):(numRight + 1);
}
CC = gcc
CFLAGS = -g -O2 -Wall

%.o:%.c
	$(CC) -o $@ -c $(CFLAGS) $<

main:main.o TreeDepth.o BinaryTree.o
	$(CC) main.o TreeDepth.o BinaryTree.o -o main $(CFLAGS)

clean:
	rm -rf *.o main

좋은 웹페이지 즐겨찾기