-
Notifications
You must be signed in to change notification settings - Fork 3
/
avl_tree.py
1038 lines (946 loc) · 32.5 KB
/
avl_tree.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- Mode: Python -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import random
# Standard libraries.
import sys
# prototype and test code for a Python AVL tree module.
# These trees can be used (simultaneously) as either a list
# or a dictionary data type, which causes a bit of a problem,
# syntax-wise: Does tree[5] mean '5th element of <tree>' or
# "lookup key '5' in <tree>"?
class node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def __repr__(self):
return "%d" % self.key
# standard binary search tree.
class binary_tree:
def __init__(self):
self.tree = None
def insert(self, key):
if self.tree:
self._insert(self.tree, key)
else:
self.tree = node(key)
def _insert(self, tree, key):
if key < tree.key:
if tree.left:
self._insert(tree.left, key)
else:
tree.left = node(key)
else:
if tree.right:
self._insert(tree.right, key)
else:
tree.right = node(key)
# tree-printing code inspired by
# Usenet article <461f7c$2ss@phoenix.csc.calpoly.edu>,
# posted by DSTUBBS@PHOENIX.CSC.CALPOLY.EDU (DANIEL STUBBS)
# 1995/10/17
# located by the all-powerful http://www.dejanews.com/
def print_tree(self):
l = link_node(None, 0, 0)
self._print(self.tree, l)
def _print(self, node, link):
node_repr = repr(node)
if node.right:
l = link_node(link, 1, len(node_repr) + 5)
self._print(node.right, l)
link.print_connectors()
sys.stdout.write("+-[%s]" % (node_repr))
if node.left or node.right:
sys.stdout.write("-|\n")
else:
sys.stdout.write("\n")
if node.left:
l = link_node(link, -1, len(node_repr) + 5)
self._print(node.left, l)
def tree_from_list(list):
bt = binary_tree()
bt.tree = _tree_from_list(list, 0, len(list))
return bt
def _tree_from_list(list, low, high):
midway = ((high - low) / 2) + low
if midway == high == low:
return None
else:
top = node(0)
top.left = _tree_from_list(list, low, midway)
top.key = list[midway]
top.right = _tree_from_list(list, midway + 1, high)
return top
# used by the printing code to keep track of the node widths
# and branch directions.
class link_node:
def __init__(self, parent, direction, width):
self.parent = parent
self.direction = direction
self.width = width
def print_connectors(self):
self._print_helper(self)
def _print_helper(self, link):
if link.parent:
self._print_helper(link.parent)
if (link.parent and
(link.parent.direction != link.direction) and
(link.parent.parent)):
sys.stdout.write("|" + (" " * (link.width - 1)))
else:
sys.stdout.write(" " * link.width)
class avl_node(node):
def __init__(self, key, parent=None):
self.parent = parent
self.balance = 0
self.rank = 1
node.__init__(self, key)
def __repr__(self):
return "%c %s %d" % (
["\\", "-", "/"][self.balance + 1],
repr(self.key),
self.rank,
)
# import os
# # Colors are nice. If these escape sequences mess up
# # your display, just use the __repr__ below instead.
# # [the new print code broke this because the escape
# # codes contribute to the width, but shouldnt']
# if os.name == 'posix':
# def __repr__ (self):
# blue_on = '\033[34;48m'
# red_on = '\033[31;48m'
# reset = '\033[0m'
# return (red_on +
# [' \ ',' - ', ' / '][self.balance+1] +
# # '%03d'% (self.rank) +
# ' '+
# blue_on + '%03d'%self.key +
# reset)
# else:
# def __repr__ (self):
# return [' \ ',' - ', ' / '][self.balance+1] + '%03d'%self.key
# based directly on Knuth, Sorting and Searching, Section 6.2.3
# I prefer beautiful recursive algorithms, but in
# the interest of speed & efficiency I'll take advantage
# of code designed to run on a circa 1968 machine.
#
# The base algorithm is modified to include the use
# of the RANK field as described by Knuth, in order to
# use the tree as an ordered list data structure, where
# elements can be referenced by index.
#
# The key comparisons were also changed to permit duplicate
# keys. duplicates are inserted and deleted at the lowest
# possible position in the tree
#
# Note: I think that the <RANK> field could be used to simplify
# the rebalancing code
# Other variations:
# o use red/black trees - I've heard lots about them on Usenet, but
# can't get my hands on any of the quoted references.
# o use threads. threads use the wasted None pointers in all the
# leaves to assist traversal. look at libg++'s
# gen/AVLMap.{ccP,ccH} for an example.
# It looks pretty hairy, but then so does this code, now.
# o write preorder and postorder
# o make a getslice that returns a new tree rather than a list
# o make 'iterator' versions of the traversal primitives (rather than
# building a list or tree, apply a function to each node)
Unbalanced = "Unbalanced AVL Tree Error"
Invalid_Balance = "Invalid Balance attribute Error"
Invalid_Rank = "Invalid RANK attribute Error"
Invalid_Parent = "Invalid PARENT attribute Error"
class avl_tree(binary_tree):
def __init__(self):
# following Knuth, there's a special 'fake'
# node at the top. the real tree's root is
# in self.tree.right.
self.tree = avl_node(0)
self.height = 0
self.length = 0
self.ops = []
def insert(self, key, index=None):
if index is None:
self._insert(key)
else:
self._insert_by_index(key, index)
def _insert(self, key):
if not self.tree.right:
self.tree.right = avl_node(key, self.tree)
else:
t = self.tree
s = p = t.right
while 1:
if key <= p.key:
# move left
p.rank = p.rank + 1
q = p.left
if not q:
# insert
q = avl_node(key, p)
p.left = q
break
elif q.balance:
t = p
s = q
p = q
else: # (key > p.key)
# move right
q = p.right
if not q:
# insert
q = avl_node(key, p)
p.right = q
break
elif q.balance:
t = p
s = q
p = q
self.length = self.length + 1
# adjust balance factors
if key <= s.key:
r = p = s.left
else:
r = p = s.right
while p != q:
if key <= p.key:
p.balance = -1
p = p.left
else: # key >= p.key:
p.balance = 1
p = p.right
# balancing act
if key <= s.key:
a = -1
else:
a = +1
if not s.balance:
s.balance = a
self.height = self.height + 1
return
elif s.balance == -a:
s.balance = 0
return
elif s.balance == a:
if r.balance == a:
# single rotation
# -1 == left
# +1 == right
p = r
if a == -1:
s.left = r.right
if r.right:
r.right.parent = s
r.right = s
s.parent = r
s.rank = s.rank - r.rank
else:
s.right = r.left
if r.left:
r.left.parent = s
r.left = s
s.parent = r
r.rank = r.rank + s.rank
s.balance = 0
r.balance = 0
elif r.balance == -a:
# double rotation
# -1 == left
# +1 == right
if a == -1:
p = r.right
r.right = p.left
if p.left:
p.left.parent = r
p.left = r
r.parent = p
s.left = p.right
if p.right:
p.right.parent = s
p.right = s
s.parent = p
p.rank = p.rank + r.rank
s.rank = s.rank - p.rank
else:
p = r.left
r.left = p.right
if p.right:
p.right.parent = r
p.right = r
r.parent = p
s.right = p.left
if p.left:
p.left.parent = s
p.left = s
s.parent = p
r.rank = r.rank - p.rank
p.rank = p.rank + s.rank
if p.balance == a:
s.balance, r.balance = (-a, 0)
elif p.balance == -a:
s.balance, r.balance = (0, a)
else:
s.balance, r.balance = (0, 0)
p.balance = 0
# finishing touch
if s == t.right:
t.right = p
else:
t.left = p
p.parent = t
# this isn't really useful yet, because we're not
# not using the tree to implement arbitrarily ordered
# lists [the main reason I can think of wanting such a thing
# is to avoid sequential allocation of lists]
def _insert_by_index(self, key, index):
self.length = self.length + 1
if not self.tree:
self.tree = avl_node(0)
self.tree.right = avl_node(key, self.tree)
else:
t = self.tree
s = p = t.right
u = m = index
while 1:
if m <= p.rank:
# move left
p.rank = p.rank + 1
r = p.left
if not r:
# insert
q = avl_node(key, p)
p.left = q
break
elif r.balance:
t = p
s = r
u = m
p = r
else: # (key > p.key)
# move right
m = m - p.rank
r = p.right
if not q:
# insert
q = avl_node(key, p)
p.right = q
break
elif q.balance:
t = p
s = r
u = m
p = r
# adjust balance factors
m = u
if m < s.rank:
r = p = s.left
else:
r = p = s.right
while p != q:
if m < p.rank:
p.balance = -1
p = p.left
else:
p.balance = +1
m = m - p.rank
p = p.right
# balancing act
if u < s.rank:
a = -1
else:
a = 1
if not s.balance:
s.balance = a
self.height = self.height + 1
return
elif s.balance == -a:
s.balance = 0
return
elif s.balance == a:
if r.balance == a:
# single rotation
# -1 == left
# +1 == right
p = r
if a == -1:
s.left = r.right
if r.right:
r.right.parent = s
r.right = s
s.parent = r
s.rank = s.rank - r.rank
else:
s.right = r.left
if r.left:
r.left.parent = s
r.left = s
s.parent = r
r.rank = r.rank + s.rank
s.balance = 0
r.balance = 0
elif r.balance == -a:
# double rotation
# -1 == left
# +1 == right
if a == -1:
p = r.right
r.right = p.left
if p.left:
p.left.parent = r
p.left = r
r.parent = p
s.left = p.right
if p.right:
p.right.parent = s
p.right = s
s.parent = p
p.rank = p.rank + r.rank
s.rank = s.rank - p.rank
else:
p = r.left
r.left = p.right
if p.right:
p.right.parent = r
p.right = r
r.parent = p
s.right = p.left
if p.left:
p.left.parent = s
p.left = s
s.parent = p
r.rank = r.rank - p.rank
p.rank = p.rank + s.rank
if p.balance == a:
s.balance, r.balance = (-a, 0)
elif p.balance == -a:
s.balance, r.balance = (0, a)
else:
s.balance, r.balance = (0, 0)
p.balance = 0
# finishing touch
if s == t.right:
t.right = p
else:
t.left = p
p.parent = t
# I want to use these, but currently don't
def single_rotate_right(self, s, r):
s.right = r.left
if r.left:
r.left.parent = s
r.left = s
s.parent = r
r.rank = r.rank + s.rank
def single_rotate_left(self, s, r):
s.left = r.right
if r.right:
r.right.parent = s
r.right = s
s.parent = r
s.rank = s.rank - r.rank
# this is very much like the code for insert, 'cept I wrote it
# myself.
def remove(self, key):
x = self.tree.right
while 1:
if key < x.key:
# move left
# we will be deleting from the left, adjust
# this node's rank accordingly.
x.rank = x.rank - 1
if x.left:
x = x.left
else:
# Oops! now we have to undo the rank changes
# all the way up the tree
x.rank = x.rank + 1
while x != self.tree.right:
if x.parent.left == x:
x.parent.rank = x.parent.rank + 1
x = x.parent
raise KeyError("key not in tree")
elif key > x.key:
# move right
if x.right:
x = x.right
else:
x.rank = x.rank + 1
while x != self.tree.right:
if x.parent.left == x:
x.parent.rank = x.parent.rank + 1
x = x.parent
raise KeyError("key not in tree")
else:
break
shift = 0
# <x> is the node we wish to delete
if x.left and x.right:
# debug only
print("shifting...")
shift = 1
# the complicated case. reduce this
# to the simple case where we are deleting
# a node with only a single child.
# find the immediate predecessor <y>
y = x.left
while y.right:
y = y.right
# replace <x> with <y>
x.key = y.key
# we know <x>'s left subtree lost a node because
# that's where we took it from.
x.rank = x.rank - 1
x = y
# now <x> has at most one child
# now we scoot this child into the place of <x>
# is <x> a left (xp=1) or right (xp=0) child of its parent?
xp = x == x.parent.left
if x.left:
x_child = x.left
x_child.parent = x.parent
elif x.right:
x_child = x.right
x_child.parent = x.parent
else:
x_child = None
if xp:
x.parent.left = x_child
shortened_side = -1
else:
x.parent.right = x_child
shortened_side = +1
# debug only
if shift:
self.print_tree()
print("-" * 50)
# the height of the subtree <x>
# has now been shortened. climb back up
# the tree, rotating to adjust for the change
shorter = 1
p = x.parent
# p = x
while shorter and p.parent:
print(p)
# raw_input()
# case 1: height unchanged
if p.balance == 0:
print("case 1")
if shortened_side == -1:
# we removed a left child, the tree is
# now heavier on the right
p.balance = +1
else:
# we removed a right child, the tree is
# now heavier on the left.
p.balance = -1
shorter = 0
# case 2: taller subtree shortened, height reduced
elif p.balance == shortened_side:
print("case 2")
p.balance = 0
# case 3: shorter subtree shortened
else:
top = p.parent
# set <q> to the taller of the two subtrees of <p>
if shortened_side == 1:
q = p.left
else:
q = p.right
if q.balance == 0:
print("case 3a")
# case 3a: height unchanged
if shortened_side == -1:
# single rotate left
q.parent = p.parent
p.right = q.left
if q.left:
q.left.parent = p
q.left = p
p.parent = q
q.rank = q.rank + p.rank
else:
# single rotate right
q.parent = p.parent
p.left = q.right
if q.right:
q.right.parent = p
q.right = p
p.parent = q
p.rank = p.rank - q.rank
shorter = 0
q.balance = shortened_side
p.balance = -shortened_side
elif q.balance == p.balance:
print("case 3b")
# case 3b: height reduced
if shortened_side == -1:
# single rotate left
q.parent = p.parent
p.right = q.left
if q.left:
q.left.parent = p
q.left = p
p.parent = q
q.rank = q.rank + p.rank
else:
# single rotate right
q.parent = p.parent
p.left = q.right
if q.right:
q.right.parent = p
q.right = p
p.parent = q
p.rank = p.rank - q.rank
shorter = 1
q.balance = 0
p.balance = 0
else:
print("case 3c")
# case 3c: height reduced, balance factors
# opposite.
if shortened_side == 1:
# double rotate right
# first, a left rotation around q
r = q.right
r.parent = p.parent
q.right = r.left
if r.left:
r.left.parent = q
r.left = q
q.parent = r
# now, a right rotation around p
p.left = r.right
if r.right:
r.right.parent = p
r.right = p
p.parent = r
r.rank = r.rank + q.rank
p.rank = p.rank - r.rank
else:
# double rotate left
# first, a right rotation around q
r = q.left
r.parent = p.parent
q.left = r.right
if r.right:
r.right.parent = q
r.right = q
q.parent = r
# now, a left rotation around p
p.right = r.left
if r.left:
r.left.parent = p
r.left = p
p.parent = r
q.rank = q.rank - r.rank
r.rank = r.rank + p.rank
# (shortened_side == 1) <=> -a
# this is magic
if r.balance == shortened_side:
q.balance, p.balance = (-shortened_side, 0)
elif r.balance == (-shortened_side):
q.balance, p.balance = (0, shortened_side)
else:
q.balance, p.balance = (0, 0)
r.balance = 0
q = r
# a rotation has caused <q> (or <r> in case 3c) to become
# the root. let <p>'s former parent know this.
if top.left == p:
top.left = q
else:
top.right = q
# end case 3
p = q
self.print_tree()
x = p
p = x.parent
# shortened_side tells us which side we
# came up from
if x == p.left:
shortened_side = -1
else:
shortened_side = +1
# end while (shorter)
# when we're all done, we're one shorter.
self.length = self.length - 1
def __len__(self):
return self.length
def __getitem__(self, index):
m = index + 1
p = self.tree.right
while 1:
if not p:
raise IndexError("index out of range")
if m < p.rank:
p = p.left
elif m > p.rank:
m = m - p.rank
p = p.right
else:
return p.key
def count_prev_ops(self):
# find the rightmost (last) node
node = self.tree.right
while node.right:
node = node.right
for i in xrange(self.length):
node = self._get_prev(node)
return self.ops
# analysis:
# I built random trees of 1000, 10000, and 100000
# nodes. I then iterated get_prev() over the whole
# tree, giving me a list of the number of ops for
# each call to get_prev(). In every case
# after sorting I get a sequence where 1/2 of the calls
# required only 1 operation, 1/4 required 2 ops, 1/8
# required 3, etc..
# assuming that this is an accurate characterization,
# the total number of operations to scan the
# whole tree is
# (1 * (1/2 * n) +
# (2 * (1/4 * n)) +
# (3 * (1/8 * n)) +
# (4 * (1/16 * n)) +
# ...
# k n / (2^k)
#
# this sum converges on 2 as n->infinity, but I don't have
# any symbolic math programs to verify it (and it's been
# 8 years since I did this type of calculus 8^)
#
# statistically, the sum seems to hover near 2*n,
# which makes the average number of operations to find
# the predecessor/successor of a node ~2. I'd love to
# prove this.
# used by getslice only, that's why there's no fall-off check
def _get_prev(self, node):
# immediate predecessor is either
# a) one left, down until no right child
# b) up until previous node was a right child.
ops = 0
if node.left:
node = node.left
ops = ops + 1
while node.right:
node = node.right
ops = ops + 1
self.ops.append(ops)
return node
else:
child = node
while node.parent:
node = node.parent
ops = ops + 1
if child == node.right:
self.ops.append(ops)
return node
child = node
self.ops.append(ops)
return node
def _get_succ(self, node):
# immediate predecessor is either
# a) one left, down until no right child
# b) up until previous node was a right child.
ops = 0
if node.right:
node = node.right
ops = ops + 1
while node.left:
node = node.left
ops = ops + 1
self.ops.append(ops)
return node
else:
child = node
while node.parent:
node = node.parent
ops = ops + 1
if child == node.left:
self.ops.append(ops)
return node
child = node
self.ops.append(ops)
return node
# Return a slice of the tree as a list [rather than as a new tree]
def __getslice__(self, i=None, j=None):
# handle default or negative indices
if i is None:
i = 0
elif i < 0:
i = i + self.length
if j is None:
j = self.length
elif j < 0:
j = j + self.length
if (j >= self.length) or (i < 0):
raise IndexError("index out of range")
# result list template
num_left = j - i
result = range(num_left)
# find the <j>th node.
m = j + 1
node = self.tree.right
while 1:
if not node:
raise IndexError("index out of range")
if m < node.rank:
node = node.left
elif m > node.rank:
m = m - node.rank
node = node.right
else:
break
# fill in the result list by repeatedly
# calling self._get_prev()
while num_left:
# print(node)
num_left = num_left - 1
node = self._get_prev(node)
result[num_left] = node.key
return result
def __repr__(self):
return repr(self.inorder())
def inorder(self):
if not self.tree.right:
return []
return self._inorder(self.tree.right)
def _inorder(self, node):
result = []
if node.left:
result = result + self._inorder(node.left)
result.append(node.key)
if node.right:
result = result + self._inorder(node.right)
return result
def verify(self):
self._verify_balance(self.tree.right)
self._verify_rank(self.tree.right)
self._verify_parent(self.tree.right, self.tree)
# this verifies balance 'manually', and also
# double-checks each node's <balance> member.
def _verify_balance(self, node):
if node is None:
return 0
lh = self._verify_balance(node.left)
rh = self._verify_balance(node.right)
if (rh - lh) != node.balance:
raise Invalid_Balance("at node <%s>" % (repr(node)))
if abs(lh - rh) > 1:
raise Unbalanced("at node <%s>" % (repr(node)))
else:
return 1 + max(lh, rh)
def _verify_rank(self, node):
if not node:
return 0
num_left = num_right = 0
if node.left:
num_left = self._verify_rank(node.left)
if node.right:
num_right = self._verify_rank(node.right)
if node.rank != num_left + 1:
raise Invalid_Rank("at node <%s>" % (repr(node)))
return num_left + num_right + 1
def _verify_parent(self, node, parent):
if node.parent != parent:
raise Invalid_Parent("at node <%s>" % (repr(node)))
if node.left:
self._verify_parent(node.left, node)
if node.right:
self._verify_parent(node.right, node)
demo_nums = [50, 45, 15, 10, 75, 55, 70, 80, 60, 32, 20, 40, 25, 22, 31, 30]
def avl_demo():
tree = avl_tree()
for num in demo_nums:
tree.insert(num)
print("-" * 50)
tree.print_tree()
print("<cr>", end="")
raw_input()
return tree
def btest(n=50):
print("generating random numbers...")
nums = map(lambda x, n=n: int(random.uniform(0, 999)), range(n))
print(nums)
t = binary_tree()
print("inserting them into the tree...")
for i in range(len(nums)):
num = nums[i]
t.insert(num)
t.print_tree()
def del_test(iter=100, l=20):
for i in range(iter):
at, nums = test(l)
dnums = []
while at:
n = random.choice(at)
print(n)
dnums.append(n)
at.remove(n)
at.verify()
print("-" * 50)
at.print_tree()
print("*" * 50)
print("*" * 50)
def slice_test(tree_size=100, times=100):
at = test(tree_size)
for x in range(times):
print(x)
i = int(random.uniform(0, tree_size - 1))
j = int(random.uniform(0, tree_size - 1))
if j < i:
i, j = j, i
sl = at[i:j]
return at.ops
# The rest of the file was used while working on the
# tree_from_tree slice algorithm
class tree_iterator:
def __init__(self, tree, node=None):
self.tree = tree
# default to the leftmost node
if node is None:
node = tree.tree
while node.left:
node = node.left
self.node = node
def next(self):
result = self.node
self.node = self.tree._get_succ(self.node)
return result
# strong typing is for weak minds