From 1d1a5e93c9fd37fe2e7bbe4ecfe795d5335cedfe Mon Sep 17 00:00:00 2001 From: Ted Callahan Date: Mon, 16 Jan 2017 12:44:25 -0800 Subject: [PATCH 01/12] Initial build out of node and bst structures. --- src/bst.py | 32 ++++++++++++++++++++++++++++++++ src/test_bst.py | 1 + 2 files changed, 33 insertions(+) create mode 100644 src/bst.py create mode 100644 src/test_bst.py diff --git a/src/bst.py b/src/bst.py new file mode 100644 index 0000000..ba9ab44 --- /dev/null +++ b/src/bst.py @@ -0,0 +1,32 @@ +"""This module implements a binary search tree.""" + +class Node(object): + """Node object for use in a binary search tree.""" + def __init__(self): + self.key = key + self.value = value + self.right = right + self.left = left + + + + +class BinarySearchTree(object): + """Binary Search Tree Object. + + Methods: + - 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): + """Initialize a Binary Search Tree object.""" + pass + + def insert(self, val): + \ No newline at end of file diff --git a/src/test_bst.py b/src/test_bst.py new file mode 100644 index 0000000..2f32ae3 --- /dev/null +++ b/src/test_bst.py @@ -0,0 +1 @@ +"""This module tests a binary search tree.""" From 6f6a938c70d6641944b7ddadadc36cd657cddc89 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Mon, 16 Jan 2017 13:37:18 -0800 Subject: [PATCH 02/12] Added bst and test_bst files. --- src/bst.py | 1 + src/test_bst.py | 1 + 2 files changed, 2 insertions(+) create mode 100644 src/bst.py create mode 100644 src/test_bst.py diff --git a/src/bst.py b/src/bst.py new file mode 100644 index 0000000..300550c --- /dev/null +++ b/src/bst.py @@ -0,0 +1 @@ +"""Module for Binary Search Tree.""" diff --git a/src/test_bst.py b/src/test_bst.py new file mode 100644 index 0000000..4e39447 --- /dev/null +++ b/src/test_bst.py @@ -0,0 +1 @@ +"""Test Module for Binary Search Tree.""" \ No newline at end of file From 98c5039f1beb4911c0cf2a4fae5becc0c43ddd82 Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Mon, 16 Jan 2017 14:03:22 -0800 Subject: [PATCH 03/12] commiting initial insert tests. --- src/test_bst.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test_bst.py b/src/test_bst.py index 2b8ca81..d6dbe35 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -1 +1,30 @@ """Test Module for Binary Search Tree.""" + +def test_insert1(): + a = bst() + a.insert(5) + assert a[5] + +def test_insert2(): + a = bst() + a.insert(5) + a.insert(10) + assert a[5] == ["z", 10] + +def test_insert3(): + a = bst() + 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[6] == [4, 7] + assert a[3] == [1, 6] + assert a[14] == [13, "z"] + assert a[8] == [3, 10] + assert a[13] == ["z", "z"] + From 0ecf7b2390632e399030028033f7c0cccbce0789 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Mon, 16 Jan 2017 14:30:42 -0800 Subject: [PATCH 04/12] added insert method. --- src/bst.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/bst.py b/src/bst.py index 6d07b52..22babec 100644 --- a/src/bst.py +++ b/src/bst.py @@ -1,2 +1,29 @@ +"""Module for Binary Search Tree.""" -"""Module for Binary Search Tree.""" \ No newline at end of file + +class BinarySearchTree(object): + """Foo.""" + + def __init__(self): + """Init of the Binary Search Tree class.""" + self._bstdict = {} + self.root = None + self.vertex = self.root + + def insert(self, val): + """Takes a value, inserts into Binary Search Tree at correct placement.""" + if not any(self._bstdict): + self.root = val + self._bstdict[val] = ["z", "z"] + + elif val > self._bstdict[self.vertex]: + if self._bstdict[self.vertex][1] == "z": + self._bstdict[self.vertex][1] = val + self.vertex = self.vertex[1] + insert(val) + + elif val < self._bstdict[self.vertex]: + if self._bstdict[self.vertex][0] == "z": + self._bstdict[self.vertex][0] = val + self.vertex = self.vertex[0] + insert(val) From 597732fee731503a4f13c0f797a9072534411256 Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Mon, 16 Jan 2017 15:02:43 -0800 Subject: [PATCH 05/12] commit to pull remote changes. --- src/test_bst.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test_bst.py b/src/test_bst.py index d6dbe35..c118ea9 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -27,4 +27,3 @@ def test_insert3(): assert a[14] == [13, "z"] assert a[8] == [3, 10] assert a[13] == ["z", "z"] - From 5c46d3b379a2a890c667b698a4c8c138ef8b6a4d Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Mon, 16 Jan 2017 15:07:08 -0800 Subject: [PATCH 06/12] refactor of insert and tests. --- src/bst.py | 6 +++--- src/test_bst.py | 51 +++++++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/bst.py b/src/bst.py index 22babec..4f190b3 100644 --- a/src/bst.py +++ b/src/bst.py @@ -14,16 +14,16 @@ def insert(self, val): """Takes a value, inserts into Binary Search Tree at correct placement.""" if not any(self._bstdict): self.root = val - self._bstdict[val] = ["z", "z"] + self._bstdict[val] = [None, None] elif val > self._bstdict[self.vertex]: - if self._bstdict[self.vertex][1] == "z": + if self._bstdict[self.vertex][1] == None: self._bstdict[self.vertex][1] = val self.vertex = self.vertex[1] insert(val) elif val < self._bstdict[self.vertex]: - if self._bstdict[self.vertex][0] == "z": + if self._bstdict[self.vertex][0] == None: self._bstdict[self.vertex][0] = val self.vertex = self.vertex[0] insert(val) diff --git a/src/test_bst.py b/src/test_bst.py index d6dbe35..6de1791 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -1,30 +1,35 @@ """Test Module for Binary Search Tree.""" -def test_insert1(): - a = bst() - a.insert(5) - assert a[5] + +# def test_insert1(): +# from bst import BinarySearchTree +# # import pdb; pdb.set_trace() +# a = BinarySearchTree() +# a.insert(5) +# assert a._bstdict[5] def test_insert2(): - a = bst() + from bst import BinarySearchTree + a = BinarySearchTree() + import pdb; pdb.set_trace() a.insert(5) a.insert(10) - assert a[5] == ["z", 10] - -def test_insert3(): - a = bst() - 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[6] == [4, 7] - assert a[3] == [1, 6] - assert a[14] == [13, "z"] - assert a[8] == [3, 10] - assert a[13] == ["z", "z"] + assert a._bstdict[5] == [None, 10] +# def test_insert3(): +# from bst import BinarySearchTree +# 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._bstdict[6] == [4, 7] +# # assert a[3] == [1, 6] +# # assert a[14] == [13, "z"] +# # assert a[8] == [3, 10] +# # assert a[13] == ["z", "z"] From c5587dcf3f36dd1e84619593b760288a521996ff Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Mon, 16 Jan 2017 16:03:23 -0800 Subject: [PATCH 07/12] fixed initial tests and insert function, new test for size. --- src/bst.py | 64 +++++++++++++++++++++++++++++++++------------ src/test_bst.py | 69 ++++++++++++++++++++++++++++++------------------- 2 files changed, 89 insertions(+), 44 deletions(-) diff --git a/src/bst.py b/src/bst.py index 4f190b3..ae0858c 100644 --- a/src/bst.py +++ b/src/bst.py @@ -1,29 +1,59 @@ """Module for Binary Search Tree.""" +class Node(object): + + def __init__(self, value=None, left=None, right=None): + self.value = value + self.left = left + self.right = right + + class BinarySearchTree(object): """Foo.""" + """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): """Init of the Binary Search Tree class.""" - self._bstdict = {} self.root = None - self.vertex = self.root + self.counter = 0 def insert(self, val): """Takes a value, inserts into Binary Search Tree at correct placement.""" - if not any(self._bstdict): - self.root = val - self._bstdict[val] = [None, None] - - elif val > self._bstdict[self.vertex]: - if self._bstdict[self.vertex][1] == None: - self._bstdict[self.vertex][1] = val - self.vertex = self.vertex[1] - insert(val) - - elif val < self._bstdict[self.vertex]: - if self._bstdict[self.vertex][0] == None: - self._bstdict[self.vertex][0] = val - self.vertex = self.vertex[0] - insert(val) + if self.root is None: + self.root = Node(val) + self.counter += 1 + + else: + vertex = self.root + while True: + if val > vertex.value: + if vertex.right: + vertex = vertex.right + else: + vertex.right = Node(val) + self.counter += 1 + break + + elif val < vertex.value: + if vertex.left: + vertex = vertex.left + else: + vertex.left = Node(val) + self.counter += 1 + break + + + # def search(self, val): + + + def size(self): + """Returns size of Binary Search Tree.""" + diff --git a/src/test_bst.py b/src/test_bst.py index 6de1791..c2a8aea 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -1,35 +1,50 @@ """Test Module for Binary Search Tree.""" +from bst import BinarySearchTree -# def test_insert1(): -# from bst import BinarySearchTree -# # import pdb; pdb.set_trace() -# a = BinarySearchTree() -# a.insert(5) -# assert a._bstdict[5] +def test_insert_5_is_root(): + a = BinarySearchTree() + a.insert(5) + assert a.root + +def test_insert_5_where_root_equals_5(): + a = BinarySearchTree() + a.insert(5) + assert a.root.value == 5 -def test_insert2(): - from bst import BinarySearchTree +def test_insert_5_and_10_and_confirm_right(): a = BinarySearchTree() - import pdb; pdb.set_trace() a.insert(5) a.insert(10) - assert a._bstdict[5] == [None, 10] + assert a.root.right.value == 10 -# def test_insert3(): -# from bst import BinarySearchTree -# 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._bstdict[6] == [4, 7] -# # assert a[3] == [1, 6] -# # assert a[14] == [13, "z"] -# # assert a[8] == [3, 10] -# # assert a[13] == ["z", "z"] +def test_insert_many_numbers(): + 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 From 994d3cb2b86586c0fbbcf53ad88d8b5325d6435c Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Mon, 16 Jan 2017 16:03:50 -0800 Subject: [PATCH 08/12] commit to pull remote changes. --- src/bst.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bst.py b/src/bst.py index 4f190b3..8e87bf7 100644 --- a/src/bst.py +++ b/src/bst.py @@ -12,6 +12,7 @@ def __init__(self): def insert(self, val): """Takes a value, inserts into Binary Search Tree at correct placement.""" + import pdb; pdb.set_trace() if not any(self._bstdict): self.root = val self._bstdict[val] = [None, None] From afecfc5f6a3d34d7e17557c8c9ecf7cbf109ca6c Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Tue, 17 Jan 2017 09:13:50 -0800 Subject: [PATCH 09/12] else to handle if node with value already exists on insert. --- src/bst.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bst.py b/src/bst.py index d960111..826351b 100644 --- a/src/bst.py +++ b/src/bst.py @@ -52,6 +52,8 @@ def insert(self, val): self.counter += 1 self.container.append(val) break + else: + break def size(self): """Return size of Binary Search Tree.""" From f407dc325d637c0803d55ffca1e9b8cc6c26cd0f Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Tue, 17 Jan 2017 09:51:29 -0800 Subject: [PATCH 10/12] edited docstrings. --- src/bst.py | 8 ++++-- src/test_bst.py | 74 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/bst.py b/src/bst.py index 826351b..c4b1949 100644 --- a/src/bst.py +++ b/src/bst.py @@ -2,15 +2,17 @@ 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): - """Foo.""" + """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""" @@ -82,6 +84,7 @@ def search(self, val): 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. """ @@ -97,10 +100,11 @@ def calc_depth(self, tree): 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.left) - self.calc_depth(self.root.right) + return self.calc_depth(self.root.right) - self.calc_depth(self.root.left) diff --git a/src/test_bst.py b/src/test_bst.py index e950d9b..eff9834 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -2,23 +2,30 @@ from bst import BinarySearchTree -def test_insert_5_is_root(): +def test_insert_5_is_root(): + """Test the insert function.""" a = BinarySearchTree() a.insert(5) assert a.root + def test_insert_5_where_root_equals_5(): + """Test the insert funciton.""" a = BinarySearchTree() a.insert(5) assert a.root.value == 5 + def test_insert_5_and_10_and_confirm_right(): + """Test the insert function.""" 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) @@ -35,6 +42,7 @@ def test_insert_many_numbers(): 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() @@ -49,6 +57,7 @@ def test_size_returns_size_of_binary_search_tree(): 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() @@ -63,6 +72,7 @@ def test_binary_search_tree_contains_value(): a.insert(4) assert a.contains(4) + def test_binary_search_tree_does_not_contain_value(): """Test that the contains method returns True if value in binary search tree.""" a = BinarySearchTree() @@ -77,22 +87,58 @@ def test_binary_search_tree_does_not_contain_value(): 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(): + +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) @@ -103,14 +149,11 @@ def test_search_empty(): # a.insert(6) # a.insert(7) # a.insert(4) -# assert a.search(100) is None +# assert a.depth == 4 -# def test_depth_one(): -# a = BinarySearchTree() -# a.insert(8) -# assert a.depth == 1 -# def test_depth_many(): +# def test_balance(): +# """Test the balance function.""" # a = BinarySearchTree() # a.insert(8) # a.insert(10) @@ -120,17 +163,4 @@ def test_search_empty(): # a.insert(1) # a.insert(6) # a.insert(7) -# a.insert(4) -# assert a.depth == 4 - -def test_balance(): - 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 == 1 +# assert a.balance == -1 From 7842eaab069ec18ef506b88411b5d80982d37bce Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Sun, 22 Jan 2017 23:21:01 -0800 Subject: [PATCH 11/12] fixed tests, added iterator to bst. --- src/bst.py | 8 ++++- src/test_bst.py | 88 +++++++++++++++++++++++++------------------------ 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/bst.py b/src/bst.py index c4b1949..2f0f816 100644 --- a/src/bst.py +++ b/src/bst.py @@ -21,11 +21,17 @@ class BinarySearchTree(object): """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): + 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.""" diff --git a/src/test_bst.py b/src/test_bst.py index eff9834..d4c1e5b 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -3,21 +3,21 @@ def test_insert_5_is_root(): - """Test the insert function.""" + """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.""" + """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.""" + """Test the insert function where right child of root is 10.""" a = BinarySearchTree() a.insert(5) a.insert(10) @@ -124,43 +124,45 @@ def test_search_none(): 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 == -1 +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 + + From fb52cb738cb3a301907d08d99c2d93137e9329a3 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Sun, 22 Jan 2017 23:28:04 -0800 Subject: [PATCH 12/12] added test results and coverage to the readme, as well as function descriptions. --- README.MD | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/README.MD b/README.MD index eaae067..9892d99 100644 --- a/README.MD +++ b/README.MD @@ -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 \ No newline at end of file