forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request keon#208 from danghai/algo_bst
Add some problem to bst
- Loading branch information
Showing
6 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |