mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
fix few details, stacks
This commit is contained in:
parent
dffd4fb2b7
commit
5a7a97b25f
173
src/trees/binary_trees_and_others/binary_tree.py
Normal file
173
src/trees/binary_trees_and_others/binary_tree.py
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
__author__ = "Mari Wahl"
|
||||||
|
__email__ = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
''' Implementation of a binary tree and its properties. For example, the following bt:
|
||||||
|
|
||||||
|
1 ---> level 0
|
||||||
|
2 3 ---> level 1
|
||||||
|
4 5 ---> level 2
|
||||||
|
6 7 ---> level 3
|
||||||
|
8 9 ---> level 4
|
||||||
|
|
||||||
|
has the following properties:
|
||||||
|
|
||||||
|
- SIZE OR NUMBER OF NODES: n = 9
|
||||||
|
- NUMBER OF BRANCHES OR INTERNAL NODES: b = n-1 = 8
|
||||||
|
- VALUE OF ROOT = 1
|
||||||
|
- MAX_DEPTH OR HEIGHT: h = 4
|
||||||
|
- IS BALANCED? NO
|
||||||
|
- IS BST? NO
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
class NodeBT(object):
|
||||||
|
def __init__(self, item=None, level=0):
|
||||||
|
self.item = item
|
||||||
|
self.level = level
|
||||||
|
self.left = None
|
||||||
|
self.right = None
|
||||||
|
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '{}'.format(self.item)
|
||||||
|
|
||||||
|
|
||||||
|
def _addNextNode(self, value, level_here=1):
|
||||||
|
new_node = NodeBT(value, level_here)
|
||||||
|
if not self.item:
|
||||||
|
self.item = new_node
|
||||||
|
elif not self.left:
|
||||||
|
self.left = new_node
|
||||||
|
elif not self.right:
|
||||||
|
self.right = new_node
|
||||||
|
else:
|
||||||
|
self.left = self.left._addNextNode(value, level_here+1)
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
def _searchForNode(self, value):
|
||||||
|
if self.item == value:
|
||||||
|
return self
|
||||||
|
else:
|
||||||
|
found = None
|
||||||
|
if self.left:
|
||||||
|
found = self.left._searchForNode(value)
|
||||||
|
if self.right:
|
||||||
|
found = found or self.right._searchForNode(value)
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def _isLeaf(self):
|
||||||
|
return not self.right and not self.left
|
||||||
|
|
||||||
|
|
||||||
|
def _getMaxHeight(self):
|
||||||
|
''' Get the max height at the node, O(n)'''
|
||||||
|
levelr, levell = 0, 0
|
||||||
|
if self.right:
|
||||||
|
levelr = self.right._getMaxHeight() + 1
|
||||||
|
if self.left:
|
||||||
|
levell = self.left._getMaxHeight() + 1
|
||||||
|
return max(levelr, levell)
|
||||||
|
|
||||||
|
|
||||||
|
def _getMinHeight(self, level=0):
|
||||||
|
''' Get the min height at the node, O(n)'''
|
||||||
|
levelr, levell = -1, -1
|
||||||
|
if self.right:
|
||||||
|
levelr = self.right._getMinHeight(level +1)
|
||||||
|
if self.left:
|
||||||
|
levell = self.left._getMinHeight(level +1)
|
||||||
|
return min(levelr, levell) + 1
|
||||||
|
|
||||||
|
|
||||||
|
def _isBalanced(self):
|
||||||
|
''' Find whether the tree is balanced, by calculating heights first, O(n2) '''
|
||||||
|
if self._getMaxHeight() - self._getMinHeight() < 2:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if self._isLeaf():
|
||||||
|
return True
|
||||||
|
elif self.left and self.right:
|
||||||
|
return self.left._isBalanced() and self.right._isBalanced()
|
||||||
|
elif not self.left and self.right:
|
||||||
|
return self.right._isBalanced()
|
||||||
|
elif not self.right and self.left:
|
||||||
|
return self.left._isBalanced()
|
||||||
|
|
||||||
|
def _isBST(self):
|
||||||
|
''' Find whether the tree is a BST, inorder '''
|
||||||
|
if self.item:
|
||||||
|
if self._isLeaf():
|
||||||
|
return True
|
||||||
|
elif self.left:
|
||||||
|
if self.left.item < self.item:
|
||||||
|
return self.left._isBST()
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
elif self.right:
|
||||||
|
if self.right.item > self.item:
|
||||||
|
return self.right._isBST()
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
raise Exception('Tree is empty')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class BinaryTree(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.root = None
|
||||||
|
|
||||||
|
|
||||||
|
def addNode(self, value):
|
||||||
|
if not self.root:
|
||||||
|
self.root = NodeBT(value)
|
||||||
|
else:
|
||||||
|
self.root._addNextNode(value)
|
||||||
|
|
||||||
|
|
||||||
|
def isLeaf(self, value):
|
||||||
|
node = self.root._searchForNode(value)
|
||||||
|
return node._isLeaf()
|
||||||
|
|
||||||
|
def getNodeLevel(self, item):
|
||||||
|
node = self.root._searchForNode(item)
|
||||||
|
if node:
|
||||||
|
return node.level
|
||||||
|
else:
|
||||||
|
raise Exception('Node not found')
|
||||||
|
|
||||||
|
def isRoot(self, value):
|
||||||
|
return self.root.item == value
|
||||||
|
|
||||||
|
def getHeight(self):
|
||||||
|
return self.root._getMaxHeight()
|
||||||
|
|
||||||
|
def isBalanced(self):
|
||||||
|
return self.root._isBalanced()
|
||||||
|
|
||||||
|
def isBST(self):
|
||||||
|
return self.root._isBST()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
bt = BinaryTree()
|
||||||
|
print "Adding nodes 1 to 10 in the tree..."
|
||||||
|
for i in range(1, 10):
|
||||||
|
bt.addNode(i)
|
||||||
|
print "Is 8 a leaf? ", bt.isLeaf(8)
|
||||||
|
print "Whats the level of node 8? ", bt.getNodeLevel(8)
|
||||||
|
print "Is node 10 a root? ", bt.isRoot(10)
|
||||||
|
print "Is node 1 a root? ", bt.isRoot(1)
|
||||||
|
print "Whats the tree height? ", bt.getHeight()
|
||||||
|
print "Is this tree BST? ", bt.isBST()
|
||||||
|
print "Is this tree balanced? ", bt.isBalanced()
|
64
src/trees/binary_trees_and_others/binary_tree_lists.py
Normal file
64
src/trees/binary_trees_and_others/binary_tree_lists.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
__author__ = "Mari Wahl"
|
||||||
|
__email__ = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
|
|
||||||
|
''' constructs a list with a root and 2 empty sublists for the children. To add a left subtree to the root of a tree, we need to insert a new list into the second position of the root list '''
|
||||||
|
|
||||||
|
|
||||||
|
def BinaryTreeList(r):
|
||||||
|
return [r, [], []]
|
||||||
|
|
||||||
|
|
||||||
|
def insertLeft(root, newBranch):
|
||||||
|
t = root.pop(1)
|
||||||
|
if len(t) > 1:
|
||||||
|
root.insert(1,[newBranch,t,[]])
|
||||||
|
else:
|
||||||
|
root.insert(1,[newBranch, [], []])
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
|
def insertRight(root, newBranch):
|
||||||
|
t = root.pop(2)
|
||||||
|
if len(t) > 1:
|
||||||
|
root.insert(2,[newBranch,[],t])
|
||||||
|
else:
|
||||||
|
root.insert(2,[newBranch,[],[]])
|
||||||
|
return root
|
||||||
|
|
||||||
|
def getRootVal(root):
|
||||||
|
return root[0]
|
||||||
|
|
||||||
|
def setRootVal(root, newVal):
|
||||||
|
root[0] = newVal
|
||||||
|
|
||||||
|
def getLeftChild(root):
|
||||||
|
return root[1]
|
||||||
|
|
||||||
|
def getRightChild(root):
|
||||||
|
return root[2]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
'''
|
||||||
|
3
|
||||||
|
[5, [4, [], []], []]
|
||||||
|
[7, [], [6, [], []]]
|
||||||
|
'''
|
||||||
|
|
||||||
|
r = BinaryTreeList(3)
|
||||||
|
insertLeft(r,4)
|
||||||
|
insertLeft(r,5)
|
||||||
|
insertRight(r,6)
|
||||||
|
insertRight(r,7)
|
||||||
|
print(getRootVal(r))
|
||||||
|
print(getLeftChild(r))
|
||||||
|
print(getRightChild(r))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
|
||||||
|
|
||||||
|
__author__ = "Mari Wahl"
|
||||||
|
__email__ = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
class BunchClass(dict):
|
class BunchClass(dict):
|
||||||
def __init__(self, *args, **kwds):
|
def __init__(self, *args, **kwds):
|
@ -1,13 +1,16 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
|
||||||
|
|
||||||
|
__author__ = "Mari Wahl"
|
||||||
|
__email__ = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
|
""" A class for a simple tree """
|
||||||
|
|
||||||
class SimpleTree(object):
|
class SimpleTree(object):
|
||||||
def __init__(self, value, children = None):
|
def __init__(self, value=None, children = None):
|
||||||
if children == None: children = []
|
|
||||||
self.children = children
|
|
||||||
self.value = value
|
self.value = value
|
||||||
|
self.children = children
|
||||||
|
if self.children == None:
|
||||||
|
self.children = []
|
||||||
|
|
||||||
def __repr__(self, level=0):
|
def __repr__(self, level=0):
|
||||||
ret = "\t"*level+repr(self.value)+"\n"
|
ret = "\t"*level+repr(self.value)+"\n"
|
||||||
@ -16,6 +19,7 @@ class SimpleTree(object):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
'a'
|
'a'
|
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python
|
||||||
# mari von steinkirch 2013
|
|
||||||
# http://astro.sunysb.edu/steinkirch
|
|
||||||
|
|
||||||
|
__author__ = "Mari Wahl"
|
||||||
|
__email__ = "marina.w4hl@gmail.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -274,7 +274,7 @@ class BinaryTree(object):
|
|||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
''' Construtor for the Binary Tree, which is a container of Nodes'''
|
''' Constructor for the Binary Tree, which is a container of Nodes'''
|
||||||
self.root = None
|
self.root = None
|
||||||
|
|
||||||
|
|
||||||
@ -342,7 +342,7 @@ class BinaryTree(object):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
''' The followin methods are for searching the lowest common ancestor
|
''' The following methods are for searching the lowest common ancestor
|
||||||
in a BT. Since a simple BT does not have ordering, it can be O(n). If
|
in a BT. Since a simple BT does not have ordering, it can be O(n). If
|
||||||
we have a link for the ancestors, the steps are:
|
we have a link for the ancestors, the steps are:
|
||||||
(1) search both trees in order to find the nodes separately
|
(1) search both trees in order to find the nodes separately
|
Loading…
x
Reference in New Issue
Block a user