documentclass[UTF8]{article} usepackage{tikz} usepackage{CTEX} usepackage{amssymb} usepackage{amsmath} usepackage{xcolor} egin{document} itle{Algorithm homework 2} author{****\ 21617019} date{2016-10-17} maketitle section*{6.3-3} egin{flushleft} 证明:\ $ecause$quad 下标为 i 的父节点的子节点为 2i 和 2i+1 ,所以节点为 n 的叶节点的父节点应该为$lfloor n/2 floor$,叶节点的个数为$Num=n-lfloor n/2 floor=lceil n/2 ceil$。\ 高度为h 的节点个数$Num_h$满足: egin{align*} &Num_0=lceil n/2 ceil\ &Num_{h+1}=lceil Num_h/2 ceil end{align*} $ hereforequad$距离叶节点距离为h,其个数为 $Num/2^h=lceil n/2^{h+1} ceil$ end{flushleft} section*{6.4-1} ikzset{ box/.style={circle, minimum width=5pt, inner sep=3pt, draw=gray,thick, fill=white} } egin{center} %aaaaaaaaaaaaaaaaaaaaaa egin{tikzpicture} ode[box] {25}[sibling distance=80pt] child {node[box] {13}[sibling distance=40pt] child {node[box] {8}[sibling distance=40pt] child {node[box] {5}} child {node[box] {4}} } child {node[box] {7}} } child {node[box] {20}[sibling distance=40pt] child {node[box] {17}} child {node[box] {2}} }; end{tikzpicture}\ a.最大堆 end{center} %bbbbbbbbbbbbbbbbbbbbbbbbbbb egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {20}[sibling distance=80pt] child {node[box] {13}[sibling distance=40pt] child {node[box] {8}[sibling distance=40pt] child {node[box] {5} } } child {node[box] {7}} } child {node[box] {17}[sibling distance=40pt] child {node[box] {4}} child {node[box] {2}} }; end{tikzpicture}\ egin{tikzpicture} ode[box] {25}; end{tikzpicture}\ (b) end{minipage} %ccccccccccccccccccccccccccc egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {17}[sibling distance=80pt] child {node[box] {13}[sibling distance=40pt] child {node[box] {8}[sibling distance=40pt] } child {node[box] {7}} } child {node[box] {5}[sibling distance=40pt] child {node[box] {4}} child {node[box] {2}} }; end{tikzpicture}\ egin{tikzpicture} ode[box] (b) at(4,5){25}; ode[box] (a) at(3,5){20}; end{tikzpicture}\ (c) end{minipage} \%%%%%%%%ddddddddddddddddddddddd egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {13}[sibling distance=80pt] child {node[box] {8}[sibling distance=40pt] child {node[box] {2}[sibling distance=40pt] } child {node[box] {7}} } child {node[box] {5}[sibling distance=40pt] child {node[box] {4}} }; end{tikzpicture}\ egin{tikzpicture} ode[box] (b) at(4,5){25}; ode[box] (a) at(3,5){20}; ode[box] (a) at(2,5){17}; end{tikzpicture}\ (d) end{minipage} %%%%%%%eeeeeeeeeeeeeeeeee egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {8}[sibling distance=80pt] child {node[box] {7}[sibling distance=40pt] child {node[box] {2}[sibling distance=40pt] } child {node[box] {4}} } child {node[box] {5}[sibling distance=40pt] }; end{tikzpicture}\ egin{tikzpicture} ode[box] (b) at(4,5){25}; ode[box] (a) at(3,5){20}; ode[box] (a) at(2,5){17}; ode[box] (a) at(1,5){13}; end{tikzpicture}\ (e) end{minipage} \%%%%%%%%ffffffffffffffffffffffffff egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {7}[sibling distance=80pt] child {node[box] {4}[sibling distance=40pt] child {node[box] {2}[sibling distance=40pt] } } child {node[box] {5}[sibling distance=40pt] }; end{tikzpicture}\ egin{tikzpicture} ode[box] (b) at(4,5){25}; ode[box] (a) at(3,5){20}; ode[box] (a) at(2,5){17}; ode[box] (a) at(1,5){13}; ode[box] (a) at(0,5){8}; end{tikzpicture}\ (f) end{minipage} %%%%ggggggggggggggggggggggggggggggg egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {5}[sibling distance=80pt] child {node[box] {4}[sibling distance=40pt] } child {node[box] {2}[sibling distance=40pt] }; end{tikzpicture}\ egin{tikzpicture} ode[box] (b) at(5,5){25}; ode[box] (a) at(4,5){20}; ode[box] (a) at(3,5){17}; ode[box] (a) at(2,5){13}; ode[box] (a) at(1,5){8}; ode[box] (a) at(0,5){7}; end{tikzpicture}\ (g) end{minipage} \%%%%%%%%hhhhhhhhhhhhhhhhhhhhhhhhhhh egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {4}[sibling distance=80pt] child {node[box] {2}[sibling distance=40pt] } ; end{tikzpicture}\ egin{tikzpicture} ode[box] (b) at(5,5){25}; ode[box] (a) at(4.5,5){20}; ode[box] (a) at(4,5){17}; ode[box] (a) at(3.5,5){13}; ode[box] (a) at(3,5){8}; ode[box] (a) at(2.5,5){7}; ode[box] (a) at(2,5){5}; end{tikzpicture}\ (h) end{minipage} %%%%%%%iiiiiiiiiiiiiiiiiiiiiii egin{minipage}[t]{0.6 extwidth} centering egin{tikzpicture} ode[box] {2}; end{tikzpicture}\ egin{tikzpicture} ode[box] (b) at(5.6,5){25}; ode[box] (a) at(4.8,5){20}; ode[box] (a) at(4,5){17}; ode[box] (a) at(3.2,5){13}; ode[box] (a) at(2.4,5){8}; ode[box] (a) at(1.6,5){7}; ode[box] (a) at(0.8,5){5}; ode[box] (a) at(0,5){4}; end{tikzpicture}\ (i) end{minipage} \\\ 排序结果:\ egin{tikzpicture} ode[box] (b) at(9,5){25}; ode[box] (a) at(8,5){20}; ode[box] (a) at(7,5){17}; ode[box] (a) at(6,5){13}; ode[box] (a) at(5,5){8}; ode[box] (a) at(4,5){7}; ode[box] (a) at(3,5){5}; ode[box] (a) at(2,5){4}; ode[box] (a) at(1,5){2}; end{tikzpicture}\ end{document}