Drawing an AVL Tree with tikzpicture in Latex - latex

I hope someone can help me. I am trying to draw an AVL Tree with Tikzpicture. I must say I am not very familiar in how to modify Tikz. In row 3 both the childs lay over each other. How can I avoid this, so that they are next to each other not on each other? I have attached the code I was using for this drawing. Many thanks in advance.
\documentclass[11pt, a4paper]{book} % add parameters to the document
\usepackage{fullpage}
\usepackage[utf8]{inputenc}
\usepackage{tikz} % Graphen zeichnen
\begin{document}
\begin{tikzpicture}[
edge from parent path=
{(\tikzparentnode.south) .. controls +(0,0) and +(0,0)
.. (\tikzchildnode.north)},
every node/.style={draw,circle},
label distance=-1mm
]
\node [label=330:$0$]{7}
child {node[label=330:$0$] {2}
child {node[label=330:$0$] {1}}
child {node[label=330:$0$] {3}}}
child {node[label=330:$0$] {24}
child {node[label=330:$0$] {15}}
child {node[label=330:$0$] {42}}
};
\end{tikzpicture}
\end{document}

One possible solution could be to change the sibling distance like this:
\documentclass[11pt, a4paper]{book} % add parameters to the document
\usepackage{fullpage}
\usepackage[utf8]{inputenc}
\usepackage{tikz} % Graphen zeichnen
\begin{document}
\begin{tikzpicture}[
edge from parent path=
{(\tikzparentnode.south) .. controls +(0,0) and +(0,0)
.. (\tikzchildnode.north)},
every node/.style={draw,circle},
label distance=-1mm,
level 1/.style={sibling distance=30mm},
level 2/.style={sibling distance=15mm}
]
\node [label=330:$0$]{7}
child {node[label=330:$0$] {2}
child {node[label=330:$0$] {1}}
child {node[label=330:$0$] {3}}}
child {node[label=330:$0$] {24}
child {node[label=330:$0$] {15}}
child {node[label=330:$0$] {42}}
};
\end{tikzpicture}
\end{document}

Related

circuitikz - move current labels closer to current arrow

how can I move the current label closer to the current arrow. I.e. I want to move the i_1 closer to the arrow.
MWE:
\documentclass{standalone}
\usepackage[european,cuteinductors,fetbodydiode,straightvoltages]{circuitikz}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[scale=0.65, arrowmos]
\coordinate (zero) at (0,0);
\draw (zero) to[R,-*,R=$R_1$,i>_=$i_1$] ++(2.75,2);
\end{tikzpicture}
\end{figure}
\end{document}
I tried to put a \vspace in front of the label but it didn't work.
I propose two solutions here: the first one is using a fake label and then setting the label manually (with a lot of flexibility), or using the provided styling for labels. Details in comments; you need a quite recent circuitikz for using this solution (>=1.4.2).
\documentclass{standalone}
\usepackage[european,cuteinductors,fetbodydiode,straightvoltages]{circuitikz}
\begin{document}
\centering
\begin{tikzpicture}[scale=0.65, arrowmos]
\coordinate (zero) at (0,0);
% First option
% use a blank label for the current, and name the component
\draw (zero) to[R,-*,R=$R_1$,i>_=~,name=myI] ++(2.75,2);
% manually place the label where you like
% myIcurrent is the normal position where the blank label is set
\node[below=1mm, anchor=center, red] at (myIcurrent) {$i_1$};
% you can also use bipole current style to change inner sep;
\draw (2,0) to[bipole current style={inner sep=0pt}, R,-*,R=$R_1$,i>_=$i_1$] ++(2.75,2);
\end{tikzpicture}
\end{document}
See circuitikz manual, https://texdoc.org/serve/circuitikz/0#subsection.5.6 .

How do I, in a TikZ/er picture, omit the "diamond" symbol of a relationship and control placement of text along the relationship's path?

I have the following LaTeX file:
\documentclass[border=10pt]{standalone}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{er,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=1.5cm]
\node[entity] (node1) {Fancy Node One};
\node[relationship] (rel1) [right = of node1] {has};
\node[entity] (node2) [right = of rel1] {Fancy Node Two};
\path (rel1) edge node {1} (node1)
edge node {n} (node2);
\end{tikzpicture}
\end{document}
When I render it it looks like this:
Is it possible to a) omit the "diamond" symbol entirely and b) control placement of "1" and "n"? What I want in this particular example is to have the "1" above the line, and both "1" and "n" closer to the nodes -- basically, this:
(Not sure if this would be regarded as valid ER diagram notation, but that's a separate concern.)
If it's not possible or impractical with TikZ and/or er, I'm open to suggestions for other LaTeX packages suitable for making ER diagrams. (If it is possible, I'd also appreciate pointers to documentation on this.)
Quick hack:
\documentclass[border=10pt]{standalone}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{er,positioning}
\begin{document}
\begin{tikzpicture}[auto,node distance=1.5cm]
\node[entity] (node1) {Fancy Node One};
\node[entity] (node2) [right = 3cm of node1] {Fancy Node Two};
\draw (node1.east) -- (node2.west);
\node[anchor=south west] at (node1.east) {1};
\node[anchor=south east] at (node2.west) {n};
\end{tikzpicture}
\end{document}

Tikz-Network Draw Edge without Node (Invisible Node) Latex

I want to draw an edge from a point below a node, to the node. Here is what I have so far:
\begin{tikzpicture}
\Vertex[x=0,label=1, size=1.2]{A} \Vertex[x=3,size=1.2,label=2]{B} \Vertex[x=6,size=1.2,label=3]{C}
\Edge[Direct,label=0.4,bend=20,fontscale=1.2](A)(B)
\Edge[Direct,label=0.6,bend=30](A)(C)
\Edge[Direct,label=0.25,bend=20](C)(B)
\end{tikzpicture}
Which yields:
I want to draw an arrow from about 3mm below node 1 directed to node 1
\documentclass{article}
\usepackage{tikz-network}
\begin{document}
\begin{tikzpicture}
\Vertex[x=0,label=1, size=1.2]{A} \Vertex[x=3,size=1.2,label=2]{B} \Vertex[x=6,size=1.2,label=3]{C}
\Edge[Direct,label=0.4,bend=20,fontscale=1.2](A)(B)
\Edge[Direct,label=0.6,bend=30](A)(C)
\Edge[Direct,label=0.25,bend=20](C)(B)
\end{tikzpicture}
\end{document}
With the positioning library of tikz, you could create a dummy node below A at whatever distance you like and then draw the edge from this dummy node to A
\documentclass{article}
\usepackage{tikz-network}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\Vertex[x=0,label=1, size=1.2]{A}
\Vertex[x=3,size=1.2,label=2]{B}
\Vertex[x=6,size=1.2,label=3]{C}
\node[below=1.5cm of A] (D) {};
\Edge[Direct,label=0.4,bend=20,fontscale=1.2](A)(B)
\Edge[Direct,label=0.6,bend=30](A)(C)
\Edge[Direct,label=0.25,bend=20](C)(B)
\Edge[Direct,label=0.25](D)(A)
\end{tikzpicture}
\end{document}

How can I apply a style to existing tikz node on specific slides

This is what I'm trying to do
\begin{tikzpicture}
[node distance = 1cm, auto,font=\footnotesize,
% STYLES
every node/.style={node distance=1.3cm},
comment/.style={rectangle, inner sep= 5pt, text width=4cm, node distance=0.25cm, font=},
module/.style={rectangle, drop shadow, draw, fill=black!10, inner sep=5pt, text width=3cm, text badly centered, minimum height=0.8cm, font=\bfseries\footnotesize\sffamily,rounded corners},
selected/.style={fill=red!40}]
\node [module] (nodeA) {node A};
\node [module, below of=nodeA] (nodeA) {node B};
\only<1>{
\node [comment, text width=6cm, right=0.25 of nodeA] {short description of Node A};
\node [comment, text width=6cm, right=0.25 of nodeB] {short description of Node B};
}
\only<2>{
\node [selected] (nodeA) {};
\node [comment, text width=6cm, right=0.25 of nodeA] {long description of node A};
}
\only<3>{
\node [selected] (nodeB) {};
\node [comment, text width=6cm, right=0.25 of nodeA] {long description of node B};
}
\end{tikzpicture}
The problem is
\node [selected] (nodeB) {};
creates a new node, but I want it to apply the style for the existing node. Is there any way to do so?
Of course I could have copies of every node in selected state and not-selected state, but I really want to have a normal solution.
I don't think you can do this the way you want to (assuming I understand the question correctly), because once a node is drawn, there's no way to change its appearance. I'd suggest using Beamer's \alt macro:
\alt<2>{\node[module,selected] at (nodeA) {node A};}{\node[module] at (nodeA) {node A};}
\alt<3>{\node[module,selected] at (nodeB) {node B};}{\node[module] at (nodeB) {node B};}
\node[comment,text width=6cm,right=0.25 of nodeA]{\alt<2>{short description}{long description}};
\node[comment,text width=6cm,right=0.25 of nodeB]{\alt<3>{short description}{long description}};
Or something like that (you might have to tinker with the semicolons to get it to work, I can't test that at the moment).
Another option would be to actually just draw a new node. If you include
\node[module,selected] at (nodeA) {node A};
inside \only<2>, that will draw a node that looks just like node A, except with a red background, at the same position at node A. The new node will cover up the original node A.
Sometimes, to avoid repetitions, it may be nice to do something like this:
% #1 Overlay specs.
% #2 Style name.
% #4 Style properties.
\def\onlystyle<#1>#2#3{%
\alt<#1>{%
\tikzset{#2/.style = {#3}}
}{%
\tikzset{#2/.style = {}}
}%
}
Then, if you put, for example, this within a frame:
\onlystyle<2>{selected}{fill = red}
the style selected will be defined as fill = red on the second slide of the animation, and as a style with no effect whatsoever on every other slide. Then, you can write a readable figure such as:
\begin{tikzpicture}
\node at (0, 0) {A};
\node[selected] at (1, 0) {B};
\node at (2, 0) {C};
\end{tikzpicture}
and the “B” node will be highlighted on the second slide. This way, you don't have to copy-paste tons of node definitions.
Of course, it cannot be applied to every single animation need, but I like to keep this technique up my sleeve.
I found yet another solution, which has advantages (more functionality!) compared to all previous solutions, including the one I posted before.
First I mention the improved solution, then I explain why it actually shows more functionality than all other solutions.
The following solution, adapted from How can I make Beamer overlays with TikZ node attributes?, uses an additional tikz library and uses a parameter for the slide number-dependent attribute (for the slide numbers, of course). Note that the tikz setting has to be done outside the frame now.
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}
\begin{frame}{With code/node duplication (explicit frame numbers)}
\begin{tikzpicture}
[every node/.style={draw,circle},
redStyle/.style={fill=red},
blueStyle/.style={fill=blue}]
\node<1> [] (A) {A}; % no style
\node<2> [redStyle] (A) {A}; % red style
\node<3> [blueStyle] (A) {A}; % blue style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\tikzset{
every node/.style={draw,circle},
redStyle/.style={fill=red}, redStyle on/.style={alt=#1{redStyle}{}},
blueStyle/.style={fill=blue}, blueStyle on/.style={alt=#1{blueStyle}{}}}
\begin{frame}{\textbf{Without} code/node duplication (explicit frame numbers)}
\begin{tikzpicture}
\node [redStyle on=<2>,blueStyle on=<3>] (A) {A};
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\tikzset{
every node/.style={draw,circle},
redStyle/.style={fill=red}, redStyle on/.style={alt=#1{redStyle}{}},
blueStyle/.style={fill=blue}, blueStyle on/.style={alt=#1{blueStyle}{}}}
\begin{frame}{\textbf{Without} code/node duplication (relative frame numbers)}
\begin{tikzpicture}
\node [redStyle on=<+(1)>,blueStyle on=<+(1)>] (A) {A};
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\end{document}
Now the explanation why this solution is the best mentioned so far, i.e. which additional features it has and why (or when) they are relevant. Well, simply because the respective property does not have to show at the same slide number for different usages within the same frame. Simple example:
Let's say you have a simple tree, i.e. nodes with directed edges connecting them. Say you want to make some of the edges bold in specific slides. Of course you have multiple edges, so clearly they do not all become bold at the very same time! Some edges get bold at frame numbers m to n, others get bold at x and y. This can now be trivially realized with using the node (or edge, in this case) properties timedBold on=<m-n> and timedBold on=<x,y>.
Note that there is yet another possibility, which to me seems a bit better than the previous two suggestions, because it has less code duplication (compared to David Z's solution), and because (compared to Alice M.'s solution) you do not need to define a new command that you had to define outside the frame (though defining this additional command is probably not an issue to anybody). In principle, the following suggestion seems to be closely related to the suggestion by Alice M.
Anyway, the solution is a direct application of How to modify a node in TikZ when using beamer's overlays.
Is basically just re-defines a style based on the frame number. See the following minimal example, second or third frame. (I re-did the minimal example since I regard the given one extremely complicated; it was certainly not a minimal example, and also not working on its own.)
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}{With code/node duplication (explicit frame numbers)}
\begin{tikzpicture}
[every node/.style={draw,circle},
redStyle/.style={fill=red},
blueStyle/.style={fill=blue}]
\node<1> [] (A) {A}; % no style
\node<2> [redStyle] (A) {A}; % red style
\node<3> [blueStyle] (A) {A}; % blue style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\begin{frame}{\textbf{Without} code/node duplication (explicit frame numbers)}
\only<1>{\tikzset{colorStyle/.style={}}} % no style
\only<2>{\tikzset{colorStyle/.style={fill=red}}} % red style
\only<3>{\tikzset{colorStyle/.style={fill=blue}}} % blue style
\begin{tikzpicture}
[every node/.style={draw,circle}]
\node [colorStyle] (A) {A}; % frame-dependent style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\begin{frame}{\textbf{Without} code/node duplication (relative frame numbers)}
\only<+>{\tikzset{colorStyle/.style={}}} % no style
\only<+>{\tikzset{colorStyle/.style={fill=red}}} % red style
\only<+>{\tikzset{colorStyle/.style={fill=blue}}} % blue style
\begin{tikzpicture}
[every node/.style={draw,circle}]
\node [colorStyle] (A) {A}; % frame-dependent style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\end{document}

Positioning of hyperlinked shapes with matrix in tikz

I can draw a hyperlinked shape in tikz using the following code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}
\node {%
\href{http://www.stackoverflow.com}{%
\begin{tikzpicture}
\filldraw[blue] circle(1cm) node [white] {Click};
\end{tikzpicture}}};
\end{tikzpicture}
\end{document}
Now I would like to organize my shapes using the matrix, and have one of the shapes hyperlinked. It almost works, but I am not able to align the hyperlinked shape with the rest of the shapes, and it is bigger than the other shapes:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{hyperref}
\begin{tikzpicture}
\matrix [matrix of nodes, row sep = 1cm, column sep=1cm, nodes={circle, draw}]
{% First row:
1 & 2 \\
% second row:
\path node {\href{http://www.stackoverflow.com}{%
\begin{tikzpicture}
\node {3};
\end{tikzpicture}}}; & 4\\
};
\end{tikzpicture}
\end{document}
I get the following result:
(source: picture.im)
My question is: How could I align shape 3 in the picture above with the other shapes, and get rid of the outer circle?
I think you're aiming too high and your second {tikzpicture} is messing up your layout.
What do you think about the code below? Is that what you've been looking for?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage[pdftex,active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes, row sep = 1cm, column sep=1cm, nodes={circle, draw}]
{%
1 & 2\\%
\href{http://stackoverflow.com}{3} & 4\\%
};
\end{tikzpicture}
\end{document}
BTW: the \PreviewEnvironment{tikzpicture} is not really needed, but it makes for a nice, cropped pdf...

Resources