From 80057a1ea9cd717f3a8c78d3d1c70024513b509c Mon Sep 17 00:00:00 2001 From: Antti H S Laaksonen Date: Fri, 24 Feb 2017 20:22:45 +0200 Subject: [PATCH] Better array representation --- chapter18.tex | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/chapter18.tex b/chapter18.tex index 732d404..2be2193 100644 --- a/chapter18.tex +++ b/chapter18.tex @@ -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);