# 数据结构期末考试总结

2.链式栈使用什么结构？

Following are advantages/disadvantages of doubly linked list over singly linked list.

Advantages over singly linked list
1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.

Disadvantages over singly linked list
1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though (See this and this).
2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.

7.二分搜索树怎么搜索？

11.前、中、后序的线索树，哪个找不到后继？

struct Node
{
int data;
Node *left, *right;
bool rightThread;
}

// Utility function to find leftmost node in a tree rooted with n
struct Node* leftMost(struct Node *n)
{
if (n == NULL)
return NULL;

while (n->left != NULL)
n = n->left;

return n;
}

// C code to do inorder traversal in a threaded binary tree
void inOrder(struct Node *root)
{
struct Node *cur = leftmost(root);
while (cur != NULL)
{
printf("%d ", cur->data);

// If this node is a thread node, then go to
// inorder successor
if (cur->rightThread)
cur = cur->rightThread;
else // Else go to the leftmost child in right subtree
cur = leftmost(cur->right);
}
}

We could also base a scheme on post-order. Unfortunately, forward post-order will always be hard, because a node’s post-order successor is never below it in the tree. However, a scheme to support reverse post-order would be somewhat easier!

12.4个权值构建二分搜索树？

Catalan number Cn = (2n)!/(n+1)!*n!

1) Count the number of expressions containing n pairs of parentheses which are correctly matched. For n = 3, possible expressions are ((())), ()(()), ()()(), (())(), (()()).

2) Count the number of possible Binary Search Trees with n keys.

3) Count the number of full binary trees (A rooted binary tree is full if every vertex has either two children or no children) with n+1 leaves.

1.尾递归的定义？

Why do we care?
The tail recursive functions considered better than non tail recursive functions as tail-recursion can be optimized by compiler. The idea used by compilers to optimize tail-recursive functions is simple, since the recursive call is the last statement, there is nothing left to do in the current function, so saving the current function’s stack frame is of no use.

10.Two-way merge算法，第i轮中first sequence长度？
2^i(2的i次方)

degree为2的点的graph，一定有cycle?（中间的一道题，忘记题号……）

In graph theory, the degree (or valency) of a vertex of a graph is the number of edges incident to the vertex, with loops counted twice.

Prove that every undirected finite graph with vertex degree of at least 2 has a cycle

3.给出最大堆，如何进行排序？

1.机器人走路，使用中间变量写递归

#include <iostream>

using namespace std;

#define M 3
#define N 3

int countStep(int row, int column, int matrix[M][N])
{
if (row==M-1 && column == N-1)
{
return 1;
}
int temp1 = 0;
int temp2 = 0;
if (row + 1 < M)
{
if (matrix[row + 1][column] != -1)
{
temp1 = matrix[row + 1][column];
}
else {
temp1 = countStep(row + 1, column, matrix);
}

}
if (column + 1 < N)
{
if (matrix[row][column + 1] != -1)
{
temp2 = matrix[row][column + 1];
}
else {
temp2 = countStep(row, column + 1, matrix);
}
}
int total = temp1 + temp2;
matrix[row][column] = total;
return total;
}

int main()
{
int matrix[M][N];
memset(matrix, -1, sizeof(int)*M*N);

int count = countStep(0, 0, matrix);
cout << "All the possible ways are: "<<count;

system("pause");
return 0;
}

2.二路归并，如何不用额外数组来排序？

#include <iostream>

using namespace std;
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int left, int middle, int right)
{
int i, j, k;
int left_length = middle - left + 1;
int right_length = right - middle;

/* Merge the temp arrays back into arr[l..r]*/
i = left; // Initial index of first subarray
j = middle+1; // Initial index of second subarray

int swap_count = 0;
while (i<= right && j <= right)
{
if (arr[i] > arr[j])
{
int temp = arr[j];
memcpy(&arr[i + 1], &arr[i], (left_length - swap_count)*sizeof(int));
arr[i] = temp;

i++;
j++;
}
else {
i++;
swap_count++;
}

}
}

/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int middle = left + (right - left) / 2;

// Sort first and second halves
mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);

merge(arr, left, middle, right);
}
}

/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

/* Driver program to test above functions */
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");
printArray(arr, arr_size);

system("pause");
return 0;
}

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。