# 计算机代写|算法分析作业代写Introduction to Algorithms代考|CS120

## 计算机代写|算法分析作业代写Introduction to Algorithms代考|Average node depth in a randomly built binary search tree

A randomly huilt hinary search tree on $n$ keys is a binary search tree created by starting with an empty tree and inserting the keys in random order, where each of the $n$ ! permutations of the keys is equally likely. In this problem, you will prove that the average depth of a node in a randomly built binary search tree with $n$ nodes is $O(\lg n)$. The technique reveals a surprising similarity between the building of a binary search tree and the execution of RANDOMIZED-QUICKSORT from Section 7.3.

Denote the depth of any node $x$ in tree $T$ by $d(x, T)$. Then the total path length $P(T)$ of a tree $T$ is the sum, over all nodes $x$ in $T$, of $d(x, T)$.
a. Argue that the average depth of a node in $T$ is
$$\frac{1}{n} \sum_{x \in T} d(x, T)=\frac{1}{n} P(T) .$$
Thus, you need to show that the expected value of $P(T)$ is $O(n \lg n)$.

b. Let $T_L$ and $T_R$ denote the left and right subtrees of tree $T$, respectively. Argue that if $T$ has $n$ nodes, then
$$P(T)=P\left(T_L\right)+P\left(T_R\right)+n-1 .$$
c. Let $P(n)$ denote the average total path length of a randomly built binary search tree with $n$ nodes. Show that
$$P(n)=\frac{1}{n} \sum_{i=0}^{n-1}(P(i)+P(n-i-1)+n-1) .$$
d. Show how to rewrite $P(n)$ as
$$P(n)=\frac{2}{n} \sum_{k=1}^{n-1} P(k)+\Theta(n) .$$
$e$. Recalling the alternative analysis of the randomized version of quicksort given in Problem 7-3, conclude that $P(n)=O(n \lg n)$.
Each recursive invocation of randomized quicksort chooses a random pivot element to partition the set of elements being sorted. Each node of a binary search tree partitions the set of elements that fall into the subtree rooted at that node.
$f$. Describe an implementation of quicksort in which the comparisons to sort a set of elements are exactly the same as the comparisons to insert the elements into a binary search tree. (The order in which comparisons are made may differ, but the same comparisons must occur.)

## 计算机代写|算法分析作业代写Introduction to Algorithms代考|Properties of red-black trees

A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. By constraining the node colors on any simple path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other, so that the tree is approximately balanced. Indeed, as we’re about to see, the height of a red-black tree with $n$ keys is at most $2 \lg (n+1)$, which is $O(\lg$ $n$ ).

Each node of the tree now contains the attributes color, key, left, right, and $p$. If a child or the parent of a node does not exist, the corresponding pointer attribute of the node contains the value NIL. Think of these NILs as pointers to leaves (external nodes) of the binary search tree and the normal, key-bearing nodes as internal nodes of the tree.

A red-black tree is a binary search tree that satisfies the following redblack properties:

1. Every node is either red or black.
2. The root is black.
3. Every leaf (NIL) is black.
4. If a node is red, then both its children are black.
5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
Figure 13.1(a) shows an example of a red-black tree.
As a matter of convenience in dealing with boundary conditions in red-black tree code, we use a single sentinel to represent NIL (see page 262). For a red-black tree $T$, the sentinel T.nil is an object with the same attributes as an ordinary node in the tree. Its color attribute is BLACK, and its other attributes-p, left, right, and key-can take on arbitrary values. As Figure 13.1(b) shows, all pointers to NIL are replaced by pointers to the sentinel T.nil.

# 算法分析代考

## 计算机代写|算法分析作业代写Introduction to Algorithms代考|Average node depth in a randomly built binary search tree

$$\frac{1}{n} \sum_{x \in T} d(x, T)=\frac{1}{n} P(T) .$$

b. 让 $T_L$ 和 $T_R$ 表示树的左右子树 $T$ ，分别。认为如果 $T$ 有 $n$ 节点，那么
$$P(T)=P\left(T_L\right)+P\left(T_R\right)+n-1 .$$
C。让 $P(n)$ 表示随机构建的二叉搜索树的平均总路径长度 $n$ 节点。显示
$$P(n)=\frac{1}{n} \sum_{i=0}^{n-1}(P(i)+P(n-i-1)+n-1) .$$
d. 显示如何重写 $P(n)$ 作为
$$P(n)=\frac{2}{n} \sum_{k=1}^{n-1} P(k)+\Theta(n)$$
$e$. 回顾问题 7-3 中给出的快速排序的随机版本的替代分析，得出结论 $P(n)=O(n \lg n)$.

f. 描述快速排序的一个实现，其中对一组元㨞进行排序的比较与将元㨞揷入二叉搜索树的比较完全相

## 计算机代写|算法分析作业代写Introduction to Algorithms代考|Properties of red-black trees

1. 每个节点要么是红色要么是黑色。
2. 根是黑色的。
3. 每片叶子 (NIL) 都是黑色的。
4. 如果一个节点是红色的，那么它的两个孩子都是黑色的。
5. 对于每个节点，从该节点到后代叶子的所有简单路径都包含相同数量的黑色节点。
图 13.1(a) 显示了一个红黑树的例子。
为了方便处理红黑树代码中的边界条件，我们使用单个哨兵来表示 NIL（参见第 262 页）。对于红黑树吨，哨兵 T.nil 是一个对象，与树中的普通节点具有相同的属性。它的颜色属性是 BLACK，它的其他属性——p、left、right 和 key——可以取任意值。如图 13.1(b) 所示，所有指向 NIL 的指针都被指向哨兵 T.nil 的指针所取代。

myassignments-help数学代考价格说明

1、客户需提供物理代考的网址，相关账户，以及课程名称，Textbook等相关资料~客服会根据作业数量和持续时间给您定价~使收费透明，让您清楚的知道您的钱花在什么地方。

2、数学代写一般每篇报价约为600—1000rmb，费用根据持续时间、周作业量、成绩要求有所浮动(持续时间越长约便宜、周作业量越多约贵、成绩要求越高越贵)，报价后价格觉得合适，可以先付一周的款，我们帮你试做，满意后再继续，遇到Fail全额退款。

3、myassignments-help公司所有MATH作业代写服务支持付半款，全款，周付款，周付款一方面方便大家查阅自己的分数，一方面也方便大家资金周转，注意:每周固定周一时先预付下周的定金，不付定金不予继续做。物理代写一次性付清打9.5折。

Math作业代写、数学代写常见问题

myassignments-help擅长领域包含但不是全部: