Data structure of All In

1. Suppose the pre-order sequence of a binary tree: ABDFCEGH, the middle-order sequence: BFDAGEHC.

(1) Draw this binary tree
(2) Draw the post-order clue tree of
this binary tree (3) Convert this binary tree into a corresponding tree or forest

Insert picture description here


Using the method of traversing the binary tree in hierarchical order, count the number of nodes with degree 1 in the tree

Insert picture description hereInsert picture description here


Find the time complexity of the following algorithm

s=0;
for i=0; i<n; i++)
	for(j=0; j<n; j++)
         s+=B[i][j];
sum=s;

Answer: O(n2)
Explanation: The number of executions of the statement s+=B[i][j]; is n2.


In the sequence table of n nodes, the time complexity of the algorithm is O(1) and the operation is (A).

A.Visit the i-th node (1≤i≤n) and find the immediate predecessor of the i-th node (2≤i≤n)
B. Insert a new node after the i-th node (1≤i≤n)
C. Delete the i-th node (1≤i≤n)
D. Sort n nodes from small to large.
Analysis: The read node operation in option A is directly located by the array subscript


Insert a new element into a sequence table with 127 elements and keep the original sequence unchanged, and average the number of elements to be moved ().

A.8
B.63.5
C. 63
D. 7
Knowledge points: Analysis of the insertion algorithm of the linear table n=127, the number of moves n/2=63.5


Algorithm design questions:

The design algorithm decomposes a singly linked list A with a lead node into two linked lists B and C with the same structure. The node of table B is the node whose value is less than zero in table A, and the node of table C is table A The node whose median value is greater than zero (the elements in the linked list A are non-zero integers, and the B and C tables are required to use the nodes of the A table)

void Decompose(LinkList &A,LinkList &B,LinkList &C )
{
    
     B=A;
B->next= NULL; ∥B表初始化
C=new LNode;∥为C申请结点空间
C->next=NULL; ∥C初始化为空表
p=A->next; ∥p为工作指针
while(p!= NULL)
{
    
     r=p->next; ∥暂存p的后继
if(p->data<0)
{
    
    p->next=B->next; B->next=p; }
∥将小于0的结点链入B表,前插法
else {
    
    p->next=C->next; C->next=p; }
∥将大于等于0的结点链入C表,前插法
p=r;∥p指向新的待处理结点
}} 

If a stack is stored in the vector V[1...n] and the initial top pointer top is set to n+1, the correct operation for pushing element x onto the stack is ().

A.top++; V[top]=x;
B.V[top]=x; top++;
C.top–; V[top]=x;
D. V[top]=x; top–;
Analysis: The initial stack top pointer top isn+1, Which means that the element is pushed into the stack from the high-end address of the array vector, when the element x is pushed into the stack, the top pointer first moves down to n (top–), and then the element x is stored in V[n]


Assuming that the queue is represented by a circular linked list with the head node, and only one pointer is set to point to the end element node of the queue (note: no head pointer is set), try to write the corresponding empty queue, judge whether the queue is empty, enter and dequeue And other algorithms

Insert picture description hereInsert picture description here
Insert picture description here



Pretend to be the dividing line



Design an algorithm to reverse the link direction of all nodes in the linked list "in situ", that is, the storage space of the original table is required, in other words, the space complexity of the algorithm is required to be O(1).

Insert picture description here


Insert picture description here


Suppose there are n2 nodes with degree 2 in the binary tree, n1 nodes with degree 1, and n0 nodes with degree 0, then

This is super important
Insert picture description here
Insert picture description here
Insert picture description here


Full binary tree:

The total number of nodes n = 2 k -1


Complete binary tree

The first k-1 layer is full, the kth layer can be dissatisfied, but the kth layer nodes are concentrated on the left


Complete binary tree
Insert picture description here


Incomplete binary tree
Insert picture description here


Number the complete binary tree from top to bottom and from left to right,

  树根的编号为1。对于编号为i的结点X有:
  
若i=1,则X是根;若i>1, 则X的双亲的编号为i/2;

若X有左孩子,则X左孩子的编号为2i;

若X有右孩子,则X右孩子的编号为2i+1;

若i为奇数且i>1,则X的左兄弟为i-1;

若i为偶数且i<n,则X的右兄弟为i+1;

Insert picture description here

Insert picture description here


If the preorder (preorder) traversal sequence of a binary tree is a, e, b, d, c, and the postorder traversal sequence is b, c, d, e, a, then the child nodes of the root node are ()

A.Only e
Be,b
Ce,c
D. Unsure

Analysis: According to the preorder traversal, we know that the root node is a and e is the child node of a. When the preorder sequence of the two nodes is XY and the postorder sequence is YX, it can be known that X is the ancestor of Y. The ancestor of b, c, d is e, so the child of the root node is only e


The nodes of the binary tree are numbered consecutively starting from 1, and the number of each node is required to be greater than the number of its left and right children. Among the left and right children of the same node, the number of its left child is less than the number of its right child, which can be used () Traverse the implementation number

A. First order B. Middle order C.Post sequence D. From the root level traversal


The pre-order traversal sequence and the post-order traversal sequence of a non-empty binary tree are exactly opposite, then the binary tree must satisfy ()

A. All nodes have no left children B. All nodes have no right children C. == Only one leaf node == D. It is any binary tree.


Example: When a system is in communication, only five characters of C, A, S, T, and B appear, and their appearance frequencies are 2, 4, 2, 3, 3, and try to design Huffman codes.

Insert picture description here


Insert picture description here



Pretend to be dividing line 2



The array Q[n] is used to represent a circular queue, f is the previous position of the head element of the current queue, and r is the position of the tail element. Assuming that the number of elements in the queue is less than n, the formula for calculating the number of elements in the queue is : (D)

A r-f
B (n+f-r)%n
C n+r-f
D (n+r-f)%n

Analysis: circular queue: when the rf difference is negative, add n, and then calculate the remainder of n


There is a recursive algorithm as follows:

int fact (int n)
{
    
    
if(n<=0) return 1;
else return n*fact(n-1);
}

Then the number of times the function fact(n) needs to be called is (A)
A. n+1 B. n-1 C. n D. n+2


There is a famous "Ackermann (Ackermann) function" in mathematics, which is defined as follows:

Insert picture description here
(1) Write the recursive algorithm of Ack(m,n)

int Ack(int m, int n)
//Ack函数的递归算法
{
    
    
If(m==0)
   return n+1;
else if (m!=0 && n==0)
   return Ack(m-1, 1);
Else
   return Ack(m-1,Ack(m,m-1));
}

(2) Write a non-recursive algorithm for calculating Ack(m,n)

int Ack(int m, int n)
//Ack函数的递归算法
{
    
    
    for(j=0;j<n;j++) 
    akm[0][j] = j+1;//得到Ack(0,n)的值
    for(i=0;i<m;i++)
    {
    
    
        akm[i][0] = akm[i-1][1];
        for(j=1;j<n;j++)
        akm[i][j] = akm[i-1][akm[i][j-1]];
    }
    return akm[m][n];
}

From the data structure, the data structure can be logically divided into () and ()

A. Dynamic structure and static structure B. Compact structure and non-compact structure
C.Linear structure and nonlinear structure D. Internal structure and external structure

Knowledge point: the logical structure of data


The logical relationship between data elements in the sequential storage structure is represented by (storage location)

The logical relationship between data elements in the chain storage structure is represented by (pointers)


The three components of abstract data types are: (data object), (data relationship) and (basic operation)


Find the time complexity of the following algorithm

x=90; y=100; 
while(y>0)
if(x>100)
 {
    
    x=x-10;y--;}
else x++;

Answer: O(1)
knowledge point: the time complexity of the algorithm


Find the time complexity of the following algorithm

s=0;
     for i=0; i<n; i++)
for(j=0; j<n; j++)
         s+=B[i][j];
sum=s;

Answer: O(n2)
Explanation: The number of executions of the statement s+=B[i][j]; is n2.


The sentence execution frequency of an algorithm is (3n+nlog2n+n2+8), and its time complexity is expressed as ()

A. O(n)B. O(nlog2n)C. O(n2) D. O(log2n)
analysis:
Insert picture description here


Corresponding to a singly linked list with a head node whose head pointer is head, the condition for judging that the table is empty is

A.head= =NULL;
B.head->next= =NULL;
C.head->next= =head; 
D.head!=NULL;


Assuming that the most commonly used operation of a linked list is to insert nodes at the end and delete the end nodes, choose () to save time

A. Single linked list
B. Single circular linked list
C. Single circular linked list with tail pointer 
D.Double circular linked list with leading node


The storage state of the singly linked list whose header element is c is known as shown in the figure below. Now store f at 1014H and insert it into the singly linked list. If f is logically located between a and e, then the "link addresses" of a, e, and f are in turn

A.1010H,1014H,1004H
B.1010H,1004H,1014H
C.1014H,1010H,1004H 
D.1014H,1004H,1010H
Insert picture description here


The design algorithm decomposes a singly linked list A with a lead node into two linked lists B and C with the same structure. The node of table B is the node whose value is less than zero in table A, and the node of table C is table A The node whose median value is greater than zero (the elements in the linked list A are non-zero integers, and the B and C tables are required to use the nodes of the A table)

void Decompose(LinkList &A,LinkList &B,LinkList &C )
{
    
     B=A;
B->next= NULL; ∥B表初始化
C=new LNode;∥为C申请结点空间
C->next=NULL; ∥C初始化为空表
p=A->next; ∥p为工作指针
while(p!= NULL)
{
    
     r=p->next; ∥暂存p的后继
if(p->data<0)
{
    
    p->next=B->next; B->next=p; }
∥将小于0的结点链入B表,前插法
else {
    
    p->next=C->next; C->next=p; }
∥将大于等于0的结点链入C表,前插法
p=r;∥p指向新的待处理结点
}} 

Guess you like

Origin blog.csdn.net/Touale/article/details/112970023