6.22. 练习¶
6.22. Exercises
-
绘制以下树函数调用产生的树结构:
>>> r = BinaryTree(3) >>> insert_left(r, 4) [3, [4, [], []], []] >>> insert_left(r, 5) [3, [5, [4, [], []], []], []] >>> insert_right(r, 6) [3, [5, [4, [], []], []], [6, [], []]] >>> insert_right(r, 7) [3, [5, [4, [], []], []], [7, [], [6, [], []]]] >>> set_root_val(r, 9) >>> insert_left(r, 11) [9, [11, [5, [4, [], []], []], []], [7, [], [6, [], []]]]
-
追踪创建表达式树的算法,表达式为 :math:
(4 * 8) / 6 - 3
。 -
考虑以下整数列表:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]。展示插入这些整数后的二叉搜索树。
-
考虑以下整数列表:[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]。展示插入这些整数后的二叉搜索树。
-
生成一个随机整数列表。展示插入这些整数后的二叉堆树。
-
使用上一个问题中的列表,展示使用该列表作为
heapify
方法参数后得到的二叉堆树。展示树的形式和列表形式。 -
绘制以下键按给定顺序插入后的二叉搜索树:68, 88, 61, 89, 94, 50, 4, 76, 66 和 82。
-
生成一个随机整数列表。绘制插入这些整数后的二叉搜索树。
-
考虑以下整数列表:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]。展示插入这些整数后的二叉堆。
-
考虑以下整数列表:[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]。展示插入这些整数后的二叉堆。
-
考虑我们用于实现二叉树遍历的两种不同技术。为什么在将
preorder
实现为方法时必须在调用之前进行检查,而当将其实现为函数时可以在调用内部进行检查? -
展示构建以下二叉树所需的函数调用。
-
给定以下树,执行适当的旋转操作使其恢复平衡。
-
使用以下内容作为起点,推导出节点 D 更新后的平衡因子的方程。
-
扩展
build_parse_tree
函数以处理数学表达式中没有每个字符之间的空格的情况。 -
修改
build_parse_tree
和evaluate
函数以处理布尔语句(and
,or
和not
)。请记住,not
是一个一元操作符,因此这会使您的代码变得复杂一些。 -
使用
find_successor
方法,编写一个非递归的二叉搜索树中序遍历。 -
一个 线程化 二叉树为每个节点维护到其后继的引用。修改二叉搜索树的代码以使其成为线程化的,然后为线程化的二叉搜索树编写一个非递归的中序遍历方法。
-
修改我们对二叉搜索树的实现,以正确处理重复的键。也就是说,如果树中已经存在一个键,那么新的负载应该替换旧的负载,而不是添加另一个具有相同键的节点。
-
创建一个具有有限堆大小的二叉堆。换句话说,堆只跟踪最重要的 :math:
n
项。如果堆的大小增长到超过 :math:n
项,则最不重要的项将被丢弃。 -
清理
print_exp
函数,以便它不再在每个数字周围包含额外的一对括号。 -
使用
heapify
方法,编写一个排序函数,该函数可以在 :math:O(n\log{n})
时间内对列表进行排序。 -
编写一个函数,该函数接受一个数学表达式的解析树,并计算该表达式相对于某个变量的导数。
-
实现一个作为最大堆的二叉堆。
-
使用
BinaryHeap
类,实现一个名为PriorityQueue
的新类。您的PriorityQueue
类应实现构造函数以及enqueue
和dequeue
方法。 -
为 AVL 树实现
delete
方法。
-
Draw the tree structure resulting from the following set of tree function calls:
>>> r = BinaryTree(3) >>> insert_left(r, 4) [3, [4, [], []], []] >>> insert_left(r, 5) [3, [5, [4, [], []], []], []] >>> insert_right(r, 6) [3, [5, [4, [], []], []], [6, [], []]] >>> insert_right(r, 7) [3, [5, [4, [], []], []], [7, [], [6, [], []]]] >>> set_root_val(r, 9) >>> insert_left(r, 11) [9, [11, [5, [4, [], []], []], []], [7, [], [6, [], []]]]
-
Trace the algorithm for creating an expression tree for the expression :math:
(4 * 8) / 6 - 3
. - Consider the following list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. Show the binary search tree resulting from inserting the integers in the list.
- Consider the following list of integers: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]. Show the binary search tree resulting from inserting the integers in the list.
- Generate a random list of integers. Show the binary heap tree resulting from inserting the integers on the list one at a time.
- Using the list from the previous question, show the binary heap tree resulting from using the list as a parameter to the
heapify
method. Show both the tree and list form. - Draw the binary search tree that results from inserting the following keys in the order given: 68, 88, 61, 89, 94, 50, 4, 76, 66, and 82.
- Generate a random list of integers. Draw the binary search tree resulting from inserting the integers on the list.
- Consider the following list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. Show the binary heap resulting from inserting the integers one at a time.
- Consider the following list of integers: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]. Show the binary heap resulting from inserting the integers one at a time.
-
Consider the two different techniques we used for implementing traversals of a binary tree. Why must we check before the call to
preorder
when implementing it as a method, whereas we could check inside the call when implementing it as a function? -
Show the function calls needed to build the following binary tree.
-
Given the following tree, perform the appropriate rotations to bring it back into balance.
-
Using the following as a starting point, derive the equation that gives the updated balance factor for node D.
-
Extend the
build_parse_tree
function to handle mathematical expressions that do not have spaces between every character. - Modify the
build_parse_tree
andevaluate
functions to handle Boolean statements (and
,or
, andnot
). Remember thatnot
is a unary operator, so this will complicate your code somewhat. - Using the
find_successor
method, write a non-recursive inorder traversal for a binary search tree. - A threaded binary tree maintains a reference from each node to its successor. Modify the code for a binary search tree to make it threaded, then write a non-recursive inorder traversal method for the threaded binary search tree.
- Modify our implementation of the binary search tree so that it handles duplicate keys properly. That is, if a key is already in the tree then the new payload should replace the old rather than add another node with the same key.
- Create a binary heap with a limited heap size. In other words, the heap only keeps track of the :math:
n
most important items. If the heap grows in size to more than :math:n
items the least important item is dropped. - Clean up the
print_exp
function so that it does not include an extra set of parentheses around each number. - Using the
heapify
method, write a sorting function that cansort a list in :math:O(n\log{n})
time. - Write a function that takes a parse tree for a mathematical expression and calculates the derivative of the expression with respect to some variable.
- Implement a binary heap as a max heap.
- Using the
BinaryHeap
class, implement a new class calledPriorityQueue
. YourPriorityQueue
class should implement the constructor plus theenqueue
anddequeue
methods. - Implement the
delete
method for an AVL tree.
创建日期: 2024年9月9日