2

I am trying to create a decision tree and I am having trouble trying to colour separately the terminal leaves. The current code I have looks like (ignore the table):

enter image description here

However, I would like to colour the terminal leaves separately, but not sure how to do so since the following line leaf/.style={basic, rounded corners=6pt, thin,align=center, fill=green, text width=1cm}] which corresponds to the leaf only takes one colour value. How can I create a similar decision tree to the one below?

enter image description here

Code:

\documentclass[]{article}


\usepackage{float}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{tikz}

\usetikzlibrary{arrows,shapes,positioning,shadows,trees,patterns}
\definecolor{blue1}{HTML}{0081FF}
\definecolor{grey1}{HTML}{B0B0B0}


\begin{document}

\begin{figure}[]
    \centering
    \subfloat[Oblivious Decision Tree]{
      \resizebox{0.5\textwidth}{!}{
        \begin{tikzpicture}[->,>=stealth',
            level/.style={sibling distance = 5cm/#1, level distance = 2cm},
            basic/.style={draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
            split/.style={basic, rounded corners=2pt, thin, align=center, fill=blue1},
            leaf/.style={basic, rounded corners=6pt, thin,align=center, fill=green, text width=1cm}] 
        \node [split] {$x_1<0.75$}
            child{ node [split] {$x_2<1.25$} 
            child{ node [leaf] {$\omega_{00}$} edge from parent node[above left] {$no$}}
            child{ node [split] {$x_1 < 1.78$}
            child{ node [leaf] {$\omega_{99}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{87}$} edge from parent node[above right] {$yes$}}
            }
            %child{ node [leaf] {$\omega_{01}$} edge from parent node[above right] {$yes$}}                      
            edge from parent node[above left] {$no$}}
            child{ node [split] {$x_2<1.25$}
            child{ node [leaf] {$\omega_{10}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{11}$} edge from parent node[above right] {$yes$}}
            edge from parent node[above right] {$yes$}
            };
        \end{tikzpicture}
      }
    }
    \subfloat[Oblivious Decision Tree]{
        \begin{tabular}{llll}
            \hline
            \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
            \hline
            false & false & 00b & \( w_{00} \) \\
            false & true & 01b & \( w_{01} \) \\
            true & false & 10b & \( w_{10} \) \\
            true & true & 11b & \( w_{11} \) \\
            \hline
        \end{tabular}
    }
  \caption{}
  \label{fig:decision-tree}
\end{figure}



\end{document}

1 Answer 1

3

Since contents of nodes is not visible well and that structure of tree could be more apparent I change node contents to letters A, A1, A2, etc., for the left side of tree and to B, B1 and B2 for the right side of tree. Insert desired content into this nodes should not be big problem

Regarding coloring of nodes: with redefining style leaf to

leaf/.style = {basic, fill=#1}

is simple to add desired color to nodes. As follows from picture of desired tree, the most of the leaf nodes are red, so it is sensible define default color as red and add other colors in exception (see MWE below):

\documentclass{article}

\usepackage{array}
\usepackage{subfig}
\usepackage{tikz}

\usetikzlibrary{arrows,
                patterns,positioning,
                shadows,shapes,
                trees}
\definecolor{blue}{HTML}{0081FF}
\definecolor{grey}{HTML}{B0B0B0}

\begin{document}
    \begin{figure}
    \centering
\subfloat[Oblivious Decision Tree]{
    \begin{tikzpicture}[
        ->,>=stealth',
        level/.style={sibling distance = 3cm/#1, level distance = 10mm},  
        basic/.style = {draw, drop shadow, font=\sffamily\small, text=white,
                        rounded corners=2pt, thin}, % <---
        split/.style = {basic, fill=blue},    % <---
         leaf/.style = {basic, fill=#1},      % <---
       leaf/.default = red,
          lbl/.style = {font=\small, #1}      % <---
                        ]
\node [split] {$x_1<0.75$}
    child{ node [split] {A}%{$x_2<1.25$}
        child{ node [leaf] {A1}%{$\omega_{00}$} 
                edge from parent node[lbl=left] {No}}
        child{ node [split] {A2}%{$x_1 < 1.78$}
             child{ node [leaf] {A21}%{$\omega_{99}$} 
                edge from parent node[lbl=left] {No}}
            child{ node [leaf=green] {A22}%{$\omega_{87}$} 
                edge from parent node[lbl=right] {Yes}}
            edge from parent node[lbl=right] {Yes}
            }
    edge from parent node[lbl=left] {No}
        }
%
    child{ node [split] {B}%{$x_2<1.25$}
        child{ node [leaf=green] {B1}%{$\omega_{10}$} 
            edge from parent node[above left] {No}}
        child{ node [leaf] {B2}%{$\omega_{11}$} 
            edge from parent node[above right] {Yes}}
        edge from parent node[right] {Yes}
        };
    \end{tikzpicture}
    }\hfill
\subfloat[Oblivious Decision Tree]{
    \setlength\extrarowheight{2pt}
    \renewcommand\arraystretch{1.1}
    \small
    \begin{tabular}[b]{cc cc}
        \hline
        \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
        \hline
        false & false & 00b & \( w_{00} \) \\
        false & true  & 01b & \( w_{01} \) \\
        true  & false & 10b & \( w_{10} \) \\
        true  & true  & 11b & \( w_{11} \) \\
        \hline
    \end{tabular}
}
\caption{}
\label{fig:decision-tree}
    \end{figure}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .