forked from CCallahanIV/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 1
Bst #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chamberi
wants to merge
17
commits into
master
Choose a base branch
from
bst
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Bst #1
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1d1a5e9
Initial build out of node and bst structures.
CCallahanIV 6f6a938
Added bst and test_bst files.
chamberi 565e132
bst and test_bst files.
chamberi 98c5039
commiting initial insert tests.
0ecf7b2
added insert method.
chamberi c174663
Merge branch 'bst' of https://github.com/chamberi/data-structures int…
chamberi 597732f
commit to pull remote changes.
8f5b2ac
Merge branch 'bst' of https://github.com/chamberi/data-structures int…
5c46d3b
refactor of insert and tests.
chamberi 71ec1a7
merge conflict resolution.
c5587dc
fixed initial tests and insert function, new test for size.
chamberi 994d3cb
commit to pull remote changes.
a2afe95
commiting implementation of depth, balance, and tests.
afecfc5
else to handle if node with value already exists on insert.
f407dc3
edited docstrings.
7842eaa
fixed tests, added iterator to bst.
chamberi fb52cb7
added test results and coverage to the readme, as well as function de…
chamberi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,32 +1,26 @@ | ||
| #Summary | ||
|
|
||
| The assignment was to implement a [Weighted Graph](https://codefellows.github.io/sea-python-401d5/assignments/graph_3_weighted.html) | ||
| in Python containing the following methods: | ||
|
|
||
|
|
||
| * g.nodes(): return a list of all nodes in the graph | ||
| * g.edges(): return a list of all edges in the graph | ||
| * g.add_node(n): adds a new node 'n' to the graph | ||
| * g.add_edge(n1, n2): adds a new edge to the graph connecting 'n1' and 'n2', if either n1 or n2 are not already present in the graph, they should be added. | ||
| * g.del_node(n): deletes the node 'n' from the graph, raises an error if no such node exists | ||
| * g.del_edge(n1, n2): deletes the edge connecting 'n1' and 'n2' from the graph, raises an error if no such edge exists | ||
| * g.has_node(n): True if node 'n' is contained in the graph, False if not. | ||
| * g.neighbors(n): returns the list of all nodes connected to 'n' by edges, raises an error if n is not in g | ||
| * g.adjacent(n1, n2): returns True if there is an edge connecting n1 and n2, False if not, raises an error if either of the supplied nodes are not in g | ||
| * g.depth_first_traversal(start): Returns the path list for the entire graph with a depth first traversal. | ||
| * g.breadth_first_travers(start): Returns the path list for the entire graph with a breadth first traversal. | ||
| insert(self, val): will insert the value val into the BST. If val is already present, it will be ignored. | ||
| search(self, val): will return the node containing that value, else None. | ||
| size(self): will return the integer size of the BST (equal to the total number of values stored in the tree). It will return 0 if the tree is empty. | ||
| depth(self): will return an integer representing the total number of levels in the tree. If there is one value, the depth should be 1, if two values it will be 2, if three values it may be 2 or three, depending, etc. | ||
| contains(self, val): will return True if val is in the BST, False if not. | ||
| balance(self): will return an integer, positive or negative that represents how well balanced the tree is. Trees which are higher on the left than the right should return a positive value, trees which are higher on the right than the left should return a negative value. An ideally-balanced tree should return 0. | ||
|
|
||
|
|
||
| # Coverage: | ||
|
|
||
| 15 of 15 Passing Tests: | ||
|
|
||
| ---------- coverage: platform darwin, python 3.5.2-final-0 ----------- | ||
|
|
||
| Name Stmts Miss Cover Missing | ||
| ----------------------------------------------------------- | ||
| src/bst.py 65 10 85% 30-34, 64, 83-85, 115 | ||
|
|
||
| | Name | Stmts | Miss | Cover | | ||
| | ----------------------- | ----- | ---- | ----- | | ||
| | weighted_graph.py | 78 | 3 | 96% | | ||
| | test_weighted_graph.py | 178 | 0 | 100% | | ||
| | ----------------------- | --- | -- | ---- | | ||
| | TOTAL | 256 | 3 | 98% | | ||
|
|
||
| ---------- coverage: platform darwin, python 2.7.12-final-0 ---------- | ||
|
|
||
| Name Stmts Miss Cover Missing | ||
| ----------------------------------------------------------- | ||
| src/bst.py 65 10 85% 30-34, 64, 83-85, 115 |
This file contains hidden or 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,116 @@ | ||
| """Module for Binary Search Tree.""" | ||
|
|
||
|
|
||
| class Node(object): | ||
| """Node class.""" | ||
|
|
||
| def __init__(self, value=None, left=None, right=None): | ||
| """Init of the Node class.""" | ||
| self.value = value | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
|
|
||
| class BinarySearchTree(object): | ||
| """Binary Search Tree.""" | ||
|
|
||
| """insert(self, val): will insert the value val into the BST. If val is already present, it will be ignored.""" | ||
| """search(self, val): will return the node containing that value, else None""" | ||
| """size(self): will return the integer size of the BST (equal to the total number of values stored in the tree). It will return 0 if the tree is empty.""" | ||
| """depth(self): will return an integer representing the total number of levels in the tree. If there is one value, the depth should be 1, if two values it will be 2, if three values it may be 2 or three, depending, etc.""" | ||
| """contains(self, val): will return True if val is in the BST, False if not.""" | ||
| """balance(self): will return an integer, positive or negative that represents how well balanced the tree is. Trees which are higher on the left than the right should return a positive value, trees which are higher on the right than the left should return a negative value. An ideally-balanced tree should return 0.""" | ||
|
|
||
| def __init__(self, if_iter=None): | ||
| """Init of the Binary Search Tree class.""" | ||
| self.root = None | ||
| self.counter = 0 | ||
| self.container = [] | ||
| if if_iter: | ||
| try: | ||
| for value in if_iter: | ||
| self.insert(value) | ||
| except TypeError: | ||
| self.insert(if_iter) | ||
|
|
||
| def insert(self, val): | ||
| """Take a value, inserts into Binary Search Tree at correct placement.""" | ||
| if self.root is None: | ||
| self.root = Node(val) | ||
| self.counter += 1 | ||
| self.container.append(val) | ||
|
|
||
| else: | ||
| vertex = self.root | ||
| while True: | ||
| if val > vertex.value: | ||
| if vertex.right: | ||
| vertex = vertex.right | ||
| else: | ||
| vertex.right = Node(val) | ||
| self.counter += 1 | ||
| self.container.append(val) | ||
| break | ||
|
|
||
| elif val < vertex.value: | ||
| if vertex.left: | ||
| vertex = vertex.left | ||
| else: | ||
| vertex.left = Node(val) | ||
| self.counter += 1 | ||
| self.container.append(val) | ||
| break | ||
| else: | ||
| break | ||
|
|
||
| def size(self): | ||
| """Return size of Binary Search Tree.""" | ||
| return self.counter | ||
|
|
||
| def contains(self, val): | ||
| """Return True if val is in the BST, False if not.""" | ||
| return val in self.container | ||
|
|
||
| def search(self, val): | ||
| """Return the node containing that value, else None.""" | ||
| vertex = self.root | ||
| while vertex: | ||
| if val > vertex.value: | ||
| if not vertex.right: | ||
| return None | ||
| vertex = vertex.right | ||
| elif val < vertex.value: | ||
| if not vertex.left: | ||
| return None | ||
| vertex = vertex.left | ||
| else: | ||
| return vertex | ||
| return None | ||
|
|
||
| def depth(self): | ||
| """ | ||
| Return an integer representing the total number of levels in the tree. | ||
|
|
||
| If there is one value, the depth should be 1, if two values it will be 2, | ||
| if three values it may be 2 or three, depending, etc. | ||
| """ | ||
| return self.calc_depth(self.root) | ||
|
|
||
| def calc_depth(self, tree): | ||
| """Calculate the depth of the binary search tree recursively.""" | ||
| if tree is None: | ||
| return 0 | ||
| else: | ||
| return max(self.calc_depth(tree.right), self.calc_depth(tree.left)) + 1 | ||
|
|
||
| def balance(self): | ||
| """ | ||
| Return an integer, positive or negative that represents how well balanced the tree is. | ||
|
|
||
| Trees which are higher on the left than the right should return a positive value, | ||
| trees which are higher on the right than the left should return a negative value. | ||
| An ideally-balanced tree should return 0. | ||
| """ | ||
| if self.root is None: | ||
| return 0 | ||
| return self.calc_depth(self.root.right) - self.calc_depth(self.root.left) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re-re-reverse this, as the specs want a left heavy tree to be positive |
||
This file contains hidden or 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,168 @@ | ||
| """Test Module for Binary Search Tree.""" | ||
| from bst import BinarySearchTree | ||
|
|
||
|
|
||
| def test_insert_5_is_root(): | ||
| """Test the insert function inserts a root.""" | ||
| a = BinarySearchTree() | ||
| a.insert(5) | ||
| assert a.root | ||
|
|
||
|
|
||
| def test_insert_5_where_root_equals_5(): | ||
| """Test the insert funciton where root equals 5.""" | ||
| a = BinarySearchTree() | ||
| a.insert(5) | ||
| assert a.root.value == 5 | ||
|
|
||
|
|
||
| def test_insert_5_and_10_and_confirm_right(): | ||
| """Test the insert function where right child of root is 10.""" | ||
| a = BinarySearchTree() | ||
| a.insert(5) | ||
| a.insert(10) | ||
| assert a.root.right.value == 10 | ||
|
|
||
|
|
||
| def test_insert_many_numbers(): | ||
| """Test the insert function.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| a.insert(10) | ||
| a.insert(3) | ||
| a.insert(14) | ||
| a.insert(13) | ||
| a.insert(1) | ||
| a.insert(6) | ||
| a.insert(7) | ||
| a.insert(4) | ||
| assert a.root.right.right.left.value == 13 | ||
| assert a.root.left.value == 3 | ||
| assert a.root.right.right.value == 14 | ||
| assert a.root.value == 8 | ||
| assert a.root.left.right.left.value == 4 | ||
|
|
||
|
|
||
| def test_size_returns_size_of_binary_search_tree(): | ||
| """Test that the size method returns size of the bst.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| a.insert(10) | ||
| a.insert(3) | ||
| a.insert(14) | ||
| a.insert(13) | ||
| a.insert(1) | ||
| a.insert(6) | ||
| a.insert(7) | ||
| a.insert(4) | ||
| assert a.size() == 9 | ||
|
|
||
|
|
||
| def test_binary_search_tree_contains_value(): | ||
| """Test that the contains method returns True if value in binary search tree.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| a.insert(10) | ||
| a.insert(3) | ||
| a.insert(14) | ||
| a.insert(13) | ||
| a.insert(1) | ||
| a.insert(6) | ||
| a.insert(7) | ||
| a.insert(4) | ||
| assert a.contains(4) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would consider using fixtures to make your test code a little cleaner |
||
|
|
||
| def test_binary_search_tree_does_not_contain_value(): | ||
| """Test that the contains method returns True if value in binary search tree.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| a.insert(10) | ||
| a.insert(3) | ||
| a.insert(14) | ||
| a.insert(13) | ||
| a.insert(1) | ||
| a.insert(6) | ||
| a.insert(7) | ||
| a.insert(4) | ||
| assert a.contains(100) is False | ||
|
|
||
|
|
||
| def test_search_5(): | ||
| """Test the search function.""" | ||
| a = BinarySearchTree() | ||
| a.insert(5) | ||
| assert a.search(5) == a.root | ||
|
|
||
|
|
||
| def test_search_10(): | ||
| """Test the search function.""" | ||
| a = BinarySearchTree() | ||
| a.insert(5) | ||
| a.insert(10) | ||
| assert a.search(10) == a.root.right | ||
|
|
||
|
|
||
| def test_search_empty(): | ||
| """Test the search function.""" | ||
| a = BinarySearchTree() | ||
| assert a.search(5) is None | ||
|
|
||
|
|
||
| def test_search_none(): | ||
| """Test the search function.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| a.insert(10) | ||
| a.insert(3) | ||
| a.insert(14) | ||
| a.insert(13) | ||
| a.insert(1) | ||
| a.insert(6) | ||
| a.insert(7) | ||
| a.insert(4) | ||
| assert a.search(100) is None | ||
|
|
||
|
|
||
| def test_depth_zero(): | ||
| """Test the depth function.""" | ||
| a = BinarySearchTree() | ||
| assert a.depth() == 0 | ||
|
|
||
|
|
||
| def test_depth_one(): | ||
| """Test the depth function.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| assert a.depth() == 1 | ||
|
|
||
|
|
||
| def test_depth_many(): | ||
| """Test the depth function.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| a.insert(10) | ||
| a.insert(3) | ||
| a.insert(14) | ||
| a.insert(13) | ||
| a.insert(1) | ||
| a.insert(6) | ||
| a.insert(7) | ||
| a.insert(4) | ||
| assert a.depth() == 4 | ||
|
|
||
|
|
||
| def test_balance(): | ||
| """Test the balance function.""" | ||
| a = BinarySearchTree() | ||
| a.insert(8) | ||
| a.insert(10) | ||
| a.insert(3) | ||
| a.insert(14) | ||
| a.insert(13) | ||
| a.insert(1) | ||
| a.insert(6) | ||
| a.insert(7) | ||
| assert a.balance() == 0 | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vertex...it's different. Most people use current (curr).