GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-interview-questions/bst_add_child.py at master · anushabolishettii/python-interview-questions Expand file tree/ Copy pathbst_add_child.pyMore file actions 60 lines (44 loc) · 1.63 KB / Copy pathbst_add_child.pyFile metadata and controls60 lines (44 loc) · 1.63 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 class Node(object): def __init__(self, data, left=None, right=None): """ Creates a node with data and optional left/right nodes. """ self.data = data self.left = left self.right = right def insert(self, new_data): """ Inserts a new node with 'new_data' to BST tree rooted here. A "balanced" binary search tree is one where the nodes are equitably spread out to guarantee O(log n) search. For this code challenge, you should not try to re-arrange the tree to rebalance it after adding an item; instead, you should simply find the correct place in the current tree to add it. 4 2 7 1 3 5 8 >>> t = Node(4, Node(2, Node(1), Node(3)), Node(7, Node(5), Node(8))) >>> t.insert(0) >>> t.left.left.left.data == 0 True >>> t.left.left.right is None True >>> t.insert(9) >>> t.right.right.right.data == 9 True >>> t.right.right.left is None True >>> t.insert(6) >>> t.right.left.right.data == 6 True >>> t.right.left.left is None True """ if new_data < self.data: if self.left is None: self.left = Node(new_data) else: self.left.insert(new_data) else: if self.right is None: self.right = Node(new_data) else: self.right.insert(new_data) if __name__ == '__main__': import doctest results = doctest.testmod() if results.failed == 0: print "ALL TESTS PASSED!" |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |