码迷,mamicode.com
首页 > 其他好文 > 详细

求二叉树深度和copy二叉树

时间:2015-07-19 10:16:47      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:二叉树   深度   copy   

// operatorTree.cpp
// 对树的操作

#include <iostream>
#include <cstdio>

// 二叉树表示法
typedef struct BiTNode
{
	int	data;
	struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

// 中序遍历
void inOrder(BiTNode *T)
{
	if (T == NULL) {
		return;
	}

	inOrder(T->lchild);

	printf("%d ", T->data);

	inOrder(T->rchild);
}

// 计算树的高度
int depthTree(BiTNode *T)
{
	if (!T) {
		return 0;
	}

	int depthVal = 0;
	int depthLeft = 0, depthRight = 0;

	depthLeft = depthTree(T->lchild);
	depthRight = depthTree(T->rchild);

	depthVal = 1 + (depthLeft > depthRight ? depthLeft : depthRight);
	return depthVal;
}

// copy二叉树
BiTNode* copyTree(BiTNode *T)
{
	if (T == NULL) {
		return NULL;
	}

	BiTNode *newNode = (BiTNode *)malloc(sizeof(BiTNode));
	if (newNode == NULL) {
		free(newNode);
		return NULL;
	}
	newNode->data = T->data;
	newNode->lchild = copyTree(T->lchild);
	newNode->rchild = copyTree(T->rchild);

	return newNode;
}

void operatorTree()
{
	BiTNode nodeA, nodeB, nodeC, nodeD, nodeE;

	memset(&nodeA, 0, sizeof(BiTNode));
	memset(&nodeB, 0, sizeof(BiTNode));
	memset(&nodeC, 0, sizeof(BiTNode));
	memset(&nodeD, 0, sizeof(BiTNode));
	memset(&nodeE, 0, sizeof(BiTNode));

	nodeA.data = 1;
	nodeB.data = 2;
	nodeC.data = 3;
	nodeD.data = 4;
	nodeE.data = 5;

	nodeA.lchild = &nodeB;
	nodeA.rchild = &nodeC;
	nodeB.lchild = &nodeD;
	nodeC.lchild = &nodeE;

	int depth = depthTree(&nodeA);
	printf("Depth: %d\n", depth);
	// Depth: 3

	BiTNode *newTree = NULL;
	newTree = copyTree(&nodeA);
	printf("inorder traversal new tree.\n");
	inOrder(&nodeA);
	// 4 2 1 5 3
	printf("\n");
}

int main()
{
	operatorTree();

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

求二叉树深度和copy二叉树

标签:二叉树   深度   copy   

原文地址:http://blog.csdn.net/zyq522376829/article/details/46946161

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!