|
| 1 | +# 2-3 Tree |
| 2 | +# balanced tree data structure with up to 2 data items per node |
| 3 | + |
| 4 | +class Node: |
| 5 | + def __init__(self, data, par = None): |
| 6 | + #print ("Node __init__: " + str(data)) |
| 7 | + self.data = list([data]) |
| 8 | + self.parent = par |
| 9 | + self.child = list() |
| 10 | + |
| 11 | + def __str__(self): |
| 12 | + if self.parent: |
| 13 | + return str(self.parent.data) + ' : ' + str(self.data) |
| 14 | + return 'Root : ' + str(self.data) |
| 15 | + |
| 16 | + def __lt__(self, node): |
| 17 | + return self.data[0] < node.data[0] |
| 18 | + |
| 19 | + def _isLeaf(self): |
| 20 | + return len(self.child) == 0 |
| 21 | + |
| 22 | + # merge new_node sub-tree into self node |
| 23 | + def _add(self, new_node): |
| 24 | + # print ("Node _add: " + str(new_node.data) + ' to ' + str(self.data)) |
| 25 | + for child in new_node.child: |
| 26 | + child.parent = self |
| 27 | + self.data.extend(new_node.data) |
| 28 | + self.data.sort() |
| 29 | + self.child.extend(new_node.child) |
| 30 | + if len(self.child) > 1: |
| 31 | + self.child.sort() |
| 32 | + for child in self.child: |
| 33 | + child.parent = self |
| 34 | + if len(self.data) > 2: |
| 35 | + self._split() |
| 36 | + |
| 37 | + # find correct node to insert new node into tree |
| 38 | + def _insert(self, new_node): |
| 39 | + # print ('Node _insert: ' + str(new_node.data) + ' into ' + str(self.data)) |
| 40 | + |
| 41 | + # leaf node - add data to leaf and rebalance tree |
| 42 | + if self._isLeaf(): |
| 43 | + self._add(new_node) |
| 44 | + |
| 45 | + # not leaf - find correct child to descend, and do recursive insert |
| 46 | + elif new_node.data[0] > self.data[-1]: |
| 47 | + self.child[-1]._insert(new_node) |
| 48 | + else: |
| 49 | + for i in range(0, len(self.data)): |
| 50 | + if new_node.data[0] < self.data[i]: |
| 51 | + self.child[i]._insert(new_node) |
| 52 | + break |
| 53 | + |
| 54 | + # 3 items in node, split into new sub-tree and add to parent |
| 55 | + def _split(self): |
| 56 | + # print("Node _split: " + str(self.data)) |
| 57 | + left_child = Node(self.data[0], self) |
| 58 | + right_child = Node(self.data[2], self) |
| 59 | + if self.child: |
| 60 | + self.child[0].parent = left_child |
| 61 | + self.child[1].parent = left_child |
| 62 | + self.child[2].parent = right_child |
| 63 | + self.child[3].parent = right_child |
| 64 | + left_child.child = [self.child[0], self.child[1]] |
| 65 | + right_child.child = [self.child[2], self.child[3]] |
| 66 | + |
| 67 | + self.child = [left_child] |
| 68 | + self.child.append(right_child) |
| 69 | + self.data = [self.data[1]] |
| 70 | + |
| 71 | + # now have new sub-tree, self. need to add self to its parent node |
| 72 | + if self.parent: |
| 73 | + if self in self.parent.child: |
| 74 | + self.parent.child.remove(self) |
| 75 | + self.parent._add(self) |
| 76 | + else: |
| 77 | + left_child.parent = self |
| 78 | + right_child.parent = self |
| 79 | + |
| 80 | + # find an item in the tree; return item, or False if not found |
| 81 | + def _find(self, item): |
| 82 | + # print ("Find " + str(item)) |
| 83 | + if item in self.data: |
| 84 | + return item |
| 85 | + elif self._isLeaf(): |
| 86 | + return False |
| 87 | + elif item > self.data[-1]: |
| 88 | + return self.child[-1]._find(item) |
| 89 | + else: |
| 90 | + for i in range(len(self.data)): |
| 91 | + if item < self.data[i]: |
| 92 | + return self.child[i]._find(item) |
| 93 | + |
| 94 | + def _remove(self, item): |
| 95 | + pass |
| 96 | + |
| 97 | + # print preorder traversal |
| 98 | + def _preorder(self): |
| 99 | + print (self) |
| 100 | + for child in self.child: |
| 101 | + child._preorder() |
| 102 | + |
| 103 | +class Tree: |
| 104 | + def __init__(self): |
| 105 | + print("Tree __init__") |
| 106 | + self.root = None |
| 107 | + |
| 108 | + def insert(self, item): |
| 109 | + print("Tree insert: " + str(item)) |
| 110 | + if self.root is None: |
| 111 | + self.root = Node(item) |
| 112 | + else: |
| 113 | + self.root._insert(Node(item)) |
| 114 | + while self.root.parent: |
| 115 | + self.root = self.root.parent |
| 116 | + return True |
| 117 | + |
| 118 | + def find(self, item): |
| 119 | + return self.root._find(item) |
| 120 | + |
| 121 | + def remove(self, item): |
| 122 | + self.root.remove(item) |
| 123 | + |
| 124 | + def printTop2Tiers(self): |
| 125 | + print ('----Top 2 Tiers----') |
| 126 | + print (str(self.root.data)) |
| 127 | + for child in self.root.child: |
| 128 | + print (str(child.data), end=' ') |
| 129 | + print(' ') |
| 130 | + |
| 131 | + def preorder(self): |
| 132 | + print ('----Preorder----') |
| 133 | + self.root._preorder() |
| 134 | + |
| 135 | +tree = Tree() |
| 136 | + |
| 137 | +lst = [13, 7, 24, 15, 4, 29, 20, 16, 19, 1, 5, 22, 17] |
| 138 | +for item in lst: |
| 139 | + tree.insert(item) |
| 140 | +tree.printTop2Tiers() |
| 141 | + |
| 142 | +# for i in range (25): |
| 143 | + # tree.insert(i) |
| 144 | + # tree.printTop2Tiers() |
| 145 | +# tree.preorder() |
| 146 | +# print (tree.find(16)) |
0 commit comments