稀疏矩阵 三元组转置
转置详解链接
矩阵转置的实现思路是:不断遍历存储矩阵的三元组表,每次都取出表中 j 列最小的那一个三元组,
互换行标和列标的值,并按次序存储到一个新三元组表中。
栈和队列
十进制转换任意进制
int switch_num(int num,int hex){
if(num<0)
num = -num;
if(num==0)
return 0;
stack s;
while(num){
s.push(num % hex);
num /= hex;
}
while(! s.empty()){
if(s.top() < 10 )
printf("%c",s.top()+ '0');
else
printf("%c",s.top() -10 +'A');
s.pop();
}
}
字符串回文判定
int judge( char * str){
stack s;
int len =strlen(str);
for(int i=0;i<len;i++)
s.push(str[i]);
for(int i=0;i<len;i++){
if(s.top() != str[i])
return 0;
s.pop();
}
return 1;
}
树
非递归先序中序后序
void Inorder(Tree * t){
InitStack(s);
Tree *p=t;
while(p || ! IsEmpty(s)){
if(p){
push(s,p);
p= p->lchild;
}
else{
pop(s,p);
visit(p);
p = p->rchild;
}
}
}
void PreOrder(Tree *t){
InitStack(s);
Tree * p =t;
while(p || ! IsEmpty(s)){
if(p){
visit(p);
push(s,p);
p=p->lchild;
}
else{
pop(s,p);
p=p->rchild;
}
}
}
void PostPrder(Tree * t){
InitStack(s);
Tree *p =t;
Tree * temp=NULL;
while( p || ! IsEmpty(s)){
if(p){
push(s,p);
p=p->lchild;
}
else{
GetTop(s,p);
if(p->rchild && p->rchild !=temp){
p= p->rchild;
}
else{
pop(s,p);
visit(p->data);
temp =p;
p=NULL;
}
}
}
}
层次遍历
void Order(Tree * t){
InitQueue(q);
Tree *p;
EnQueue(q,t);
while(! IsEmpty(q)){
DeQueue(q,p);
visit(p);
if(p->lchild !=NULL)
EnQueue(q,p->lchild);
if(p->rchild != NULL)
EnQueue(q,p->rchild);
}
}
寻找祖先
1.顺序结构存储 找两节点最近公共祖先
int test(Tree t,int i,int j){
if(t[i]!='-1' && t[j]!='-1'){
while(i!=j){
if(i>j)
i=i/2;
else
j=j/2;
}
return t[i];
}
}
2.链式存储,找单节点所有祖先
typedef struct{
Tree t;
int tag;
}stack;
void search(Tree *t,int x){
stack s[];
Tree*p=t;
top=0;
while( p || top>0){
while(p && p->data!=x){
s[++top].t =p;
s[top].tag=0;
p=p->lchild;
}
if(p && p->rchild==x){
printf("所有祖先结点:");
for(int i=1;i<=top;i++)
printf("%d", s[i].t->data);
exit(1);
}
while(top!=0 && s[top].tag==1)
top--;
if(top!=0){
s[top].tag=1;
p= s[top].t->rchild;
}
}
}
判断是否为二叉排序树
1.顺序存储—递归实现(中序)(完全二叉树 层次遍历理解 下标从开始)
typedef struct{
int node[max];
int elemnum;
}Tree;
int pre=0;
bool InOrder(Tree*t,int i){
if(t->Node[i] == -1)
return true;
if(!InOrder(t,2*i+1))
return false;
if(pre > t->Node[i])
return false;
pre=t->Node[i];
if(!InOrder(t,2*i+2))
return false;
return true;
}
2.链式存储—递归
int pre = -1;
bool judge(Tree* root) {
if (root == NULL)
return true;
if (! judge(root->left))
return false;
if (root->data < pre)
return false;
pre = root->data;
return judge(root->right);
}
判断两二叉树是否相似
int same(Tree* a,Tree* b){
if(a ==NULL && b ==NULL) return 1;
if(a ==NULL || b==NULL) return 0;
return a->data==b->data
&& same(a->lchid,b->lchild)
&& same(a->rchild,b->child);
}
判断平衡二叉树
void judge(Tree *t,int & balance,int &h){
int bl=0,br=0,h1=0,hr=0;
if(t==NULL){
h=0;
balance = 1;
}
else if(t->lchild==NULL && t->rchild==NULL){
h=1;
balance = 1;
}
else{
judge(t->lchild,bl,hl);
judge(t->rchild,br,hr);
h=(h1 > hr ? hl:hr)+1;
if(abs(hl-hr)<2)
balance= bl && br;
else
balance=0;
}
}
交换二叉树左右子树
void swap(Tree * b){
if(b){
node*temp = b->lchild;
b->lchild = b->rchild;
b->rchild = temp;
swap(b->lchild);
swap(b->rchild);
}
}
将二叉树转换为中缀表达式
void change(tree * root){
test(root,1);
}
void test(tree* t,int deep){
if(t==NULL)
return ;
else if(t->left== NULL && t->right == NULL)
printf("%s",t->data);
else{
if(deep>1) printf("(");
test(t->left,deep+1);
printf("%s",t->data);
test(t->right,deep+1);
if(deep>1)printf(")");
}
}
二叉树wpl带权路劲长度
typedef struct Node{
int weight;
struct Node *lchild,*rchild;
}Node,*Tree;
int wpl(Tree root){
return preorder(root,0);
}
int preorder(Tree t,int deep){
static int w=0;
if(t->lchild==NULL && t->rchild ==NULL)
w+= deep * t->weight;
if(t->lchild!=NULL)
preorder(t->lchild,deep+1);
if(t->rchild!=NULL)
preorder(t->rchild,deep+1);
return w;
}
图
十字链表法 有向图
多重邻接表 无向图
typedef struct {
int Vex[Max];
int Edge[Max][Max];
int vexnum, arcnum;
} Graph;
typedef struct ArcNode{
int adjvex;
struct ArcNode * next;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *first;
}VNode,AdjList[max];
typedef struct{
AdjList vertices ;
int vexnum, arcnum;
}Graph;
广搜
int visited[Max];
void BFSTraverse(Graph G){
int i;
for (i = 0; i < G.vexnum; i++)
visited[i]=0;
InitQueue(Q);
for (i = 0; i < G.vexnum; i++){
if (!visited[i])
BFS(G,i);
}
}
void BFS(Graph G, int v){
visit(v);
visited[v] =1;
EnQueue(Q);
while(!isEmpyt(Q)) {
DeQueue(Q,v);
for(int w=FirstNeighbor(G,v);w>=0;w=NextNeighbor(G,v,w))
if(!visited[w]){
visit(w);
visited[w]=1;
EnQueue(Q,w);
}
}
}
深搜
int visited[Max];
void DFSTraverse(Graph G){
int i;
for (i = 0; i < G.vexnum; i++)
visited[i]=0;
for (i = 0; i < G.vexnum; i++)
if (!visited[i])
DFS(G,i);
}
void DFS(Graph G, int v){
visit(v);
visited[v] =1;
for(int w=FirstNeighbor(G,v);w>=0;w=NextNeighbor(G,v,w))
if(!visited[w]){
DFS(G,w);
}
}
排序
折半插入排序
void search(int a[], int n) {
int i, j, low, mid, high;
for (i = 2; i <= n; i++) {
a[0] = a[i];
low = 1;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (a[mid] > a[0])
high = mid - 1;
else
low = mid + 1;
}
for (j = i - 1; j >= low; --j)
a[j + 1] = a[j];
a[low] = a[0];
}
}
快速排序
int Partion(int a[],int low,int high){
int key =a[low];
while(low< high){
while(low <high && a[high] >= key) --high;
a[low] =a[high];
while(low <high && a[low] <= key) ++low;
a[high] =a[low];
}
a[low] =key;
return low;
}
void QuickSort(int a[], int low,int high){
if(low <high){
int key =Partion(a,low,high);
QuickSort(a,low,key-1);
QuickSort(a,key+1,high);
}
}
划分序列 右边的元素比左边的大,右区间减左区间最大
void Sort(int a[],int n){
int key,low=0,low1=0,high=n-1,high1=n-1,flag=1,k=n/2,i;
int s1=0,s2=0;
while(flag){
key= a[low];
while(low<high){
while(low<high && a[high] >= key) --high;
if(low !=high) a[low] =a[high];
while(low<high && a[low] <= key) ++low;
if(low!=high) a[high]=a[low];
}
a[low] =key;
if(low == k-1)
flag=0;
else{
if(low < k-1){
low1 = ++low;
high=high1;
}
else{
high1 =--high;
low =low1;
}
}
}
for(i=0;i<k;i++) s1+=a[i];
for(i=k;i<n;i++) s2+=a[i];
return s2-s1;
}
归并排序
int b[max];
void Merge(int a[], int low, int mid, int high){
int i,j,k;
for(k=low; k<=high;k++)
b[k] =a[k];
for(i=low,j =mid+1,k=low;i<=mid && j<=high;k++){
if(b[i] <= b[j])
a[k] = b[i++];
else
a[k] = b[j++];
}
while(i<=mid) a[k++]=b[i++];
while(j<=high) a[k++]=b[j++];
}
void MergeSort(int a[],int low,int high){
if(low< high){
int mid =(low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
Merge(a,low,mid,high);
}
}