#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);
}
二叉树的操作
最新推荐文章于 2022-11-17 09:59:53 发布