YAP
7.1.0
Toggle main menu visibility
Main Page
Related Pages
Modules
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
a
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
y
~
Functions
a
c
d
e
f
g
h
i
l
m
n
o
p
q
r
s
t
u
v
y
~
Variables
a
c
e
f
g
i
k
m
n
o
p
q
r
s
t
v
Files
File List
File Members
All
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
r
s
t
u
v
w
y
Functions
c
e
m
y
Variables
Typedefs
Enumerations
Enumerator
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
r
s
t
u
v
w
y
Macros
•
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Modules
Pages
avl.yap
Go to the documentation of this file.
1
/*************************************************************************
2
* *
3
* YAP Prolog *
4
* *
5
* Yap Prolog was developed at NCCUP - Universidade do Porto *
6
* *
7
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8
* *
9
**************************************************************************
10
* *
11
* File: regexp.yap *
12
* Last rev: 5/15/2000 *
13
* mods: *
14
* comments: AVL trees in YAP (from code by M. van Emden, P. Vasey) *
15
* *
16
*************************************************************************/
17
18
/**
19
* @file avl.yap
20
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
21
* @date Tue Nov 17 00:59:28 2015
22
*
23
* @brief Support for constructing AVL trees
24
*
25
*
26
*/
27
28
29
30
/**
31
* @defgroup avl AVL Trees
32
* @ingroup YAPLibrary
33
34
@brief Supports constructing AVL trees.
35
36
@long available through the directive:
37
38
```
39
:- use_module(library(avl)).
40
```
41
42
It includes the following predicates:
43
44
- avl_insert/4
45
- avl_lookup/3
46
- avl_new/1
47
48
AVL trees are balanced search binary trees. They are named after their
49
inventors, Adelson-Velskii and Landis, and they were the first
50
dynamically balanced trees to be proposed. The YAP AVL tree manipulation
51
predicates library uses code originally written by Martin van Emdem and
52
published in the Logic Programming Newsletter, Autumn 1981. A bug in
53
this code was fixed by Philip Vasey, in the Logic Programming
54
Newsletter, Summer 1982. The library currently only includes routines to
55
insert and lookup elements in the tree. Please try red-black trees if
56
you need deletion.
57
58
@{
59
60
*/
61
62
:- module(
avl
, [
63
avl_new/1
,
64
avl_insert/4
,
65
avl_lookup/3
66
]).
67
68
69
/** @pred avl_new(+ _T_)
70
71
72
Create a new tree.
73
74
75
*/
76
avl_new
([]).
77
78
/** @pred avl_insert(+ _Key_,? _Value_,+ _T0_,- _TF_)
79
80
81
Add an element with key _Key_ and _Value_ to the AVL tree
82
_T0_ creating a new AVL tree _TF_. Duplicated elements are
83
allowed.
84
85
86
*/
87
avl_insert
(
Key
,
Value
,
T0
,
TF
)
:-
88
insert(
T0
,
Key
,
Value
,
TF
,
_
).
89
90
insert([],
Key
,
Value
, avl([],
Key
,
Value
,-,[]), yes).
91
insert(avl(
L
,
Root
,
RVal
,
Bl
,
R
),
E
,
Value
,
NewTree
,
WhatHasChanged
)
:-
92
E
@<
Root
,
insert,
93
insert(
L
,
E
,
Value
,
NewL
,
LeftHasChanged
),
94
adjust(avl(
NewL
,
Root
,
RVal
,
Bl
,
R
),
LeftHasChanged
, left,
NewTree
,
WhatHasChanged
).
95
insert(avl(
L
,
Root
,
RVal
,
Bl
,
R
),
E
,
Val
,
NewTree
,
WhatHasChanged
)
:-
96
% E @>= Root, currently we allow duplicated values, although
97
% lookup will only fetch the first.
98
insert(
R
,
E
,
Val
,
NewR
,
RightHasChanged
),
99
adjust(avl(
L
,
Root
,
RVal
,
Bl
,
NewR
),
RightHasChanged
, right,
NewTree
,
WhatHasChanged
).
100
101
adjust(
Oldtree
, no,
_
,
Oldtree
, no).
102
adjust(avl(
L
,
Root
,
RVal
,
Bl
,
R
), yes,
Lor
,
NewTree
,
WhatHasChanged
)
:-
103
table(
Bl
,
Lor
,
Bl1
,
WhatHasChanged
,
ToBeRebalanced
),
104
rebalance(avl(
L
,
Root
,
RVal
,
Bl
,
R
),
Bl1
,
ToBeRebalanced
,
NewTree
).
105
106
% balance where balance whole tree to be
107
% before inserted after increased rebalanced
108
table
(
-
, left ,
<
, yes , no ).
109
table
(
-
, right ,
>
, yes , no ).
110
table
(
<
, left ,
-
, no , yes ).
111
table
(
<
, right ,
-
, no , no ).
112
table
(
>
, left ,
-
, no , no ).
113
table
(
>
, right ,
-
, no , yes ).
114
115
rebalance(avl(
Lst
,
Root
,
RVal
,
_Bl
,
Rst
),
Bl1
, no, avl(
Lst
,
Root
,
RVal
,
Bl1
,
Rst
)).
116
rebalance(
OldTree
,
_
, yes,
NewTree
)
:-
117
avl_geq(
OldTree
,
NewTree
).
118
119
avl_geq(avl(
Alpha
,
A
,
VA
,>,avl(
Beta
,
B
,
VB
,>,
Gamma
)),
120
avl(avl(
Alpha
,
A
,
VA
,-,
Beta
),
B
,
VB
,-,
Gamma
)).
121
avl_geq(avl(avl(
Alpha
,
A
,
VA
,<,
Beta
),
B
,
VB
,<,
Gamma
),
122
avl(
Alpha
,
A
,
VA
,-,avl(
Beta
,
B
,
VB
,-,
Gamma
))).
123
avl_geq(avl(
Alpha
,
A
,
VA
,>,avl(avl(
Beta
,
X
,
VX
,
Bl1
,
Gamma
),
B
,
VB
,<,
Delta
)),
124
avl(avl(
Alpha
,
A
,
VA
,
Bl2
,
Beta
),
X
,
VX
,-,avl(
Gamma
,
B
,
VB
,
Bl3
,
Delta
)))
:-
125
table2(
Bl1
,
Bl2
,
Bl3
).
126
avl_geq(avl(avl(
Alpha
,
A
,
VA
,>,avl(
Beta
,
X
,
VX
,
Bl1
,
Gamma
)),
B
,
VB
,<,
Delta
),
127
avl(avl(
Alpha
,
A
,
VA
,
Bl2
,
Beta
),
X
,
VX
,-,avl(
Gamma
,
B
,
VB
,
Bl3
,
Delta
)))
:-
128
table2(
Bl1
,
Bl2
,
Bl3
).
129
130
table
2
(
<
,- ,> ).
131
table
2
(
>
,< ,- ).
132
table
2
(
-
,- ,- ).
133
134
/** @pred avl_lookup(+ _Key_,- _Value_,+ _T_)
135
136
137
Lookup an element with key _Key_ in the AVL tree
138
_T_, returning the value _Value_.
139
140
*/
141
142
avl_lookup
(
Key
,
Value
, avl(
L
,
Key0
,
KVal
,
_
,
R
))
:-
143
compare(
Cmp
,
Key
,
Key0
),
144
avl_lookup(
Cmp
,
Value
,
L
,
R
,
Key
,
KVal
).
145
146
avl_lookup(=,
Value
,
_
,
_
,
_
,
Value
).
147
avl_lookup(<,
Value
,
L
,
_
,
Key
,
_
)
:-
148
avl_lookup
(
Key
,
Value
,
L
).
149
avl_lookup(>,
Value
,
_
,
R
,
Key
,
_
)
:-
150
avl_lookup
(
Key
,
Value
,
R
).
151
152
153
/**
154
@}
155
*/
156
avl_insert/4
avl_insert(+ Key,? Value,+ T0,- TF)
avl_lookup/3
avl_lookup(+ Key,- Value,+ T)
avl_new/1
avl_new(+ T)
library
avl.yap
Generated by
1.9.3