// mirror_of_search_tree.cpp : 定义控制台应用程序的入口点。
//二叉树的镜像实现,//
//二叉树的广度优先遍历//
//队列//
#include "stdafx.h"
#include "iostream"
#include "queue"
using namespace std;
struct BSTnode
{
int data;
int MaxLeft;
int MaxRight;
int maxLength;
BSTnode* left;
BSTnode* right;
};
template<typename T>
struct QueueNode
{
T data;
QueueNode* next;
};
template<typename T>
class Queue//first dequeue,last enqueue//队列//
{
public:
Queue()
{
first = NULL;
last = NULL;
}
~Queue()
{
first = NULL;
last = NULL;
}
bool isEmpty()
{
return (first == NULL);
}
void enqueue(T data)
{
QueueNode<T>* newnode;
newnode = new QueueNode<T>;
newnode->data = data;
newnode->next = NULL;
if (first == NULL)
{
first= newnode;
last = newnode;
}
else
{
last->next = newnode;
last = newnode;
}
}
T dequeue()
{
T x;
QueueNode<T>*p;
if(first == NULL)
cout<<"empty"<<endl;
else
{
x = first->data;
p = first;
if(first == last)
{
first = NULL;
last = NULL;
}
else
{
first = first->next;
delete p;
}
}
return x;
}
private:
QueueNode<T>*first;
QueueNode<T>*last;
};
class BSTtree
{
public:
BSTtree();
~BSTtree();
BSTtree(const BSTtree&);
void init_tree();
void insert_item(const int & data);
void print();
void mirror_of_search_tree();
void print_breadthFirst();
int max_lenth_of_tree();//树中两个节点的最大距离//
private:
void mirror(BSTnode*);
void print_inorder(BSTnode*p);
int max_lenth(BSTnode*pRoot);
void swap(BSTnode**l,BSTnode**s)
{
BSTnode*temp = NULL;
temp = *l;
*l = *s;
*s = temp;
}
BSTnode* root;
int MaxLenth_in_node;
};
BSTtree::BSTtree()
{
root = NULL;
MaxLenth_in_node = 0;
}
BSTtree::~BSTtree()
{
delete root;
root = NULL;
}
void BSTtree::init_tree()
{
root = NULL;
}
void BSTtree::insert_item(const int & data)//插入//
{
BSTnode* newnode = new BSTnode;
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
if (root == NULL)
root = newnode;
else
{
BSTnode* p = root;
BSTnode* trail = p;
while (p != NULL)
{
if(data < p->data)
{
trail = p;
p = p->left;
}
else if(data > p->data)
{
trail = p;
p = p->right;
}
}
if(trail->data > data)
trail->left = newnode;
else
trail->right = newnode;
//cout<<root->data<<" d"<<endl;
}
}
void BSTtree::print_inorder(BSTnode*root1)//中序遍历//
{
BSTnode* p = root1;
if(p != NULL)
{
print_inorder(p->left);
cout<<p->data<<" ";
print_inorder(p->right);
}
}
void BSTtree::print()
{
print_inorder(root);
}
void BSTtree::mirror(BSTnode*p)//二叉树镜像//
{
if(p == NULL)
return ;
else
{
swap(&(p->left),&(p->right));
mirror(p->left);
mirror(p->right);
}
}
void BSTtree::mirror_of_search_tree()
{
mirror(root);
}
void BSTtree::print_breadthFirst()//广度优先遍历//
{
Queue<BSTnode*> queue;
BSTnode* p = root;
if (p != NULL)
{
queue.enqueue(p);
while (!queue.isEmpty())
{
p = queue.dequeue();
cout<<p->data<<" ";
if(p->left != NULL)
queue.enqueue(p->left);
if(p->right != NULL)
queue.enqueue(p->right);
}
}
}
int BSTtree::max_lenth(BSTnode*pRoot)
{
if (pRoot == NULL)
return 0;
if (pRoot->left == NULL)
pRoot->MaxLeft = 0;
else
pRoot->MaxLeft = 1+ max_lenth(pRoot->left);
if (pRoot->right == NULL)
pRoot->MaxRight = 0;
else
pRoot->MaxRight = 1 + max_lenth(pRoot->right);
if (pRoot == root)
{
return pRoot->MaxLeft + pRoot->MaxRight;
}
else
{
pRoot->maxLength =( (pRoot->MaxLeft > pRoot->MaxRight)? pRoot->MaxLeft:pRoot->MaxRight);
return pRoot->maxLength;
}
}
int BSTtree::max_lenth_of_tree()//树中任意两个节点之间的最大距离//
{
//cout<<"max_lenth_of "<<root->data<<endl;
MaxLenth_in_node = max_lenth(root);
return MaxLenth_in_node;
}
void main()
{
BSTtree tree;
tree.init_tree();
tree.insert_item(10);
tree.insert_item(5);
tree.insert_item(15);
tree.insert_item(6);
tree.insert_item(20);
tree.insert_item(13);
tree.insert_item(2);
tree.insert_item(1);
//tree.print_breadthFirst();
//tree.mirror_of_search_tree();
tree.print();
cout<<endl;
cout<<tree.max_lenth_of_tree()<<endl;
}