<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dutor &#187; AVL</title>
	<atom:link href="http://www.dutor.net/index.php/tag/avl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dutor.net</link>
	<description>熟读而精思，循序而渐进，厚积而薄发。</description>
	<lastBuildDate>Tue, 17 Jan 2012 14:44:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Dump the Holy Girl(s)</title>
		<link>http://www.dutor.net/index.php/2010/05/dump-the-holy-girls/</link>
		<comments>http://www.dutor.net/index.php/2010/05/dump-the-holy-girls/#comments</comments>
		<pubDate>Mon, 03 May 2010 22:51:20 +0000</pubDate>
		<dc:creator>dutor</dc:creator>
				<category><![CDATA[尚未分类]]></category>
		<category><![CDATA[AVL]]></category>
		<category><![CDATA[动态链接]]></category>
		<category><![CDATA[概率论]]></category>
		<category><![CDATA[虚拟内存]]></category>

		<guid isPermaLink="false">http://www.dutor.net/?p=2228</guid>
		<description><![CDATA[<p></p>
<pre lang="bash" line="1">
$ ssh FenglinHou@Dalian
~% cd Wdir/
~/Wdir% cat dump.cpp
#inlcude <everything>
int
main()
{
    std::set<std::Girl> ().clear();
    std::ifstream lifein("~/*");
    std::ofstream lifeout("~/*");
    std::copy(std::istream_iterator<std::stuff>(lifein), 
              std::istream_iterator<std::stuff>(), 
              std::ostream_iterator<std::stuff>(lifeout, "\newday"));
    return (0);
}
~/Wdir% cat Makefile
dump:dump.cpp
    g++ -odump dump.cpp
~/Wdir% make
~/Wdir% ./dump

</pre>]]></description>
			<content:encoded><![CDATA[</p>

<div class="wp_codebox"><table><tr id="p22281"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p2228code1"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> FenglinHou<span style="color: #000000; font-weight: bold;">@</span>Dalian
~<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">cd</span> Wdir<span style="color: #000000; font-weight: bold;">/</span>
~<span style="color: #000000; font-weight: bold;">/</span>Wdir<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> dump.cpp
<span style="color: #666666; font-style: italic;">#inlcude &lt;everything&gt;</span>
int
main<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    std::<span style="color: #000000; font-weight: bold;">set</span><span style="color: #000000; font-weight: bold;">&lt;</span>std::Girl<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.clear<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
    std::ifstream lifein<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;~/*&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
    std::ofstream lifeout<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;~/*&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
    std::copy<span style="color: #7a0874; font-weight: bold;">&#40;</span>std::istream_iterator<span style="color: #000000; font-weight: bold;">&lt;</span>std::stuff<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>lifein<span style="color: #7a0874; font-weight: bold;">&#41;</span>, 
              std::istream_iterator<span style="color: #000000; font-weight: bold;">&lt;</span>std::stuff<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>, 
              std::ostream_iterator<span style="color: #000000; font-weight: bold;">&lt;</span>std::stuff<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>lifeout, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>ewday&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
    <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
~<span style="color: #000000; font-weight: bold;">/</span>Wdir<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> Makefile
dump:dump.cpp
    <span style="color: #c20cb9; font-weight: bold;">g++</span> <span style="color: #660033;">-odump</span> dump.cpp
~<span style="color: #000000; font-weight: bold;">/</span>Wdir<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">make</span>
~<span style="color: #000000; font-weight: bold;">/</span>Wdir<span style="color: #000000; font-weight: bold;">%</span> .<span style="color: #000000; font-weight: bold;">/</span>dump</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dutor.net/index.php/2010/05/dump-the-holy-girls/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AVL平衡树实现</title>
		<link>http://www.dutor.net/index.php/2009/11/avl-tree-implementation/</link>
		<comments>http://www.dutor.net/index.php/2009/11/avl-tree-implementation/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 14:29:00 +0000</pubDate>
		<dc:creator>dutor</dc:creator>
				<category><![CDATA[之算法神奇]]></category>
		<category><![CDATA[边走编程]]></category>
		<category><![CDATA[AVL]]></category>
		<category><![CDATA[数据结构]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.dutor.net/?p=1663</guid>
		<description><![CDATA[　　这里只给出一个简单的实现，具体原理参见Google、Baidu的各个角落及各大教材。做两点说明：
仅仅实现了必要的左旋、右旋处理，左平衡、右平衡操作，以及数据的插入操作，
不可重复插入数据。
<pre lang="cpp" line="1">
typedef int Type; //~ 节点数据类型
typedef struct Node
{
	Type data;
	int bf;
	Node *left, *right;
	Node(){ bf = 0; left = right = 0; }
} AVLNode, *AVLTree; //~ 平衡树及其节点

void RRotate(AVLTree &#038; T) //~ 右旋处理,T的左孩子成为新子树根
{
	AVLNode * tmp = T->left;
	T->left = tmp->right;
	tmp->right = T;
	T = tmp;
}
</pre>]]></description>
			<content:encoded><![CDATA[<p>　　这里只给出一个简单的实现，具体原理参见Google、Baidu的各个角落及各大教材。做两点说明：<br />
仅仅实现了必要的左旋、右旋处理，左平衡、右平衡操作，以及数据的插入操作，<br />
不可重复插入数据。</p>

<div class="wp_codebox"><table><tr id="p16632"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
</pre></td><td class="code" id="p1663code2"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">int</span> Type<span style="color: #008080;">;</span> <span style="color: #666666;">//~ 节点数据类型</span>
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">struct</span> Node
<span style="color: #008000;">&#123;</span>
	Type data<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> bf<span style="color: #008080;">;</span>
	Node <span style="color: #000040;">*</span>left, <span style="color: #000040;">*</span>right<span style="color: #008080;">;</span>
	Node<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> left <span style="color: #000080;">=</span> right <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> AVLNode, <span style="color: #000040;">*</span>AVLTree<span style="color: #008080;">;</span> <span style="color: #666666;">//~ 平衡树及其节点</span>
&nbsp;
<span style="color: #0000ff;">void</span> RRotate<span style="color: #008000;">&#40;</span>AVLTree <span style="color: #000040;">&amp;</span> T<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 右旋处理,T的左孩子成为新子树根</span>
<span style="color: #008000;">&#123;</span>
	AVLNode <span style="color: #000040;">*</span> tmp <span style="color: #000080;">=</span> T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left<span style="color: #008080;">;</span>
	T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left <span style="color: #000080;">=</span> tmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>right<span style="color: #008080;">;</span>
	tmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>right <span style="color: #000080;">=</span> T<span style="color: #008080;">;</span>
	T <span style="color: #000080;">=</span> tmp<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> LLotate<span style="color: #008000;">&#40;</span>AVLTree <span style="color: #000040;">&amp;</span> T<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 左旋处理,T的右孩子成为新子树根</span>
<span style="color: #008000;">&#123;</span>
	AVLNode <span style="color: #000040;">*</span> tmp <span style="color: #000080;">=</span> T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>right<span style="color: #008080;">;</span>
	T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>right <span style="color: #000080;">=</span> tmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left<span style="color: #008080;">;</span>
	tmp<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left <span style="color: #000080;">=</span> T<span style="color: #008080;">;</span>
	T <span style="color: #000080;">=</span> tmp<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> LBalance<span style="color: #008000;">&#40;</span>AVLTree <span style="color: #000040;">&amp;</span> T<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 左平衡处理</span>
<span style="color: #008000;">&#123;</span>
	AVLNode <span style="color: #000040;">*</span> l <span style="color: #000080;">=</span> T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>l<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ LL,单右旋转</span>
			RRotate<span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> l<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #666666;">//~ 更新平衡因子</span>
			<span style="color: #0000ff;">return</span> <span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ LR双旋转</span>
			AVLNode <span style="color: #000040;">*</span> lr <span style="color: #000080;">=</span> l<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>right<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>lr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 更新平衡因子</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span>
					l<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
					T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
					<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">case</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span>
					T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
					l<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
					<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span><span style="color: #666666;">// switch</span>
			lr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
			LLotate<span style="color: #008000;">&#40;</span>l<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			RRotate<span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #666666;">// switch</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> RBalance<span style="color: #008000;">&#40;</span>AVLTree <span style="color: #000040;">&amp;</span> T<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 右平衡处理</span>
<span style="color: #008000;">&#123;</span>
	AVLNode <span style="color: #000040;">*</span> r <span style="color: #000080;">=</span> T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>right<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>r<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ RR,单左旋转</span>
			LLotate<span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> r<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">return</span> <span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ RL双旋转</span>
			AVLNode <span style="color: #000040;">*</span> rl <span style="color: #000080;">=</span> r<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>rl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf<span style="color: #008000;">&#41;</span><span style="color: #666666;">//~ 更新平衡因子</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span>
					r<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
					T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
					<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">case</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span>
					T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
					r<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
					<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span><span style="color: #666666;">// switch</span>
			rl<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
			RRotate<span style="color: #008000;">&#40;</span>r<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			LLotate<span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #666666;">// switch</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">bool</span> Insert<span style="color: #008000;">&#40;</span>AVLTree <span style="color: #000040;">&amp;</span> T, Type <span style="color: #000040;">&amp;</span> data<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 通过插入操作构建AVL树</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">bool</span> taller <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span> <span style="color: #666666;">//~ 标识此次插入操作是否使该树长高</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>T<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 该子树为空树，长高</span>
	<span style="color: #008000;">&#123;</span>
		T <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> AVLNode<span style="color: #008080;">;</span>
		T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data <span style="color: #000080;">=</span> data<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> taller<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// if</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>data <span style="color: #000080;">==</span> T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> <span style="color: #666666;">//~ 该数据已存在，不重复插入</span>
	<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>data <span style="color: #000080;">&lt;</span> T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>data<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 需要将该数据插入左子树</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>Insert<span style="color: #008000;">&#40;</span>T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left, data<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span> <span style="color: #666666;">//~ 左子树未长高</span>
		<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf<span style="color: #008000;">&#41;</span> <span style="color: #666666;">//~ 左子树长高，判断是否需要调整</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ 失衡，许进行左平衡调整</span>
				LBalance<span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ 未失衡，只需调整当前节点的平衡因子</span>
				T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">case</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ 未失衡，只需调整当前节点的平衡因子</span>
				T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// switch</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// if</span>
	<span style="color: #0000ff;">else</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>Insert<span style="color: #008000;">&#40;</span>T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>left, data<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ 未失衡，只需调整当前节点的平衡因子</span>
				T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ 未失衡，只需调整当前节点的平衡因子</span>
				T<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>bf <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">case</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #666666;">//~ 失衡，许进行右平衡调整</span>
				RBalance<span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// switch</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// if</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dutor.net/index.php/2009/11/avl-tree-implementation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AVL左转转右转转</title>
		<link>http://www.dutor.net/index.php/2009/08/avl_rotate/</link>
		<comments>http://www.dutor.net/index.php/2009/08/avl_rotate/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 11:23:42 +0000</pubDate>
		<dc:creator>dutor</dc:creator>
				<category><![CDATA[之算法神奇]]></category>
		<category><![CDATA[边走编程]]></category>
		<category><![CDATA[AVL]]></category>
		<category><![CDATA[数据结构]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.dutor.net/?p=1048</guid>
		<description><![CDATA[严老太，在您面前小孙孙我实在心智底下啊！一个AVL就把我整的团团转，单向右旋，单向左旋，双向旋转先左后右，双向旋转先右后左，很简单的道理，您就是不肯轻易说出来，在那转啊转的，貌似很神秘的样子！

在构造AVL树的时候，会出现插入某个节点后输不再平衡的情况，这时候只需要找到离插入点最近的、平衡因子不合法的节点，调整以此节点为根的子树(*)，使其平衡因子合法，其实就是使这棵子树(*)的高度与插入节点前相比不发生变化。具体的调整策略因具体情况而异，但原则都是一样的：首先应该明确根节点大于左孩子而小于右孩子，另外，调整后的子树(*)的根是一定会发生变化的，且变化后的子树应该满足平衡条件，即平衡因子为1，0，-1之一。
<img alt="rotate sample" src="http://www.dutor.net/files/images/avl_rotate.gif" title="avl_rotate" width="602" height="281" />]]></description>
			<content:encoded><![CDATA[<p>严老太，在您面前小孙孙我实在心智底下啊！一个AVL就把我整的团团转，单向右旋，单向左旋，双向旋转先左后右，双向旋转先右后左，很简单的道理，您就是不肯轻易说出来，在那转啊转的，貌似很神秘的样子！</p>
<p>在构造AVL树的时候，会出现插入某个节点后输不再平衡的情况，这时候只需要找到离插入点最近的、平衡因子不合法的节点，调整以此节点为根的子树(*)，使其平衡因子合法，其实就是使这棵子树(*)的高度与插入节点前相比不发生变化。具体的调整策略因具体情况而异，但原则都是一样的：首先应该明确根节点大于左孩子而小于右孩子，另外，调整后的子树(*)的根是一定会发生变化的，且变化后的子树应该满足平衡条件，即平衡因子为1，0，-1之一。</p>
<p>这里有一个图示，<br />
<div class="wp-caption aligncenter" style="width: 612px"><img alt="rotate sample" src="http://www.dutor.net/files/images/avl_rotate.gif" title="avl_rotate" width="602" height="281" /><p class="wp-caption-text">rotate sample</p></div></p>
<p>还有两个，</p>
<div class="wp-caption aligncenter" style="width: 581px"><img alt="/型" src="http://www.dutor.net/files/images/avl_rotate2.jpg" title="AVL" width="571" height="284" /><p class="wp-caption-text">&quot;/&quot;型</p></div>
<div class="wp-caption aligncenter" style="width: 634px"><img alt="型" src="http://www.dutor.net/files/images/avl_rotate3.jpg" title="avl" width="624" height="353" /><p class="wp-caption-text">&quot;&lt;&quot;型</p></div>
<p>这里还有两段示例代码，</p>

<div class="wp_codebox"><table><tr id="p10483"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p1048code3"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> R_Rotate <span style="color: #008000;">&#40;</span>BSTree <span style="color: #000040;">&amp;</span>p<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//~ 对以*p为根的二叉排序树作右旋转处理，处理之后p指向新的树根结点，即旋转</span>
    <span style="color: #666666;">//~ 处理之前的左子树的根结点</span>
    lc<span style="color: #000080;">=</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>lchild <span style="color: #008080;">;</span>            <span style="color: #666666;">//~ lc指向的*p的左子树根结点</span>
    p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>lchild<span style="color: #000080;">=</span>lc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>rchild <span style="color: #008080;">;</span>    <span style="color: #666666;">//~ lc的右子树挂接为*p的左子树</span>
    lc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>rchild<span style="color: #000080;">=</span>p <span style="color: #008080;">;</span> p<span style="color: #000080;">=</span>lc <span style="color: #008080;">;</span>    <span style="color: #666666;">//~ p指向新的根结点</span>
<span style="color: #008000;">&#125;</span><span style="color: #666666;">//~ R_Rotate</span>
&nbsp;
<span style="color: #0000ff;">void</span> L_Rotate <span style="color: #008000;">&#40;</span>BSTree <span style="color: #000040;">&amp;</span>p<span style="color: #008000;">&#41;</span>    
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//~ 对以*p为根的二叉排序树作左旋处理，处理之后p指向新的树根结点，即旋转</span>
    <span style="color: #666666;">//~ 处理之前的右子树的根结点</span>
    rc<span style="color: #000080;">=</span>p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>rchild <span style="color: #008080;">;</span>            <span style="color: #666666;">//~ rc指向的*p的右子树根结点</span>
    p<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>rchild<span style="color: #000080;">=</span>rc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>lchild <span style="color: #008080;">;</span>  <span style="color: #666666;">//~ rc左子树挂接为*p的右子树</span>
    rc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>lchild<span style="color: #000080;">=</span>p <span style="color: #008080;">;</span> p<span style="color: #000080;">=</span>rc <span style="color: #008080;">;</span>   <span style="color: #666666;">//~ p指向新的根结点</span>
<span style="color: #008000;">&#125;</span><span style="color: #666666;">//~ L_Rotate</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dutor.net/index.php/2009/08/avl_rotate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AVL的定义</title>
		<link>http://www.dutor.net/index.php/2009/08/definition-avl/</link>
		<comments>http://www.dutor.net/index.php/2009/08/definition-avl/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 08:07:51 +0000</pubDate>
		<dc:creator>dutor</dc:creator>
				<category><![CDATA[边走编程]]></category>
		<category><![CDATA[AVL]]></category>
		<category><![CDATA[数据结构]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://www.dutor.net/?p=1041</guid>
		<description><![CDATA[AVL是一种二叉搜索树(Binary Search Tree)，被称为平衡树(Balanced Binary Tree)，由Adelsonr Velskii 和Landis 提出并以他们的名字命名。

今天看严蔚敏老太太的数据结构，她老人家是这么描述AVL的：AVL，它或者是一棵空树，或者是具有以下性质的二叉树：它的左子树和右子树都是平衡二叉树，且左子树和右子树的深度之差(平衡因子：BF)的绝对值不超过1。看着这么个定义很别扭，总觉得对平衡因子的要求比较弱，也可能是我悟性较弱吧，在这句话上我很是下了一番功夫……如果这样来定义的话，可能会让我更容易理解一些：
<ol>
	<li>根的左子树和右子树的高度差的最大值为1，空树的高度为0。</li>
	<li>根的左子树和右子树都是AVL树。</li>
</ol>
]]></description>
			<content:encoded><![CDATA[<p>AVL是一种二叉搜索树(Binary Search Tree)，被称为平衡树(Balanced Binary Tree)，由Adelsonr Velskii 和Landis 提出并以他们的名字命名。</p>
<p>今天看严蔚敏老太太的数据结构，她老人家是这么描述AVL的：AVL，它或者是一棵空树，或者是具有以下性质的二叉树：它的左子树和右子树都是平衡二叉树，且左子树和右子树的深度之差(平衡因子：BF)的绝对值不超过1。看着这么个定义很别扭，总觉得对平衡因子的要求比较弱，也可能是我悟性较弱吧，在这句话上我很是下了一番功夫……如果这样来定义的话，可能会让我更容易理解一些：</p>
<ol>
<li>根的左子树和右子树的高度差的最大值为1，空树的高度为0。</li>
<li>根的左子树和右子树都是AVL树。</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.dutor.net/index.php/2009/08/definition-avl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

