Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly display my AVL Tree in LaTex? A solo left-child hangs straight down

The below code almost works perfectly, however the child of 9, 7, hangs straight down instead of as a left-child. How can I correct this?

\usepackage{tikz}
\usepackage{xytree}
\begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
    \node [circle,draw] {4}
      child {
        node [circle,draw] {2}
            child {node [circle,draw] {1}
       }
        child {
            node [circle,draw]{3}
        }
      }
      child {node [circle,draw] {6}
        child {node [circle,draw]  {5}
        }
      child {node [circle,draw]  {9}
        child {node [circle, draw]  {7}}
      }
    };

\end{tikzpicture}}

Thanks, CB

like image 588
cb. Avatar asked Mar 16 '10 13:03

cb.


2 Answers

The code below works for me. It is based on your code, with the changes

1) use the tikz library trees and 2) change of the formatting of a single node (node 7)

For more information see the tikz manual

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
    \node [circle,draw] {4}
      child {
        node [circle,draw] {2}
            child {node [circle,draw] {1}
       }
        child {
            node [circle,draw]{3}
        }
      }
      child {node [circle,draw] {6}
        child {node [circle,draw]  {5}
        }
      child {node [circle,draw]  {9}
        child[grow via three points={one child at (-1,-1) and two children at (-.5,1) and (.5,1)}] {node [circle, draw]  {7}}
      }
    };

\end{tikzpicture}

\end{document}
like image 190
midtiby Avatar answered Oct 13 '22 18:10

midtiby


After consulting with the tikz manual as suggested, I was able to correct this as follows.

\begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
    \node [circle,draw] {4}
      child {
        node [circle,draw] {2}
            child {node [circle,draw] {1}
       }
        child {
            node [circle,draw]{3}
        }
      }
      child {node [circle,draw] {6}
        child {node [circle,draw]  {5}
        }
      child {node [circle,draw]  {9}
        child {node [circle, draw]  {7}} 
        child [missing]
        }
    };

    \end{tikzpicture}
like image 43
cb. Avatar answered Oct 13 '22 20:10

cb.