|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | + |
| 4 | +class Node(object): |
| 5 | + """Tree node: left and right child + data which can be any object |
| 6 | +
|
| 7 | + """ |
| 8 | + def __init__(self, data): |
| 9 | + """Node constructor |
| 10 | +
|
| 11 | + @param data node data object |
| 12 | + """ |
| 13 | + self.left = None |
| 14 | + self.right = None |
| 15 | + self.data = data |
| 16 | + |
| 17 | + def insert(self, data): |
| 18 | + """Insert new node with data |
| 19 | +
|
| 20 | + @param data node data object to insert |
| 21 | + """ |
| 22 | + if self.data: |
| 23 | + if data < self.data: |
| 24 | + if self.left is None: |
| 25 | + self.left = Node(data) |
| 26 | + else: |
| 27 | + self.left.insert(data) |
| 28 | + elif data > self.data: |
| 29 | + if self.right is None: |
| 30 | + self.right = Node(data) |
| 31 | + else: |
| 32 | + self.right.insert(data) |
| 33 | + else: |
| 34 | + self.data = data |
| 35 | + |
| 36 | + def lookup(self, data, parent=None): |
| 37 | + """Lookup node containing data |
| 38 | +
|
| 39 | + @param data node data object to look up |
| 40 | + @param parent node's parent |
| 41 | + @returns node and node's parent if found or None, None |
| 42 | + """ |
| 43 | + if data < self.data: |
| 44 | + if self.left is None: |
| 45 | + return None, None |
| 46 | + return self.left.lookup(data, self) |
| 47 | + elif data > self.data: |
| 48 | + if self.right is None: |
| 49 | + return None, None |
| 50 | + return self.right.lookup(data, self) |
| 51 | + else: |
| 52 | + return self, parent |
| 53 | + |
| 54 | + def delete(self, data): |
| 55 | + """Delete node containing data |
| 56 | +
|
| 57 | + @param data node's content to delete |
| 58 | + """ |
| 59 | + # get node containing data |
| 60 | + node, parent = self.lookup(data) |
| 61 | + if node is not None: |
| 62 | + children_count = node.children_count() |
| 63 | + if children_count == 0: |
| 64 | + # if node has no children, just remove it |
| 65 | + if parent: |
| 66 | + if parent.left is node: |
| 67 | + parent.left = None |
| 68 | + else: |
| 69 | + parent.right = None |
| 70 | + else: |
| 71 | + self.data = None |
| 72 | + elif children_count == 1: |
| 73 | + # if node has 1 child |
| 74 | + # replace node by its child |
| 75 | + if node.left: |
| 76 | + n = node.left |
| 77 | + else: |
| 78 | + n = node.right |
| 79 | + if parent: |
| 80 | + if parent.left is node: |
| 81 | + parent.left = n |
| 82 | + else: |
| 83 | + parent.right = n |
| 84 | + else: |
| 85 | + self.left = n.left |
| 86 | + self.right = n.right |
| 87 | + self.data = n.data |
| 88 | + else: |
| 89 | + # if node has 2 children |
| 90 | + # find its successor |
| 91 | + parent = node |
| 92 | + successor = node.right |
| 93 | + while successor.left: |
| 94 | + parent = successor |
| 95 | + successor = successor.left |
| 96 | + # replace node data by its successor data |
| 97 | + node.data = successor.data |
| 98 | + # fix successor's parent node child |
| 99 | + if parent.left == successor: |
| 100 | + parent.left = successor.right |
| 101 | + else: |
| 102 | + parent.right = successor.right |
| 103 | + |
| 104 | + def compare_trees(self, node): |
| 105 | + """Compare 2 trees |
| 106 | +
|
| 107 | + @param node tree to compare |
| 108 | + @returns True if the tree passed is identical to this tree |
| 109 | + """ |
| 110 | + if node is None: |
| 111 | + return False |
| 112 | + if self.data != node.data: |
| 113 | + return False |
| 114 | + res = True |
| 115 | + if self.left is None: |
| 116 | + if node.left: |
| 117 | + return False |
| 118 | + else: |
| 119 | + res = self.left.compare_trees(node.left) |
| 120 | + if res is False: |
| 121 | + return False |
| 122 | + if self.right is None: |
| 123 | + if node.right: |
| 124 | + return False |
| 125 | + else: |
| 126 | + res = self.right.compare_trees(node.right) |
| 127 | + return res |
| 128 | + |
| 129 | + def print_tree(self): |
| 130 | + """Print tree content inorder |
| 131 | +
|
| 132 | + """ |
| 133 | + if self.left: |
| 134 | + self.left.print_tree() |
| 135 | + print(self.data, end=" ") |
| 136 | + if self.right: |
| 137 | + self.right.print_tree() |
| 138 | + |
| 139 | + def tree_data(self): |
| 140 | + """Generator to get the tree nodes data |
| 141 | +
|
| 142 | + """ |
| 143 | + # we use a stack to traverse the tree in a non-recursive way |
| 144 | + stack = [] |
| 145 | + node = self |
| 146 | + while stack or node: |
| 147 | + if node: |
| 148 | + stack.append(node) |
| 149 | + node = node.left |
| 150 | + else: |
| 151 | + # we are returning so we pop the node and we yield it |
| 152 | + node = stack.pop() |
| 153 | + yield node.data |
| 154 | + node = node.right |
| 155 | + |
| 156 | + def children_count(self): |
| 157 | + """Return the number of children |
| 158 | +
|
| 159 | + @returns number of children: 0, 1, 2 |
| 160 | + """ |
| 161 | + cnt = 0 |
| 162 | + if self.left: |
| 163 | + cnt += 1 |
| 164 | + if self.right: |
| 165 | + cnt += 1 |
| 166 | + return cnt |
0 commit comments