二叉树的操作

#include "stdafx.h"
#include <iostream>
#include <cassert>
#include <stack>
using namespace std;

struct Node
{
	int element;
	Node *lChild;
	Node *rChild;
	Node(int ele = 0, Node *left = NULL, Node *right = NULL)
		:element(ele), lChild(left), rChild(right){}
};

/************************************************************************/
//函数功能:创建二叉树
/************************************************************************/
Node* CreateBinaryTree(int arr[], int curPos, int n)
{
	assert(n >= 1);
	
	Node *root = new Node(arr[curPos]);

	if (2*curPos + 1 < n)
		root->lChild = CreateBinaryTree(arr, 2*curPos + 1, n);
	if (2*curPos + 2 < n)
		root->rChild = CreateBinaryTree(arr, 2*curPos + 2, n);

	return root;
}
/************************************************************************/
//函数功能:中序遍历的非递归版本
/************************************************************************/
void MiddleOrderPrint(Node *root)
{
	if (NULL == root)
		return;
	
	stack<Node*> s;
	
	Node *currentNode = root;
	while (true)
	{
		while (NULL != currentNode)
		{
			s.push(currentNode);
			currentNode = currentNode->lChild;
		}

		if (s.empty() == true)
			return;
		
		currentNode = s.top();
		cout<<currentNode->element<<" ";
		s.pop();
		currentNode = currentNode->rChild;
	}
}

/************************************************************************/
//函数功能:生成二叉树的镜像
/************************************************************************/
Node* MirrorBinaryTree(Node *oldTree)
{
	if(oldTree == NULL)
		return NULL;

	Node *newTree = new Node(oldTree->element);

	Node *temp = NULL;

	//swap the left and right subtree
	//temp = oldTree->lChild;
	//oldTree->lChild = oldTree->rChild;
	//oldTree->rChild = temp;

	newTree->lChild = MirrorBinaryTree(oldTree->rChild);
	newTree->rChild = MirrorBinaryTree(oldTree->lChild);

	return newTree;
}
/************************************************************************/
//函数功能:拷贝二叉树
/************************************************************************/
Node* CopyBinaryTree(Node* root)
{
	if (NULL == root)
		return NULL;
	
	Node *newTree = NULL;
	newTree = new Node(root->element);

	newTree->lChild = CopyBinaryTree(root->lChild);
	newTree->rChild = CopyBinaryTree(root->rChild);
	return newTree;
}

/************************************************************************/
//函数功能:判断2个二叉树是否相等
/************************************************************************/
bool isEqual(Node *root1, Node *root2)
{
	if (root1 == NULL && root2 == NULL)
		return true;
	
	bool a = (root1 != NULL);
	bool b = (root2 != NULL);
	bool e = (root2->element == root1->element);
	return (a && b && e && isEqual(root1->lChild, root2->lChild) && isEqual(root1->rChild, root2->rChild));
}

struct bit
{
	int  b1 : 5;
	int :2;
	int b2 : 2;
};
int main()
{
	const int N = 10;
	int arr[N] = {0};
	for (int i = 0; i < N; ++i)
		arr[i] = i + 1;

	Node *root = CreateBinaryTree(arr, 0, N);
	
	MiddleOrderPrint(root);
	cout<<endl;


	Node *mirrorTree = MirrorBinaryTree(root);
	MiddleOrderPrint(mirrorTree);
	cout<<endl;

	Node *copyTree = CopyBinaryTree(root);
	MiddleOrderPrint(copyTree);
	cout<<endl;

	if (isEqual(root, copyTree))
		cout<<"Equal!"<<endl;
	else
		cout<<"Not equal!"<<endl;

	bit b;
	cout<<sizeof(b)<<endl;
	memcpy(&b, "EMC EXAMINATION", sizeof(b));

	printf("%d,%d\n", b.b1, b.b2);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值