算法竞赛模板 二叉树
①二叉树结点定义
struct TreeNode{ int val; TreeNode*left,*right; TreeNode(){} TreeNode(int val):val(val),left(NULL),right(NULL){} };
②二叉树遍历方式
void Preorder(TreeNode*root) { if(root) { printf("%d ",root->val); Preorder(root->left); Preorder(root->right); } }
void Inorder(TreeNode*root) { if(root) { Inorder(root->left); printf("%d ",root->val); Inorder(root->right); } }
void Postorder(TreeNode*root) { if(root) { Postorder(root->left); Postorder(root->right); printf("%d ",root->val); } }
void Levelorder(TreeNode*root) { int front=0,rear=1; TreeNode*qu[MaxSize],*p; qu[rear]=root; while(front!=rear) { p=qu[++front]; printf("%d ",p->val); if(p->left) qu[++rear]=p->left; if(p->right) qu[++rear]=p->right; } }
③二叉树基本操作
TreeNode*createLevel() { int front=1,rear=0,x; TreeNode*qu[1005],*t,*root=NULL; while(cin>>x,x!=-1) { if(!x) t=NULL; else { t=new TreeNode; t->val=x; t->left=NULL; t->right=NULL; } qu[++rear]=t; if(rear==1) root=t; else { if(qu[front]&&t) { if(rear%2==0) qu[front]->left=t; else qu[front]->right=t; } if(rear%2==1) front++; } } return root; }
int jud(TreeNode*left,TreeNode*right) { if(!left&&!right) return 1; if(!left||!right) return 0; if(left->val!=right->val) return 0; return jud(left->left,right->right)&&jud(left->right,right->left); } int isSymmetric(TreeNode*root) { if(!root) return 1; return jud(root->left,root->right); }
int maxDepth(TreeNode*root) { if(!root) return 0; int left=maxDepth(root->left)+1; int right=maxDepth(root->right)+1; return left>right?left:right; }
int dis; int dfsDiameter(TreeNode*root) { if(!root) return 0; int x=dfsDiameter(root->left); int y=dfsDiameter(root->right); if(x+y>dis) dis=x+y; return x>y?x+1:y+1; } int Diameter(TreeNode*root) { dfsDiameter(root); return dis; }
④二叉查找树(Binary Search Tree)
又称:二叉搜索树,二叉排序树。
它或是一棵空树,或是具有下列性质的二叉树:
① 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
② 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
③ 它的左、右子树也分别为二叉排序树。
ps:如果中序遍历BST,会得到一个递增序列
TreeNode*dfsBST(TreeNode*root,int x) { if(!root) { root=new TreeNode; root->val=x; root->left=NULL; root->right=NULL; } else if(x> root->val) root->right=dfsBST(root->right,x); else if(x<= root->val) root->left=dfsBST(root->left,x); return root; } TreeNode*createBST() { int nums[1005],Size,i; cin>>Size; for(i=0;i<Size;i++) cin>>nums[i]; TreeNode*root=NULL; for(i=0;i<Size;i++) root=dfsBST(root,nums[i]); return root; }
int L,R;//所取边界[L,R] TreeNode*cutBST(TreeNode*root) { if(!root) return NULL; if(root->val<L) return cutBST(root->right); else if(root->val>R) return cutBST(root->left); else { root->left=cutBST(root->left); root->right=cutBST(root->right); return root; } }
TreeNode*searchBST(TreeNode*t,int x) { while(t) { if(x< t->val) t=t->left; else if(x> t->val) t=t->right; else return t; } return NULL; }
void insertBST(TreeNode*&root,int x) { if(!root) root=new TreeNode(x); else if(root->val >x) insertBST(root->left,x); else if(root->val <x) insertBST(root->right,x); }
void deleteBST(TreeNode**root,int x) { TreeNode*t=*root; TreeNode*dad=*root; while(t) //查找结点 { if(t->val==x) break; dad=t; //此结点的父结点 if(t->val >x) t=t->left; else t=t->right; } if(!t) //没有该结点就跳出 return; if(t->left==NULL&&t->right==NULL) //是叶子结点 { if(t==*root) //是根结点 *root=NULL; else if(dad->left==t) dad->left=NULL; else dad->right=NULL; delete t; } else if(t->left==NULL||t->right==NULL) //该结点是单支结点 { if(t==*root) //是根结点 { if(t->left) *root=t->left; else *root=t->right; } else if(dad->left==t) { if(t->left) dad->left=t->left; else dad->left=t->right; } else { if(t->left) dad->right=t->left; else dad->right=t->right; } delete t; } else //该结点是双支结点 { TreeNode*par=t; TreeNode*child=t->right; while(child->left) { par=child; child=child->left; } t->val=child->val; if(par==t) t->right=child->right; else par->left=child->right; delete child; } }
⑤平衡二叉搜索树(Self-balancing Binary Search Tree)
它或是一棵空树,或是具有下列性质的二叉树:
① 它的左右两个子树的高度差的绝对值不超过1;
② 它左右两个子树都是一棵平衡二叉树。
int flag=1; int dfsBalance(TreeNode*root) { if(!root) return 0; int x=dfsBalance(root->left)+1; int y=dfsBalance(root->right)+1; if(abs(x-y)>1) flag=0; return x>y?x:y; } int isBalance(TreeNode*root) { dfsBalance(root); return flag; //若是 返回1,不是返回0 }