#include
#include
#include
#include
#include
using namespace std;
typedef struct node* tree;
struct node {
int data;
tree left;
tree right;
};
int n=10;
int a[10000], b[10000];
tree bintree(int prel, int prer, int inl,int inr)
{ //(int postl,int postr,int inl,int inr)
if (prel > prer)
return NULL;
tree root = new node;
root->left = root->right = NULL;
root->data = a[prel];
//root-<data=a[postr];
int k;
for (int k = 0; b[k] != a[prel]; k++);
int y = k - inl;
//前序遍历
root->left = bintree(prel + 1, prel + y, inl, k - 1);
root->right = bintree(prel + 1 + y, prer, k + 1, inr);
//后序遍历
//root->left = bintree(postl, postl + y - 1, inl, k - 1);
//root->right = bintree(post + y, postr - 1, k + 1, inr);
return root;
}
/*
tree g=bintree(0,n-1,0,n-1);
*/
void precount(tree root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
if (root->left != NULL)
{
precount(root->left);
}
if (root->right != NULL)
{
precount(root->right);
}
}
void precountMirror(tree root)
{
if (root == NULL)
{
return;
}
if (root->right != NULL)
{
precount(root->right);
}
cout << root->data << " ";
if (root->left != NULL)
{
precount(root->left);
}
}
void midcount(tree root)
{
if (root == NULL)
{
return;
}
if (root->left != NULL)
{
midcount(root->left);
}
cout << root->data << " ";
if(root->right!=NULL)
{
midcount(root->right);
}
}
void midcountMirror(tree root)
{
if (root == NULL)
{
return;
}
if (root->right != NULL)
{
midcount(root->right);
}
cout << root->data << " ";
if (root->left != NULL)
{
midcount(root->left);
}
}
void postcount(tree root)
{
if (root == NULL)
{
return;
}
postcount(root->left);
postcount(root->right);
cout << root->data << " ";
}
void postcountMirror(tree root)
{
if (root == NULL)
{
return;
}
postcount(root->right);
postcount(root->left);
cout << root->data << " ";
}
void cengxu(tree root)
{
if (root == NULL)
return;
queue<tree> que;
que.push(root);
while (!que.empty())
{
cout << que.front()->data << " ";
if (que.front()->left)
{
que.push(que.front()->left);
}
if (que.front()->right)
{
que.push(que.front()->right);
}
que.pop();
}
/* 控制格式的输出
*
*int g=0;
* while (!que.empty())
{
g++
if(g==n)
cout << que.front()->data ;
else
cout << que.front()->date << " ";
if (que.front()->left)
{
que.push(que.front()->left);
}
if (que.front()->right)
{
que.push(que.front()->right);
}
que.pop();
}
*/
}
//二叉树的反转,所谓镜面反转,是指将所有非叶结点的左右孩子对换
tree fanzhuan(tree root)
{
if (root == NULL)
{
return;
}
tree treee;
if (root->left != NULL || root->right != NULL)
{
treee = root->right;
root->right = root->left;
root->left = treee;
}
return root;
}
//二叉搜索树建立
tree Binsousuo(tree bt, int x)
{
if (!bt)
{
bt = new node;
bt->data = x;
bt->left = bt->right;
}
else if (x < bt->data)
{
bt->left = Binsousuo(bt->left, x);
}
else
bt->left = Binsousuo(bt->right, x);
return bt;
}
//平衡树
//求树高
int Getheight(tree root)
{
int hr, hl, hm;
if (!root)
{
return 0;
}
else
{
hl = Getheight(root->left);
hr = Getheight(root->right);
hm = max(hr, hl);
return hm + 1;
}
}
//左左单旋
tree leftleft(tree root)
{
tree b = root->left;
root->left = b->right;
b->right = root;
return b;
}
//右右双旋
tree rightright(tree root)
{
tree b = root->right;
root->right = b->left;
b->left = root;
return b;
}
//左右双旋
tree leftright(tree root)
{
root->left = rightright(root->left);
return leftleft(root);
}
//右左双旋
tree rightleft(tree root)
{
root->right = leftleft(root->right);
return rightright(root);
}
typedef int ElementType;
//ElementType 可为任意
tree buildtree(tree root, ElementType x)
{
if (!root)
{
root = new node;
root->data = x;
root->left = root->right = NULL;
}
else if(xdata)
{
root->left = buildtree(root->left, x);
if ( Getheight(root->left) - Getheight(root->right) == 2)
{
if (x < root->left->data)
{
root=leftleft(root);
}
else
{
root = leftright(root);
}
}
}
else if (x > root->data)
{
root->right = buildtree(root->right, x);
if (Getheight(root->right) - Getheight(root->left) == 2)
{
if (x > root->right->data)
{
root = rightright(root);
}
else if(x<root->right->data)
{
root = rightleft(root);
}
}
}
}