408 code questions are generally 2-3 questions, (1) algorithm design idea (2) code implementation (3) time and space complexity

Generally 408 considering the normal distribution of scores, this problem will generally have optimal solutions, suboptimal solutions, and barely calculate the functions that can be realized by the code you write. The design idea and complexity calculation are consistent with your code implementation. These solutions for 15 point problems generally have a maximum of about 15, 11 and 9 points respectively.

Generally, suboptimal solutions are the basic knowledge of data structure and the functions required by the problem. It is mainly related to sorting, tree traversal and the application of linked list. The probability of sorting is the highest.

## Structure definition

There may be a question about defining the structure used.

Binary tree definition

typedef struct BiTNode{ int data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree;

Single linked list definition

typedef struct LNode{ int data; struct LNode *next; }LTNode,*LinkList;

Mainly remember the format of structure definition

## Quick row*****

Be sure to remember that when you take the exam, you are likely to sort it directly, and then you are almost finished. Just throw a shuttle directly.

Note that the time complexity is O(nlog2n) and the space complexity is O(log2n)

log is based on 2 n. Space complexity, because recursive stack will be used, just remember.

void QuickSort(ElemType A[],int low,int high){ if(low<high){//Recursive jump out condition int p=Partition(A,low,high);//Divided into two sub tables QuickSort(A,low,p-1);//Recursively sort the two sub tables in turn QuickSort(A,p+1,high); } } int Partition(ElemType A[],int low,int high){//One trip Division ElemType p=A[low];//Set the first element in the current table as the pivot to divide the table while (low<high){//Loop jump condition while(low<high&&A[high]>=p) --high; A[low]=A[high];//Move elements smaller than the pivot to the left while(low<high&&A[low]<=p) ++low; A[high]=A[low];//Move elements larger than the pivot to the left } A[low]=p;//Store pivot element in final position return low;//Returns the final location where the pivot element is stored }

When you write a sort function library, copy it down and use it directly.

It's good to understand the principle of fast platoon.

General topic arrays are of type int. you can directly change ElemType to int.

## Tree traversal

The first, middle and last order traversals of the tree are almost the same. If you remember one, you'll remember it all. Level traversal is OK, because if you need to use level traversal, you can generally use preorder traversal instead.

- proorder traversal

void PreOrder(BiTree T){ if (T!=NULL){ visit(T);//Access root node PreOrder(T.lchild); //Recursive traversal of left subtree PreOrder(T.rchild); //Recursive traversal of right subtree } }

Used in the title

int xxx=0//(to accumulate recursive results, consider defining global variables) void main(BiTree root){ PreOrder(root,0); } void PreOrder(BiTree t,int deep){ if(t.lchild==NULL and t.rchild==NULL){ //Judge whether it is a leaf node } if(t.lchild !=NULL){//If the left subtree is not empty, recursively traverse the left subtree PreOrder(t.lchild，deep+1); } if(t.rchild !=NULL){//If the right subtree is not empty, recursively traverse the right subtree PreOrder(t.rchild,deep+1); } }

- Middle order traversal of tree

void InOrder(BiTree T){ if (T!=NULL){ InOrder(T.lchild); //Recursive traversal of left subtree visit(T);//Access root node InOrder(T.rchild); //Recursive traversal of right subtree } }

Used in the title

int xxx=0//(to accumulate recursive results, consider defining global variables) void main(BiTree root){ InOrder(root,0); } void InOrder(BiTree t,int deep){ if(t.lchild !=NULL){//If the left subtree is not empty, recursively traverse the left subtree InOrder(t.lchild，deep+1); } if(t.lchild==NULL and t.rchild==NULL){ //Judge whether it is a leaf node } if(t.rchild !=NULL){//If the right subtree is not empty, recursively traverse the right subtree InOrder(t.rchild,deep+1); } }

- Postorder traversal of tree

void PostOrder(BiTree T){ if (T!=NULL){ PostOrder(T.lchild); //Recursive traversal of left subtree PostOrder(T.rchild); //Recursive traversal of right subtree visit(T);//Access root node } }

Used in the title

int xxx=0//(to accumulate recursive results, consider defining global variables) void main(BiTree root){ PostOrder(root,0); } void PostOrder(BiTree t,int deep){ if(t.lchild !=NULL){//If the left subtree is not empty, recursively traverse the left subtree PostOrder(t.lchild，deep+1); } if(t.rchild !=NULL){//If the right subtree is not empty, recursively traverse the right subtree PostOrder(t.rchild,deep+1); } if(t.lchild==NULL and t.rchild==NULL){ //Judge whether it is a leaf node } }

- The hierarchical traversal of the tree (including the declaration and use of the queue) is not recommended. It is generally not used. It is too troublesome

You can understand the use of the lower queue and the implementation of the hierarchical traversal code.

#define MaxSize 100 / / sets the maximum queue size void fuc(BiTree root){ BiTree q[maxsize]; //Declare the queue. end1 is the head pointer and end2 is the tail pointer int end1=0,end2=0; int deep=0; //Initialization depth BiTree lastNode; //lastNode records the last node of the current layer BiTree newlastNode; //newlastNode records the last node of the next layer lastNode=root;//The lastNode is initialized as the root node newlastNode=NULL;//newlastNode initialization is null q[end2++]=root;//Root node join while(end1!=end2){//Level traversal. If it is not empty, it will loop BiTree t=q[end1++]; //Take out the team head element if(t.lchild==NULL and t.rchild==NULL){ //Judge whether it is a leaf node } if(t.lchild !=NULL){//If the left child exists, the left child is added to the queue q[end2++]=t.lchild; newlastNode=t.lchild; } if(t.rchild !=NULL){//If the right child exists, the right child will be added to the queue q[end2++]=t.rchild; newlastNode=t.rchild; } if(t==lastNode){//If this node is the last node of this layer lastNode=newlastNode; deep+=1//Number of layers plus one } } }

## Linked list

Single chain header interpolation inverse linked list

Disconnect the head node in the original linked list from the first element node (make its pointer field empty), first form a new empty list, and then insert each node in the original linked list into the head of the new table from the first node (that is, make each inserted node become the new first element node)

void reverList(LNode *L){ LNode *temp=L.next;//Record the first node except the header node LNode *q;//Used to record subsequent nodes L.next=NULL;//Disconnect the original header node while(temp!=NULL){ q=temp.next;//The q node records the subsequent nodes of temp temp.next=L.next;//Insert subsequent nodes into the new table header at one time L.next=temp; temp=q; } }

## Joint search set

Take judging whether a graph has a ring as an example. Because the vertex elements of the graph are letters, we use an int array to represent and query the set (the order of letters corresponds to the subscript itself). Otherwise, we need to define the structure according to the question. I think this kind of question generally won't be tested. We should learn and query the set.

int hasCyclic(int g[5][5]){//g[5][5] is the adjacency matrix represented by two-dimensional array int S[5];//Define, initialize, and query sets for(int i=0;i<5;i++) s[i]=-1; for(int i=0;i<5;i++) for(int j=i+1;j<5;j++) if(g[i][j]>0){//There are edges between nodes i and j int iroot=Find(S,i);//Find the set i belongs to by merging and searching the set int jroot=Find(S,j);//Find the set j belongs to by merging and searching the set if(iroot!=jroot)//i. j is not in the same set Union(S,iroot,jroot) else //i. j is originally in the same set, that is, Unicom return 1; } return 0;//There is no ring in the figure }

## Binary search

int binary_search(int A[],int key,int n){ int low=0,high=n-1,mid; while(low<high){ mid=(low+high)/2;//Take the middle position if(A[mid]==key) return mid; //If the search is successful, the location will be returned else if(A[mid]>key) high=mid-1;//Keep looking from the front half else low=mid+1;//Keep looking from the second half } return -1;//Search failed, return - 1 }