Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.4 KiB

  1. class Node():
  2. def __init__(self,key):
  3. self.key = key
  4. self.left = None
  5. self.right = None
  6. self.parent = None
  7. class Tree():
  8. def __init__(self):
  9. self.root = None
  10. def add_node(self,key,node=None):
  11. if node is None:
  12. node = self.root
  13. if self.root is None:
  14. self.root = Node(key)
  15. else:
  16. if key <= node.key :
  17. if node.left is None:
  18. node.left = Node(key)
  19. node.left.parent = node
  20. print "left"
  21. return
  22. else:
  23. # return self.add_node(key,node = self.root.left)
  24. return self.add_node(key,node = node.left)
  25. else:
  26. if node.right is None:
  27. node.right = Node(key)
  28. node.right.parent = node
  29. print "right"
  30. return
  31. else:
  32. # return self.add_node(key,node = self.root.right)
  33. return self.add_node(key,node = node.right)
  34. def search(self,key,node = None):
  35. if node is None:
  36. node = self.root
  37. if self.root.key == key:
  38. print "key is at the root"
  39. return self.root
  40. else:
  41. if node.key == key :
  42. print "key exists"
  43. return node
  44. elif key < node.key and node.left is not None:
  45. print "left"
  46. return self.search(key,node = node.left)
  47. elif key > node.key and node.right is not None:
  48. print "right"
  49. return self.search(key,node = node.right)
  50. else:
  51. print "key does not exist"
  52. return None
  53. def delete_node(self,key,node=None):
  54. #search for the node to be deleted in tree
  55. if node is None:
  56. node = self.search(key)#return the node to be deleted
  57. #root has no parent node
  58. if self.root.key == node.key: #if it is root
  59. parent_node = self.root
  60. else:
  61. parent_node = node.parent
  62. '''case 1: The node has no chidren'''
  63. if node.left is None and node.right is None:
  64. if key <= parent_node.key:
  65. parent_node.left = None
  66. else:
  67. parent_node.right = None
  68. return
  69. '''case 2: The node has children'''
  70. ''' if it has a single left node'''
  71. if node.left is not None and node.right is None :
  72. if node.left.key < parent_node.key :
  73. parent_node.left = node.left
  74. else:
  75. parent_node.right = node.left
  76. return
  77. '''if it has a single right node'''
  78. if node.right is not None and node.left is None:
  79. if node.key <= parent_node.key:
  80. parent_node.left = node.right
  81. else:
  82. parent_node.right = node.right
  83. return
  84. '''if it has two children'''
  85. '''find the node with the minimum value from the right subtree.
  86. copy its value to thhe node which needs to be removed.
  87. right subtree now has a duplicate and so remove it.'''
  88. if node.left is not None and node.right is not None:
  89. min_value = self.find_minimum(node)
  90. node.key = min_value.key
  91. min_value.parent.left = None
  92. return
  93. def find_minimum(self,node = None):
  94. if node is None:
  95. node = self.root
  96. '''find mimimum value from the right subtree'''
  97. '''case when there is only a root node'''
  98. if node.right is not None:
  99. node = node.right
  100. else:
  101. return node
  102. if node.left is not None:
  103. return self.find_minimum(node = node.left)
  104. else:
  105. return node
  106. def tree_data(self,node=None):
  107. if node is None:
  108. node = self.root
  109. stack = []
  110. while stack or node:
  111. if node is not None:
  112. stack.append(node)
  113. node = node.left
  114. else:
  115. node = stack.pop()
  116. yield node.key
  117. node = node.right
  118. t=Tree()
  119. t.add_node(10)
  120. t.add_node(13)
  121. t.add_node(14)
  122. t.add_node(8)
  123. t.add_node(9)
  124. t.add_node(7)
  125. t.add_node(11)
  126. '''
  127. 10---8---7
  128. | |
  129. | ---9
  130. ---13---11
  131. |
  132. ---14
  133. '''