Skip to content

Commit

Permalink
Merge pull request keon#208 from danghai/algo_bst
Browse files Browse the repository at this point in the history
Add some problem to bst
  • Loading branch information
goswami-rahul authored Apr 10, 2018
2 parents ecffa5c + 44dffaa commit 03387fa
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ Thanks for your interest in contributing! There are many ways to contribute to t
- [serialize_deserialize](tree/bst/serialize_deserialize.py)
- [successor](tree/bst/successor.py)
- [unique_bst](tree/bst/unique_bst.py)
- [depth_sum](tree/bst/depth_sum.py)
- [count_left_node](tree/bst/count_left_node.py)
- [num_empty](tree/bst/num_empty.py)
- [height](tree/bst/height.py)
- [red_black_tree](tree/red_black_tree)
- [red_black_tree](tree/red_black_tree/red_black_tree.py)
- [segment_tree](tree/segment_tree)
Expand Down
4 changes: 2 additions & 2 deletions tree/bst/bst.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def postorder(self, root):
/ / \
7 20 30
/
13
18
"""

class TestSuite(unittest.TestCase):
Expand All @@ -126,7 +126,7 @@ def setUp(self):
self.tree.insert(7)
self.tree.insert(20)
self.tree.insert(30)
self.tree.insert(13)
self.tree.insert(18)

def test_search(self):
self.assertTrue(self.tree.search(24))
Expand Down
61 changes: 61 additions & 0 deletions tree/bst/count_left_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
Write a function count_left_node returns the number of left children in the
tree. For example: the following tree has four left children (the nodes
storing the values 6, 3, 7, and 10):
9
/ \
6 12
/ \ / \
3 8 10 15
/ \
7 18
count_left_node = 4
"""
import unittest
from bst import Node
from bst import bst

def count_left_node(root):
if root is None:
return 0
elif root.left is None:
return count_left_node(root.right)
else:
return 1 + count_left_node(root.left) + count_left_node(root.right)

"""
The tree is created for testing:
9
/ \
6 12
/ \ / \
3 8 10 15
/ \
7 18
count_left_node = 4
"""

class TestSuite(unittest.TestCase):
def setUp(self):
self.tree = bst()
self.tree.insert(9)
self.tree.insert(6)
self.tree.insert(12)
self.tree.insert(3)
self.tree.insert(8)
self.tree.insert(10)
self.tree.insert(15)
self.tree.insert(7)
self.tree.insert(18)

def test_count_left_node(self):
self.assertEqual(4, count_left_node(self.tree.root))

if __name__ == '__main__':
unittest.main()
66 changes: 66 additions & 0 deletions tree/bst/depth_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Write a function depthSum returns the sum of the values stored
in a binary search tree of integers weighted by the depth of each value.
For example:
9
/ \
6 12
/ \ / \
3 8 10 15
/ \
7 18
depth_sum = 1*9 + 2*(6+12) + 3*(3+8+10+15) + 4*(7+18)
"""
import unittest
from bst import Node
from bst import bst

def depth_sum(root, n):
if root:
return recur_depth_sum(root, 1)

def recur_depth_sum(root, n):
if root is None:
return 0
elif root.left is None and root.right is None:
return root.data * n
else:
return n * root.data + recur_depth_sum(root.left, n+1) + recur_depth_sum(root.right, n+1)

"""
The tree is created for testing:
9
/ \
6 12
/ \ / \
3 8 10 15
/ \
7 18
depth_sum = 1*9 + 2*(6+12) + 3*(3+8+10+15) + 4*(7+18)
"""

class TestSuite(unittest.TestCase):
def setUp(self):
self.tree = bst()
self.tree.insert(9)
self.tree.insert(6)
self.tree.insert(12)
self.tree.insert(3)
self.tree.insert(8)
self.tree.insert(10)
self.tree.insert(15)
self.tree.insert(7)
self.tree.insert(18)

def test_depth_sum(self):
self.assertEqual(253, depth_sum(self.tree.root, 4))

if __name__ == '__main__':
unittest.main()
60 changes: 60 additions & 0 deletions tree/bst/height.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Write a function height returns the height of a tree. The height is defined to
be the number of levels. The empty tree has height 0, a tree of one node has
height 1, a root node with one or two leaves as children has height 2, and so on
For example: height of tree is 4
9
/ \
6 12
/ \ / \
3 8 10 15
/ \
7 18
height = 4
"""
import unittest
from bst import Node
from bst import bst

def height(root):
if root is None:
return 0
else:
return 1 + max(height(root.left), height(root.right))

"""
The tree is created for testing:
9
/ \
6 12
/ \ / \
3 8 10 15
/ \
7 18
count_left_node = 4
"""

class TestSuite(unittest.TestCase):
def setUp(self):
self.tree = bst()
self.tree.insert(9)
self.tree.insert(6)
self.tree.insert(12)
self.tree.insert(3)
self.tree.insert(8)
self.tree.insert(10)
self.tree.insert(15)
self.tree.insert(7)
self.tree.insert(18)

def test_height(self):
self.assertEqual(4, height(self.tree.root))

if __name__ == '__main__':
unittest.main()
67 changes: 67 additions & 0 deletions tree/bst/num_empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Write a function num_empty returns returns the number of empty branches in a
tree. Function should count the total number of empty branches among the nodes
of the tree. A leaf node has two empty branches. In the case, if root is None,
it considered as a 1 empty branch
For example: the following tree has 10 empty branch (* is empty branch)
9 __
/ \___
6 12
/ \ / \
3 8 10 15
/ \ / \ / \ / \
* * 7 * * * * 18
/ \ / \
* * * *
empty_branch = 10
"""
import unittest
from bst import Node
from bst import bst

def num_empty(root):
if root is None:
return 1
elif root.left is None and root.right:
return 1 + num_empty(root.right)
elif root.right is None and root.left:
return 1 + num_empty(root.left)
else:
return num_empty(root.left) + num_empty(root.right)

"""
The tree is created for testing:
9
/ \
6 12
/ \ / \
3 8 10 15
/ \
7 18
num_empty = 10
"""

class TestSuite(unittest.TestCase):
def setUp(self):
self.tree = bst()
self.tree.insert(9)
self.tree.insert(6)
self.tree.insert(12)
self.tree.insert(3)
self.tree.insert(8)
self.tree.insert(10)
self.tree.insert(15)
self.tree.insert(7)
self.tree.insert(18)

def test_num_empty(self):
self.assertEqual(10, num_empty(self.tree.root))

if __name__ == '__main__':
unittest.main()

0 comments on commit 03387fa

Please sign in to comment.