Better array representation
This commit is contained in:
parent
9bc5daa8ea
commit
80057a1ea9
|
@ -266,14 +266,18 @@ is $3+4+3+1=11$.
|
|||
\end{center}
|
||||
|
||||
The idea is to construct a tree traversal array that contains
|
||||
three values for each node: (1) the identifier of the node,
|
||||
(2) the size of the subtree, and (3) the value of the node.
|
||||
three values for each node: the identifier of the node,
|
||||
the size of the subtree, and the value of the node.
|
||||
For example, the array for the above tree is as follows:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=0.7]
|
||||
\draw (0,1) grid (9,-2);
|
||||
|
||||
\node[left] at (-1,0.5) {node id};
|
||||
\node[left] at (-1,-0.5) {subtree size};
|
||||
\node[left] at (-1,-1.5) {node value};
|
||||
|
||||
\node at (0.5,0.5) {$1$};
|
||||
\node at (1.5,0.5) {$2$};
|
||||
\node at (2.5,0.5) {$6$};
|
||||
|
@ -330,6 +334,10 @@ can be found as follows:
|
|||
\fill[color=lightgray] (4,-1) rectangle (8,-2);
|
||||
\draw (0,1) grid (9,-2);
|
||||
|
||||
\node[left] at (-1,0.5) {node id};
|
||||
\node[left] at (-1,-0.5) {subtree size};
|
||||
\node[left] at (-1,-1.5) {node value};
|
||||
|
||||
\node at (0.5,0.5) {$1$};
|
||||
\node at (1.5,0.5) {$2$};
|
||||
\node at (2.5,0.5) {$6$};
|
||||
|
@ -438,6 +446,10 @@ For example, the following array corresponds to the above tree:
|
|||
\begin{tikzpicture}[scale=0.7]
|
||||
\draw (0,1) grid (9,-2);
|
||||
|
||||
\node[left] at (-1,0.5) {node id};
|
||||
\node[left] at (-1,-0.5) {subtree size};
|
||||
\node[left] at (-1,-1.5) {path sum};
|
||||
|
||||
\node at (0.5,0.5) {$1$};
|
||||
\node at (1.5,0.5) {$2$};
|
||||
\node at (2.5,0.5) {$6$};
|
||||
|
@ -491,6 +503,10 @@ the array changes as follows:
|
|||
\fill[color=lightgray] (4,-1) rectangle (8,-2);
|
||||
\draw (0,1) grid (9,-2);
|
||||
|
||||
\node[left] at (-1,0.5) {node id};
|
||||
\node[left] at (-1,-0.5) {subtree size};
|
||||
\node[left] at (-1,-1.5) {path sum};
|
||||
|
||||
\node at (0.5,0.5) {$1$};
|
||||
\node at (1.5,0.5) {$2$};
|
||||
\node at (2.5,0.5) {$6$};
|
||||
|
@ -710,13 +726,16 @@ in the array and there are a total of $2n-1$
|
|||
nodes in the array.
|
||||
|
||||
We store two values in the array:
|
||||
(1) the identifier of the node, and (2) the level of the
|
||||
the identifier of the node and the level of the
|
||||
node in the tree.
|
||||
The following array corresponds to the above tree:
|
||||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=0.7]
|
||||
|
||||
\node[left] at (-1,1.5) {node id};
|
||||
\node[left] at (-1,0.5) {level};
|
||||
|
||||
\draw (0,1) grid (15,2);
|
||||
%\node at (-1.1,1.5) {\texttt{node}};
|
||||
\node at (0.5,1.5) {$1$};
|
||||
|
@ -781,6 +800,10 @@ can be found as follows:
|
|||
|
||||
\begin{center}
|
||||
\begin{tikzpicture}[scale=0.7]
|
||||
|
||||
\node[left] at (-1,1.5) {node id};
|
||||
\node[left] at (-1,0.5) {level};
|
||||
|
||||
\fill[color=lightgray] (2,1) rectangle (3,2);
|
||||
\fill[color=lightgray] (5,1) rectangle (6,2);
|
||||
\fill[color=lightgray] (2,0) rectangle (6,1);
|
||||
|
|
Loading…
Reference in New Issue