Repository navigation
Expand file tree
/
Copy pathdata_tree_binary_search_node.py
More file actions
1743 lines (1256 loc) · 54.9 KB
/
Copy pathdata_tree_binary_search_node.py
File metadata and controls
1743 lines (1256 loc) · 54.9 KB
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
"""
-----EXPERIMENTAL-----
-----EXPERIMENTAL-----
-----EXPERIMENTAL-----
-----EXPERIMENTAL-----
-----EXPERIMENTAL-----
This module holds the library for a binary search tree.
"""
#
# Libraries - native
#
import os
import sys
#
# Libraries - native - collections
#
from collections import deque
#
# Libraries - custom
#
try :
from .base_data_node import Base_data_node
except :
sys.path.append( os.path.dirname( __file__ ) )
from base_data_node import Base_data_node
#
# Class
#
class Data_tree_binary_search_node(Base_data_node):
"""
Features needed:
-Hook for event where node's _key_unique changes
-More tree-focused functionality
"""
#
# Public
#
#
# Public - append
#
def append_node( self, arg_bool_is_left_side, arg_node_next, arg_node_root = None ):
"""
This method only replaces the immediate child nodes.
For 'insert' functionality, refer to insert_node()
Tests and status:
-Basic add - passed
-Exception handling for replacing nodes - functionality needs added
"""
_node_root = arg_node_root if arg_node_root else self
if arg_bool_is_left_side :
_node_root._node_next_left = arg_node_next
else :
_node_root._node_next_right = arg_node_next
#
# Public - balance
#
def balance_tree( self, arg_node_root = None ):
"""
This method automatically re-balances the search tree for efficiency. Since rebalancing typically leads
to a new root node, this method returns that result.
Notes:
- This method calls self._balance_tree_via_list()
I feel like there should be a way to do it without storing all the nodes in a list first. If
I find it, then there will probably be pros and cons to each approach, which is why this method
is setup to make calls to other methods.
Tests and status:
-All nodes accounted for - initial tests passed, but I'm not convinced yet
-No infinite loop - passed
"""
_node_root = arg_node_root if arg_node_root else self
return self._balance_tree_via_list( self.get_list_of_nodes_traversal_level_order( _node_root ) )
def _balance_tree_via_list( self, arg_list_of_nodes_from_tree ):
"""
This method handles one possible approach for re-balancing a data tree for stack recursion.
General approach:
-At each layer the algorithm splits the list of possible nodes by calculating the index of the middle node
-The node at the middle index gets added to the tree
-The two halves of the list form the "left / lesser" and "right / higher" values respectively
-This process continues until there are no nodes remaining in any of the broken up lists
Tests and status:
-No infinite loop - passed
-No repeated nodes - passed
-Resulting tree properly reflects the order of arg_list_of_nodes_from_tree - passed
"""
#
# Reminder: This lambda is here as a debugging asset for getting a list of unique keys rather than node memory signatures
# when printing to the terminal.
#
# _lambda_get_list_of_keys = lambda arg_list_of_nodes : [ item_node._key_unique for item_node in arg_list_of_nodes ]
#
# Sort list of nodes according to their keys ahead of parsing it into a tree
#
_list_of_nodes_from_tree = sorted( arg_list_of_nodes_from_tree,
key = lambda arg_node : arg_node._key_unique, )
#
# Reminders:
# -The double division sign rounds down to the nearest whole number
# -This is slightly faster than using int() to round down
#
item_index_middle = len( _list_of_nodes_from_tree ) // 2
item_list_of_nodes_left = _list_of_nodes_from_tree[ : item_index_middle ]
item_list_of_nodes_right = _list_of_nodes_from_tree[ item_index_middle + 1 : ]
_node_root_new = _list_of_nodes_from_tree[ item_index_middle ]
item_node = _node_root_new
#
# Iterate over sorted list of nodes
#
# Each triplet has the following:
# -item_node at index 0
# -item_list_of_nodes_left at index 1
# -item_list_of_nodes_right at index 2
#
_deque_of_triplets_node_node_parent_list_left_and_list_right_to_process = deque( [ [ item_node,
item_list_of_nodes_left,
item_list_of_nodes_right, ] ] )
while _deque_of_triplets_node_node_parent_list_left_and_list_right_to_process :
item_triplet = _deque_of_triplets_node_node_parent_list_left_and_list_right_to_process.pop()
#
# Unpack the triplet
#
item_node, item_list_of_nodes_left, item_list_of_nodes_right = item_triplet
#
# Clear item_node's children to avoid bad links
#
item_node._node_next_left = None
item_node._node_next_right = None
#
# Add a new child node to item_node's left side
# Build a triplet consisting of:
# -The child node
# -A list of the nodes to the left of it
# -A list of the nodes to the right of it
#
# The triplet can also be empty
#
item_triplet_left = self._get_triplet_node_middle_node_list_left_and_node_list( arg_node = item_node,
arg_list_of_nodes = item_list_of_nodes_left,
arg_bool_is_left = True, )
#
# Prep the next iteration only if the triplet holds any values
#
if item_triplet_left :
_deque_of_triplets_node_node_parent_list_left_and_list_right_to_process.append( item_triplet_left )
#
# Add a new child node to item_node's right side
# Build a triplet consisting of:
# -The child node
# -A list of the nodes to the left of it
# -A list of the nodes to the right of it
#
item_triplet_right = self._get_triplet_node_middle_node_list_left_and_node_list( arg_node = item_node,
arg_list_of_nodes = item_list_of_nodes_right,
arg_bool_is_left = False, )
#
# Prep the next iteration only if the triplet holds any values
#
if item_triplet_right :
_deque_of_triplets_node_node_parent_list_left_and_list_right_to_process.append( item_triplet_right )
#
# Return the new root node after the re-balance
#
return _node_root_new
def _get_triplet_node_middle_node_list_left_and_node_list( self, arg_node, arg_list_of_nodes, arg_bool_is_left ):
"""
This method gets the middle node in arg_list_of_nodes by finding its middle index. This method attaches the
new child node to arg_node.
It is a support method for _balance_tree_via_list() and handles both left and right sides.
The algorithm returns a triplet if relevant. Consisting of the following:
-The new child node attached to arg_node
-A sub list of nodes to the left of the new child node
-A sub list of nodes to the right of the new child node
Alternate return value:
If there are no more nodes to process, this method returns an empty list.
Tests and results:
-Splits arg_list_of_nodes successfully, regardless of size - passed
"""
#
# If arg_list_of_nodes has values, process it, and return the results as
# a triplet.
#
if arg_list_of_nodes :
_index_mid = len( arg_list_of_nodes ) // 2
_node_next = arg_list_of_nodes[ _index_mid ]
if arg_bool_is_left :
arg_node._node_next_left = _node_next
else :
arg_node._node_next_right = _node_next
return [ _node_next,
arg_list_of_nodes[ : _index_mid ],
arg_list_of_nodes[ _index_mid + 1 : ], ]
#
# If arg_list_of_nodes, return an empty triplet.
#
else :
return []
#
# Public - get
#
#
# Public - get - int
#
def get_int_count_for_nodes( self ):
"""
This method counts the number of nodes in the tree and returns the value.
"""
_int_count = 0
#
# Iterate over tree
#
_deque_to_process_nodes = deque( [ self ] )
while _deque_to_process_nodes :
item_node = _deque_to_process_nodes.pop()
#
# Increment count
#
_int_count += 1
#
# Prep next iteration
#
_deque_to_process_nodes.extend( item_node._get_list_of_nodes_next_adjacent() )
return _int_count
def get_int_height( self ):
"""
Returned the longest possible distance between the root node and any leaf node.
Tests and results:
-No infinite loop - passed
-Returns an accurate count - passed
"""
#
# This tracks the number of layers where at least one node exists
#
_count_height = 0
_deque_to_process_nodes = deque( [ self ] )
while True :
#
# If the count is zero, return the height
#
if not _deque_to_process_nodes :
return _count_height
#
# Increment height count per layer
#
_count_height += 1
#
# Set item_count_for_nodes equal to length of deque
# This tracks the number of nodes at the current layer
#
item_count_for_nodes = len( _deque_to_process_nodes )
#
# While the count for nodes at current layer is greater than zero...
# process the node and decrement the count
#
while item_count_for_nodes > 0 :
#
# Left pop ensures the node is from the current layer
#
item_node = _deque_to_process_nodes.popleft()
#
# Prep for next iteration
# When we hit zero, we are out of nodes at this layer
#
item_count_for_nodes -= 1
#
# Append the nodes for the next layer
#
_deque_to_process_nodes.extend( item_node._get_list_of_nodes_next_adjacent() )
#
# Public - get - list
#
def get_list_of_keys( self, **kwargs ):
"""
Returns a list of all _key_unique attribute values for all nodes in tree.
Tests and status:
-All keys accounted for - passed
-No infinite loop - passed
"""
#
# Prep return value
#
_list_to_return = []
#
# Iterate over tree
#
item_node = kwargs.get( "arg_node_root", self, )
_deque_to_process = deque( [ item_node ] )
while _deque_to_process :
item_node = _deque_to_process.popleft()
#
# Append key to list
#
_list_to_return.append( item_node._key_unique )
#
# Prep next iteration
#
if item_node._node_next_left :
_deque_to_process.append( item_node._node_next_left )
if item_node._node_next_right :
_deque_to_process.append( item_node._node_next_right )
#
# Return list
#
return _list_to_return
def get_list_of_nodes_traversal_level_order( self, arg_node_root = None ):
"""
Returns a list of nodes in order.
Order:
-Top down, layer-by-layer
-Each layer is covered completely, left to right
Strategic considerations:
-Starts with the root node
-Adds each subsequent layer, left to right
Visual example:
0
/ \
1 2
/ \ \
3 4 5
Order: 0 1 2 3 4 5
Tests and status:
-List in correct order - passed
-No infinite loop - passed
"""
_list_to_return = []
_node_root = arg_node_root if arg_node_root else self
_deque_to_process_nodes = deque( [ _node_root ] )
while _deque_to_process_nodes :
item_node = _deque_to_process_nodes.popleft()
_list_to_return.append( item_node )
_deque_to_process_nodes.extend( item_node._get_list_of_nodes_next_adjacent() )
return _list_to_return
def get_list_of_nodes_traversal_level_order_reversed( self, arg_node_root = None ):
"""
Returns a list of nodes in the tree, going right, then up.
Order:
-Goes bottom-up, right to left.
0
/ \
1 2
/ \ \
3 4 5
Order: 5 4 3 2 1 0
Tests and status:
-List in correct order - passed
-No infinite loop - passed
"""
_list_to_return = []
_node_start = self if arg_node_root == None else arg_node_root
item_list_layer_of_nodes = [ _node_start ]
_deque_to_process = deque( [ [ item_list_layer_of_nodes,
-1, # item_index_current
] ] )
while _deque_to_process :
#
# Get the current layer and current index
#
item_list_layer_of_nodes, item_index_current = _deque_to_process.pop()
#
# If True, re-add deque. Then add the node's children as another deque.
#
# Note about indexes: we can't actually disassemble the layer or else its nodes
# have to be included too early. Instead, we go backwards, and let the garbage collector
# dump the list from memory once we're done.
#
if abs( item_index_current ) <= len( item_list_layer_of_nodes ) :
_deque_to_process.append( [ item_list_layer_of_nodes,
#
# Mark this as False, so we can disassemble the dequen on the subsequent pass.
#
item_index_current - 1, # item_bool_check_nodes_next
] )
item_node = item_list_layer_of_nodes[ item_index_current ]
#
# If item_node is not a leaf node, add its layer
#
if item_node.logic_nodes_next_exist() :
_deque_to_process.append( [ item_node._get_list_of_nodes_next_adjacent(),
-1, # item_bool_check_nodes_next
] )
else :
_list_to_return.extend( reversed( item_list_layer_of_nodes ) )
return _list_to_return
def get_list_of_nodes_traversal_in_order( self, arg_node_root = None ) :
"""
This method returns a list of nodes.
The order generally follows these rules:
-Starts with left-most leaf
-left child -> parent -> right child
Strategic considerations:
-Lower sub-trees tend to appear towards the list's beginning
-Root node is likely to be somewhere in the middle
-Right half sub-trees appear to the end of the list
-Last node is going to be right-most leaf
Visual example ( source: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ ):
1
/ \
2 3
/ \ \
4 5 6
Order: 4 2 5 1 3 6
Tests and status:
-List in correct order - passed
-No infinite loop - passed
"""
_list_to_return = [ ]
_node_root = arg_node_root if arg_node_root else self
_deque_to_process_nodes = deque( [ [ _node_root,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] ] )
while _deque_to_process_nodes :
item_node, item_bool_need_to_traverse_left, item_bool_need_to_traverse_right, item_bool_need_to_append_output = _deque_to_process_nodes.pop()
if item_bool_need_to_traverse_left :
if item_node._node_next_left :
item_bool_need_to_traverse_left = False
#
# Prep next iteration
#
_deque_to_process_nodes.append( [ item_node,
item_bool_need_to_traverse_left,
item_bool_need_to_traverse_right,
item_bool_need_to_append_output,
] )
_deque_to_process_nodes.append( [ item_node._node_next_left,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] )
# Skip past storing the node and the right check for now
continue
if item_bool_need_to_append_output :
item_bool_need_to_append_output = False
_list_to_return.append( item_node )
if item_bool_need_to_traverse_right :
if item_node._node_next_right :
item_bool_need_to_traverse_right = False
#
# Prep next iteration
#
_deque_to_process_nodes.append( [ item_node,
item_bool_need_to_traverse_left,
item_bool_need_to_traverse_right,
item_bool_need_to_append_output,
] )
_deque_to_process_nodes.append( [ item_node._node_next_right,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] )
# Reminder: When we get here, we're done with this node and its children
return _list_to_return
def get_list_of_nodes_traversal_post_order( self, arg_node_root = None ):
"""
Returns a list of nodes.
Order:
-Starts with left-most leaf node
-Left child -> right child -> parent node
Strategic considerations:
-Favors children towards the beginning, and parents comes up after them
-The last item will be the root node
Visual example ( source: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ ):
1
/ \
2 3
/ \ \
4 5 6
Order: 4 5 2 6 3 1
Tests and status:
-List in correct order - passed
-No infinite loop - passed
"""
_list_to_return = [ ]
_node_root = arg_node_root if arg_node_root else self
_deque_to_process_nodes = deque( [ [ _node_root,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] ] )
while _deque_to_process_nodes :
item_node, item_bool_need_to_traverse_left, item_bool_need_to_traverse_right, item_bool_need_to_append_output = _deque_to_process_nodes.pop()
if item_bool_need_to_traverse_left :
if item_node._node_next_left :
item_bool_need_to_traverse_left = False
#
# Prep next iteration
#
_deque_to_process_nodes.append( [ item_node,
item_bool_need_to_traverse_left,
item_bool_need_to_traverse_right,
item_bool_need_to_append_output,
] )
_deque_to_process_nodes.append( [ item_node._node_next_left,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] )
# Skip past storing the node and the right check for now
continue
if item_bool_need_to_traverse_right :
if item_node._node_next_right :
item_bool_need_to_traverse_right = False
#
# Prep next iteration
#
_deque_to_process_nodes.append( [ item_node,
item_bool_need_to_traverse_left,
item_bool_need_to_traverse_right,
item_bool_need_to_append_output,
] )
_deque_to_process_nodes.append( [ item_node._node_next_right,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] )
continue
_list_to_return.append( item_node )
# Reminder: When we get here, we're done with this node and its children
return _list_to_return
def get_list_of_nodes_traversal_pre_order( self, arg_node_root = None ):
"""
This method returns a list of nodes.
Order:
-Starts with root
-Then the left most path from root to the left most leaf
-Makes its way to the right
-Node parent -> left child -> left grand child
Strategic considerations:
-Starts with root
-Left most leaves will end up in the middle left
-Right most leaves will end up in the middle right
-Right-most leaves will end up further towards the end
Visual example ( source: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ ):
1
/ \
2 3
/ \ \
4 5 6
Order: 1 2 4 5 3 6
Tests and status:
-List in correct order - passed
-No infinite loop - passed
"""
_list_to_return = [ ]
_node_root = arg_node_root if arg_node_root else self
_deque_to_process_nodes = deque( [ [ _node_root,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] ] )
while _deque_to_process_nodes :
item_node, item_bool_need_to_traverse_left, item_bool_need_to_traverse_right, item_bool_need_to_append_output = _deque_to_process_nodes.pop()
if item_bool_need_to_append_output :
item_bool_need_to_append_output = False
_list_to_return.append( item_node )
if item_bool_need_to_traverse_left :
if item_node._node_next_left :
item_bool_need_to_traverse_left = False
#
# Prep next iteration
#
_deque_to_process_nodes.append( [ item_node,
item_bool_need_to_traverse_left,
item_bool_need_to_traverse_right,
item_bool_need_to_append_output, ] )
_deque_to_process_nodes.append( [ item_node._node_next_left,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] )
# Skip past storing the node and the right check for now
continue
if item_bool_need_to_traverse_right :
if item_node._node_next_right :
item_bool_need_to_traverse_right = False
#
# Prep next iteration
#
_deque_to_process_nodes.append( [ item_node,
item_bool_need_to_traverse_left,
item_bool_need_to_traverse_right,
item_bool_need_to_append_output, ] )
_deque_to_process_nodes.append( [ item_node._node_next_right,
True, # item_bool_need_to_traverse_left
True, # item_bool_need_to_traverse_right
True, # item_bool_need_to_append_output
] )
# Reminder: When we get here, we're done with this node and its children
return _list_to_return
#
# Public - get - node
#
def get_node_at_key( self, arg_key, **kwargs ):
"""
Returns the node located at arg_key via binary search.
"""
#
# Setup initial values
#
_bool_raise_error_if_node_not_found = kwargs.get( "arg_bool_raise_error_if_node_not_found", True, )
_value_to_return_if_node_not_found = kwargs.get( "arg_value_to_return_if_node_not_found", None, )
_node_start = kwargs.get( "arg_node_root", self, )
#
# Iterate over tree
#
item_node = _node_start
while True :
if item_node :
item_key = item_node._key_unique
#
# If key match found, return item_node
#
if arg_key == item_key :
return item_node
#
# Prep next iteration
#
elif arg_key > item_key :
item_node = item_node._node_next_right
elif arg_key < item_key :
item_node = item_node._node_next_left
#
# If item_node is None, either...
# -Raise error
# -Return default value
#
else :
if _bool_raise_error_if_node_not_found :
self._raise_error_because_key_not_found( arg_key = arg_key,
arg_node = _node_start, )
else :
return _value_to_return_if_node_not_found
#
# Public - insert
#
def insert_node( self, arg_node, arg_node_root = None ):
"""
Inserts node via the value stored in its _key_unique attribute.
"""
_target_key = arg_node._key_unique
_node_start = self if arg_node_root == None else arg_node_root
#
# Iterate over tree
# arg_node should insert in all cases, and the loop will exit when that happens
#
item_node = _node_start
while True :
item_key = item_node._key_unique
if item_key > _target_key :
item_node_next = item_node._node_next_left
if item_node_next :
item_node = item_node_next
else :
item_node._node_next_left = arg_node
break
#
# Check the right node if
#
elif item_key < _target_key :
item_node_next = item_node._node_next_right
if item_node_next :
item_node = item_node_next
else :
item_node._node_next_right = arg_node
break
#
# If keys match...
# The general best practice is to append a duplicate key to the right of a node
#
else :
item_node_next = item_node._node_next_right
if item_node_next :
item_node = item_node_next
else :
item_node._node_next_right = arg_node
break
#
# Public - logic
#
def logic_circular_reference_exists( self, arg_node_root = None ):
"""
Returns True if the algorithm detects a repeated node within the tree.
"""
_node_start = self if arg_node_root == None else arg_node_root
#
# Traverse over tree
#
_set_of_nodes_traversed = set()
_deque_to_process = deque( [ [ _node_start,
None, # item_node_previous
None, # item_key_previous
] ] )
while _deque_to_process :
item_node, item_node_previous, item_key_previous = _deque_to_process.pop()
if item_node in _set_of_nodes_traversed :
return True
else :
_set_of_nodes_traversed.add( item_node )
#
# Prep next iteration
#
if item_node._node_next_left :
_deque_to_process.append( [ item_node._node_next_left,
item_node,
item_node._key_unique, ] )
if item_node._node_next_right :
_deque_to_process.append( [ item_node._node_next_right,
item_node,
item_node._key_unique, ] )
def logic_key_exists( self, arg_key, **kwargs ):
#
# Setup initial values
#
_node_start = kwargs.get( "arg_node_root", self, )
#
# Iterate over tree
#
item_node = _node_start
while True :
#
# If item_node is a node, then process its unique key
#
if item_node :
item_key = item_node._key_unique
#
# If key match found, return True
#
if item_key == arg_key :
return True
#
# Prep next iteration
#
# If item_key is less than arg_key, go right
#
elif item_key < arg_key :