# Duane's Dungeon - a rogue-like game # Copyright (C) 2023 Duane Robertson # btree.gd - simple b-tree # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . extends RefCounted class_name BTree var leaf var lchild var rchild func _init(in_leaf): leaf = in_leaf func add(in_arr:Array): if len(in_arr) != 2: G.error('BTree.add requires an array of two elements') return lchild = BTree.new(in_arr[0]) rchild = BTree.new(in_arr[1]) func get_leaves(): var lexists:bool = (lchild != null) var rexists:bool = (rchild != null) if lexists and rexists: return lchild.get_leaves() + rchild.get_leaves() elif lexists or rexists: G.error('unbalanced BTree') return else: return [leaf]