Chapter 17: Classic Abstract Data Types
Github
Link: ch17. Classic abstract data types
Abstract data type (ADT)
is very common, the most common is the array, sequence tables, linked lists, stacks and queues, and so on. Such as OS
task scheduling queue inside there, doubly linked list, red-black trees are widely used. Proficiency in various data structures is very important and necessary.
Summary of this chapter and points to note
Answers to some after-school exercises
17.9 Problem
-
Stack.
-
queue.
-
of course can. The programmer can encapsulate it.
top()
Take the top element of the stack but do not pop the top element of the stack, thepop()
function pops the top element of the stack. -
I don't feel how powerful it is. For static array simulation in terms of the stack, just to
top_element() = -1
be assigned to empty stack so that can achieve results. Of course,C++ STL
the container has a separateclear
function used to empty the container. -
It all depends on whether its initialization is -1 or 0.
-
First
assert()
mainly used in this sentence stack empty, full sentence, in fact, it is to keep out of bounds array access occurs. If you delete all the assertions, it is equivalent to the array may have out-of-bounds access, the two are equivalent. -
The nodes in the chain stack are all applied for separately, so they have to be released separately. And the
pop()
function has been implemented in the release of memory, so a direct stack sentenced empty +pop()
can be. -
Certainly not! This is a common problem when
malloc()
space isfree
the corresponding pointer no longer access a memory space has been released. So take a temporary pointer variable to save the address of the space it points to, and finally release the space pointed to by this pointer variable. -
The answer gives a very good explanation:
-
Mentioned in the book. It is really simple to use a counter, and vacating an array element without using it will cause a waste of space. When the size of the element is larger, the waste of space will be greater.
-
This is indeed a troublesome problem. There are four situations in total: the queue is empty, full,
front
first, and second. Note that at the end, the calculated element needs to be modulo. When the queue is empty, the modulo operation will also give the correct answer. Seedemo01.c
. -
Queue and do not need a reverse traversal, this does not apply doubly linked list,
STL
thedeque
double-ended queue, slow the ghost ... -
BST
Insertion will only insert at the position of its leaf node and will not modify the structure of the original tree. Just pay attention to this point. -
It is equivalent to array and linked list, O (n) O(n)O ( n )。
-
Simple question.
-
Middle order: left-root-right. Post sequence: left-right-root. Preface: Root-Left-Right. In fact, it can be found: Simply modify the preorder traversal to: root-right-left, then the reverse order of the preorder is the postorder traversal. Of course, I'm talking about iterative writing, as for recursion, there is nothing to write about. You can see the blog post, the four kinds of traversal are summarized in detail: [M Binary Tree] lc145. Post-order traversal of binary tree (stack+dfs) .
-
Same as above. Simply look at the reference answer:
-
In-order traversal. None of the four traversals can directly get the descending sequence, but simply modify the middle order traversal, and change it to right-root-left to get the descending sequence.
-
Of course, it is post-order traversal, which will process the child nodes before processing the root node.
17.10 Programming Exercises
-
This function must allocate space for the new stack and copy the value of the old stack to the new one. Then it must release the old array. Assertions are used to ensure that the new array is large enough to hold all the data currently on the stack. See
demo02.c
. -
This module is converted into
stack
the module.resize
Function is more interesting: not every position in the array need to be copied, and when the data around to the end of the array,front
andrear
it is easy to become incorrect. Seedemo03.c
. -
I'm too lazy to write, just finish writing by yourself and look at the answers after class. It is a simple basic operation of inserting and deleting the end of the linked list.
-
This is simple, the stack data is simply declared as an array, and the stack number passed as a parameter selects the element to be operated on. See
demo04.h、demo05.c
. -
Simply
dfs()
, count the number of nodes in the binary tree. -
Common
bfs()
template problem, which is realized in the form of an arrayBST
, can also refer to my blog [M Binary] lc102. Binary tree traversal sequence (queue bfs +) . -
It doesn't mean anything, whether the mid-order sequence is strictly ascending. Recursion can also be used to judge directly. It is definitely possible to solve this problem. Just go up and practice your hands. Here you have to build and write test cases, but the test is not complete.
-
My blog post: [C++ series] 76. Explain the BST binary search tree in detail . The array form is similar to each other. The four cases of deletion require extra attention. If the value is not found, the leaf node is found, the node with a single child is found, and the node with two children is found. Also find the maximum value in the left subtree for value replacement, this should be the predecessor node, and finally delete the predecessor node.
-
This is best to use post-order traversal. Unfortunately, the interface design of the traversal function passes a value pointing to the node, not a value pointing to the node contained, so we have to write our own.
-
Same as 8.
-
The book uses a macro to make the stack capable of multiple declarations, resolve naming conflicts, generics and other issues. But there are still flaws, for example, the naming of functions of the same type will be consistent. It is a good idea to decompose the stack function into three macros, decoupling. And in daily learning, there is still too little to write for macros, especially the use of macros to write function functions, it is very rare, and you have to accumulate one when you see one. see
demo08.c
Essay
-
Three ways to implement the stack. Realization of static array, dynamic array, and chain stack. Generally, static arrays are selected for simulation in algorithmic problems. The implementation of dynamic arrays involves the use of memory allocation functions and memory leaks that must be prevented. The space utilization of the chain stack is quite high, but it involves a lot of related operations that change the pointer pointing to. I personally think that the efficiency is definitely not as high as that of the static array!
-
The three implementations of queues are the same as stacks. However, due to the large amount of space wasted when the static array implements the queue, the loop array is used for optimization, and we generally call it a circular queue here. Use two pointers plus a spare array element to judge empty and full. In fact, it is quite fragrant to use a counting variable. Two circular queue pointers, wherein
rear
the pointer is initialized to 0, is added to the first element and time can befront
pointers to the same location. Therefore, eachpush
time the element is firstrear
pointer Xianxiang again after moving the position of an assignment. Full of empty sentenced sentenced two formulas is quite clever:(rear+1)%SIZE==front
that is, the queue is empty, this can be seen from the initialization out, start a queue is emptyfront
is 1,rear
it is 0. Whenrear == front
, the entire queue is only one element, and then after a teamfront++
is inrear
front of a position, so the formula sentenced empty establishment. And sincerear
, andfront
in the interval of at least one empty element, then when the(read+2)%SIZE == front
time to explain this queue is full. The dynamic queue is not implemented in the book, and the chain queue is relatively simple. -
Tree, in this book that focused on the
BST
tree, you can refer to my blog, takeC++
to achieve: [C ++ Series] 76. Detailed BST binary search tree , with explained in this columnAVL
trees and red-black tree. ChainBST
pretty good, eliminating an array of space-utilization problem. Among them,P377
the insertion function of the chain binary tree uses two pointers, one of which is a first-level pointer and a second-level pointer. The first-level pointer stores the node in the tree currently traversed, and the second-level pointer points to the left and right child pointers of the current node. space. All insertions under the binary search tree will only be inserted in the leaf nodes. -
Improved implementation raised
ADT
three issues: Users declare more than one stack, support for generics, to resolve naming conflicts. In theC++ STL
middle, with the template, these problems will be ironed. In theC
language macros can be used to solve this problem. Inch14
the already mentioned: the type of macro is irrelevant. And implement a support any type ofmalloc
function. The book achieved using macro parameters regardless of the type, the code written in the stack of a macro, the user identifier and add the name customizable by##
way added to the end of the function name. UseC
language to implement generics is very difficult, however, object-oriented language for generics have a good support.
doubt
-
The chained implementation of stacks, queues,
BST
etc. are actually quite rusty, and they have not been written in the past. Static arrays are the most written. -
On the use of macros to implement
C
generics in the language of study it is worth considering learning things! -
The data structure is much more problematic.