关于二叉树数据结构的一些操作合集(仅供参考)
#include<bits/stdc++.h>
using namespace std;
typedef struct treenode{
int data;
struct treenode *left,*right;
int ltag,rtag;
}TNode,*Tree;
int getHeight(TNode *p){
if(!p)
return 0;
else return 1+max(getHeight(p->left),getHeight(p->right));
}
int CountTreeNodes(Tree t){
if(t==NULL)
return 0;
else return 1+CountTreeNodes(t->left)+CountTreeNodes(t->right);
}
bool isFullTree(Tree t){
int n = getHeight(t);
int nodes = CountTreeNodes(t);
if(nodes == pow(2,n)-1)
return true;
else return false;
}
void LL_Rotate(TNode *&A){
TNode *B = A->left;
A->left = B->right;
B->right = A;
A = B;
}
void RR_Rotate(TNode *&A){
TNode *B = A->right;
A->right = B->left;
B->left = A;
A = B;
}
void RL_Rotate(TNode *&A){
LL_Rotate(A->right);
RR_Rotate(A);
}
void LR_Rotate(TNode *&A){
RR_Rotate(A->left);
LL_Rotate(A);
}
void InsertNode_AVL(Tree &t,int v){
if(!t){
t = (TNode *)malloc(sizeof(TNode));
t->left = t->right = NULL;
t->data = v;
t->ltag = t->rtag = 0;
}
else if(v > t->data){
InsertNode_AVL(t->right,v);
if(getHeight(t->right)-getHeight(t->left)==2){
if(v>t->right->data)
RR_Rotate(t);
else RL_Rotate(t);
}
}else{
InsertNode_AVL(t->left,v);
if(getHeight(t->left)-getHeight(t->right)==2){
if(v < t->left->data)
LL_Rotate(t);
else LL_Rotate(t);
}
}
}
int GetMaxWidth(Tree t){
queue <TNode*> q;
int maxwidth = 0;
int width = 0;
if(t){
maxwidth = width =1;
q.push(t);
while(!q.empty()){
TNode *p = q.front();
q.pop();
if(p->left)
q.push(p->left);
if(p->right)
q.push(p->right);
maxwidth = max(maxwidth,(int)q.size());
}
}
return maxwidth;
}
void printTree(Tree t){
static int level = -1;
int i;
if (NULL == t)
return;
level++;
printTree(t->right);
level--;
level++;
for (i = 0; i < level; i++)
printf("\t");
printf("%2d\n", t->data);
printTree(t->left);
level--;
}
void DeleteNode(TNode *&t){
if(t->left == t->right && t->left==NULL){
free(t);
}
else if(t->left==NULL && t->right){
*(t) = *(t->right);
}
else if(t->right==NULL && t->left){
*(t) = *(t->left);
}
else{
TNode *p = t->left,*pre = t->left;
int value;
while(p->right){
pre = p;
p=p->right;
}
pre->right = NULL;
value = t->data;
t->data = p->data;
p->data = value;
free(p);
}
}
void InsertNode(Tree &t,int v){
if(t==NULL){
t = (TNode *)malloc(sizeof(TNode));
t->left = t->right = NULL;
t->data = v;
t->ltag = t->rtag = 0;
}
else{
if(v > t->data)
InsertNode(t->right,v);
else InsertNode(t->left,v);
}
}
void InThread(Tree &t,TNode *&pre){
if(t){
InThread(t->left,pre);
if(t->left==NULL){
t->left = pre;
t->ltag = 1;
}
if(pre!=NULL && pre->right==NULL){
pre->right = t;
pre->rtag = 1;
}
pre = t;
InThread(t->right,pre);
}
}
TNode * Serch(Tree t,int v){
if(t){
if(t->data == v)
return t;
if(v < t->data)
return Serch(t->left,v);
else return Serch(t->right,v);
}
else return NULL;
}
TNode * next(TNode *&p){
if(p){
if(p->rtag==0){
p = p->right;
while(p->ltag==0)
p=p->left;
}
else p = p->right;
}
return p;
}
TNode * pre_node(TNode *&p){
if(p){
if(p->ltag==0){
p=p->left;
while(p->rtag==0)
p=p->right;
}
else p=p->left;
}
return p;
}
void PreOrder(Tree t){
stack<TNode *> s;
while(t || !s.empty()){
while(t){
cout << t->data << ' ';
s.push(t);
t = t->left;
}
if(!s.empty()){
TNode *p = s.top();
s.pop();
t = p->right;
}
}
cout << endl;
}
void InOrder(Tree t){
stack<TNode*> s;
if(!t)
return;
while(t || !s.empty()){
while(t){
s.push(t);
t = t->left;
}
if(!s.empty()){
TNode *p = s.top();
cout << p->data << ' ';
s.pop();
t = p->right;
}
}
cout << endl;
}
void PostOrder(Tree t){
stack<TNode *> s;
if(!t)
return;
TNode * cur;
TNode *pre = NULL;
s.push(t);
while(!s.empty()){
cur = s.top();
if((cur->left==NULL && cur->right==NULL)||
(pre!=NULL&&(pre==cur->left||pre==cur->right)))
{
cout << cur->data<<' ';
s.pop();
pre = cur;
}
else{
if(cur->right)
s.push(cur->right);
if(cur->left)
s.push(cur->left);
}
}
}
int *randList(int size,int type){
int *b = new int[size];
if(type==0){
for(int i=0;i<size;i++)
b[i] = rand()%size;
}
else{
for(int i=1;i<=size;i++)
b[i-1] = i;
}
return b;
}
int main(int argc, char const *argv[]){
int size = 20;
int *a = randList(size,1);
Tree t=NULL;
for(int i=0;i<size;i++){
cout << a[i] << " ";
InsertNode_AVL(t,a[i]);
}
cout<<endl;
printTree(t);
cout<<"The maxwidth:"<<GetMaxWidth(t)<<endl;
PreOrder(t);
InOrder(t);
PostOrder(t);
return 0;
}