Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. class Node:
  2. def __init__(self, val): #defining class node and initializing values
  3. self.l = None #left
  4. self.r = None #left
  5. self.v = val #value
  6.  
  7. class Tree: #new class tree
  8. def __init__(self):
  9. self.root = None #root
  10. self.size = 0 #size
  11.  
  12. def getRoot(self):
  13. return self.root #return root
  14.  
  15. def add(self, val): #public add
  16. self.size+=1
  17. if(self.root == None): #if the root is none
  18. self.root = Node(val) #the root =the new value
  19. else:
  20. self._add(val, self.root) #if not, then we use the add method below
  21.  
  22. def _add(self, val, node): #non public add
  23. if(val < node.v): #if less than node
  24. if(node.l != None): #if not empty
  25. self._add(val, node.l) #redo function on that node
  26. else:
  27. node.l = Node(val) #add to left if node is empty
  28. else:
  29. if(node.r != None): #same for right side if greater than
  30. self._add(val, node.r)
  31. else:
  32. node.r = Node(val)
  33.  
  34. def find(self, val): #search and return
  35. if(self.root != None): #if root isn't empty
  36. return self._find(val, self.root) #return the value from our nonpublic find method
  37. else:
  38. return None #if empty return none
  39.  
  40. def _find(self, val, node):
  41. if(val == node.v): #check current node
  42. return node
  43. elif(val < node.v and node.l != None): #if less than current move to left
  44. self._find(val, node.l)#recurse
  45. elif(val > node.v and node.r != None): #if greater than, move to right
  46. self._find(val, node.r) #recursively run
  47.  
  48. def avg_cmp(bst): #average comparisons (basically computing mean)
  49. return _sum_cmp(bst.root,0)/bst.size #using sum method on bst divided by size
  50.  
  51. def _sum_cmp(root,depth):
  52.  
  53. if root == None:
  54. return 0 #0 for empty tree
  55.  
  56. return _sum_cmp(root.l,depth+1)+_sum_cmp(root.r,depth+1)+depth+1 #summing
  57.  
  58. def max_depth(bst): #max depth
  59. return max_height(bst)
  60.  
  61. def max_height(bst): #max height
  62. return _max_height(bst.root,0)
  63.  
  64. def _max_height(root,depth):
  65.  
  66. if root == None: #if none
  67. return depth-1 #return depth - 1
  68.  
  69. return max(_max_height(root.l,depth+1),_max_height(root.r,depth+1)) #recurse
  70.  
  71. def avg_height(bst):
  72. return -(avg_cmp(bst)-(max_height(bst)+1)) #- (avg comparison - max height +1)
  73.  
  74. def avg_depth(bst):
  75. return avg_cmp(bst)-1 #avg comparisons - 1
  76.  
  77. def _sum_height(root,depth,maxHeight):
  78.  
  79. if root == None:
  80. return 0 # 0 if no root
  81.  
  82. return _sum_height(root.l,depth+1,maxHeight)+_sum_height(root.r,depth+1,maxHeight)+(maxHeight-depth) recurse
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement