From 1d1a5e93c9fd37fe2e7bbe4ecfe795d5335cedfe Mon Sep 17 00:00:00 2001 From: Ted Callahan Date: Mon, 16 Jan 2017 12:44:25 -0800 Subject: [PATCH 01/23] 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/23] 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/23] 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/23] 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/23] 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/23] 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/23] 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/23] 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/23] 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/23] 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 c86b4f27db9c70fddad376aaae589063f16bf0d5 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Tue, 17 Jan 2017 13:40:29 -0800 Subject: [PATCH 11/23] bst class can take an iter, built test fixture to fill bst. --- src/bst.py | 14 +++++++++++++- src/test_bst.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/bst.py b/src/bst.py index c4b1949..c8f6246 100644 --- a/src/bst.py +++ b/src/bst.py @@ -20,12 +20,24 @@ class BinarySearchTree(object): """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.""" + """in_order(self): will return a generator that will return the values in the tree using in-order traversal, one at a time.""" + """pre_order(self): will return a generator that will return the values in the tree using pre-order traversal, one at a time.""" + """post_order(self): will return a generator that will return the values in the tree using post_order traversal, one at a time.""" + """breadth_first(self): will return a generator that will return the values in the tree using breadth-first traversal, one at a time.""" - 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.push(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..99873c9 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -1,6 +1,16 @@ """Test Module for Binary Search Tree.""" from bst import BinarySearchTree +BST_SIMPLE = [8, 10, 3, 14, 13, 1, 6, 7, 4] + + +@pytest.fixture +def filled_bst(): + """Fixture to fill the bst tree with nodes.""" + from bst import BinarySearchTree + new_tree = BinarySearchTree(BST_SIMPLE) + return new_tree + def test_insert_5_is_root(): """Test the insert function.""" @@ -164,3 +174,8 @@ def test_search_none(): # a.insert(6) # a.insert(7) # assert a.balance == -1 + + +def test_in_order_traversal(): + """Returns a generator that returns in-order traversal results.""" + \ No newline at end of file From 584ed8e1cd4c83b08f8fa1317a882503520aa2eb Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Tue, 17 Jan 2017 16:38:41 -0800 Subject: [PATCH 12/23] added in_order function, refactor one test. --- src/bst.py | 16 +++++++++++++++- src/test_bst.py | 7 ++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/bst.py b/src/bst.py index c8f6246..e283e9c 100644 --- a/src/bst.py +++ b/src/bst.py @@ -36,7 +36,7 @@ def __init__(self, if_iter=None): for value in if_iter: self.insert(value) except TypeError: - self.push(if_iter) + self.insert(if_iter) def insert(self, val): @@ -120,3 +120,17 @@ def balance(self): if self.root is None: return 0 return self.calc_depth(self.root.right) - self.calc_depth(self.root.left) + + def in_order(self): + """Traverse in_order, yielding via generator.""" + vertex = self.root + visited = [] + while (not any(visited) or vertex is not None): + if vertex is not None: + visited.append(vertex) + vertex = vertex.left + else: + vertex = visited.pop() + yield vertex.value + vertex = vertex.right + diff --git a/src/test_bst.py b/src/test_bst.py index 99873c9..27c0a08 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -1,5 +1,6 @@ """Test Module for Binary Search Tree.""" from bst import BinarySearchTree +import pytest BST_SIMPLE = [8, 10, 3, 14, 13, 1, 6, 7, 4] @@ -176,6 +177,6 @@ def test_search_none(): # assert a.balance == -1 -def test_in_order_traversal(): - """Returns a generator that returns in-order traversal results.""" - \ No newline at end of file +def test_in_order_traversal_first_node_traversed_is_1(filled_bst): + """In-order traversal will get """ + assert next(filled_bst.in_order()) == 1 From 97c15734c3fe4c8d3ec41c8f062e0621137bb997 Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Tue, 17 Jan 2017 16:39:13 -0800 Subject: [PATCH 13/23] recursive function. --- src/bst.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/src/bst.py b/src/bst.py index c8f6246..d8791a6 100644 --- a/src/bst.py +++ b/src/bst.py @@ -25,7 +25,6 @@ class BinarySearchTree(object): """post_order(self): will return a generator that will return the values in the tree using post_order traversal, one at a time.""" """breadth_first(self): will return a generator that will return the values in the tree using breadth-first traversal, one at a time.""" - def __init__(self, if_iter=None): """Init of the Binary Search Tree class.""" self.root = None @@ -37,7 +36,8 @@ def __init__(self, if_iter=None): self.insert(value) except TypeError: self.push(if_iter) - + self._in_order = self.in_order_trav(self.root) + self.collect = [] def insert(self, val): """Take a value, inserts into Binary Search Tree at correct placement.""" @@ -120,3 +120,73 @@ def balance(self): if self.root is None: return 0 return self.calc_depth(self.root.right) - self.calc_depth(self.root.left) + + def in_order(self): + """Return.""" + return next(self._in_order) + + def recursive(self, node): + """Return.""" + print('before t.l', node.left, node.left.value) + if node.left: + print('in t.l') + yield next(self.recursive(node.left)) + self.recursive(node.left) + print('after rec') + print('hey', node.left, node.value) + self.collect.append(node.value) + print('before yield', node.value) + yield node.value + print('after yield', node.right) + if node.right: + self.recursive(node.right) + # print(node.value) + + def pre_order(self): + """Return.""" + num = 0 + while num < 10: + yield num + num += 1 + + def post_order(self): + """Return.""" + num = 0 + while num < 10: + yield num + num += 1 + + def breadth_first(self): + """Return.""" + num = 0 + while num < 10: + yield num + num += 1 + + def in_order_trav(self, tree): + """Return.""" + import pdb; pdb.set_trace() + if tree is None: + return + if tree.left: + for each in self.in_order_trav(tree.left): + yield self.in_order_trav(tree.left) + yield tree.value + if tree.right: + for each in self.in_order_trav(tree.right): + yield tree.value + + # for i in self.in_order_trav(tree.left): + # if tree.left is not None: + # yield tree.left.value + # yield None + # if tree.left: + # for i in self.in_order_trav(tree.left): + # yield tree.value + # yield tree.value + # if tree is None: + # yield + # num = 0 + # while num < 10: + # yield num + # num += 1 From aba9fa0db2526503773eebe6a5171f3bd4df7da5 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Tue, 17 Jan 2017 16:55:03 -0800 Subject: [PATCH 14/23] tests --- src/bst.py | 3 +-- src/test_bst.py | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/bst.py b/src/bst.py index e283e9c..5489247 100644 --- a/src/bst.py +++ b/src/bst.py @@ -125,7 +125,7 @@ def in_order(self): """Traverse in_order, yielding via generator.""" vertex = self.root visited = [] - while (not any(visited) or vertex is not None): + while visited if vertex is not None: visited.append(vertex) vertex = vertex.left @@ -133,4 +133,3 @@ def in_order(self): vertex = visited.pop() yield vertex.value vertex = vertex.right - diff --git a/src/test_bst.py b/src/test_bst.py index 27c0a08..a83ea05 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -179,4 +179,7 @@ def test_search_none(): def test_in_order_traversal_first_node_traversed_is_1(filled_bst): """In-order traversal will get """ - assert next(filled_bst.in_order()) == 1 + in_order_list = [] + for x in filled_bst.in_order(): + in_order_list.append(x) + assert in_order_list[0] == 1 From 01174068a414e5882329919471cf62084b63b7b4 Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Wed, 18 Jan 2017 09:47:02 -0800 Subject: [PATCH 15/23] pre order traversal, beginning post order traversal. --- src/bst.py | 35 ++++++++++++++++++++ src/test_bst.py | 85 ++++++++++++++++++++++++++----------------------- 2 files changed, 80 insertions(+), 40 deletions(-) diff --git a/src/bst.py b/src/bst.py index 1241abc..0a8a6f0 100644 --- a/src/bst.py +++ b/src/bst.py @@ -37,6 +37,8 @@ def __init__(self, if_iter=None): except TypeError: self.insert(if_iter) self._in_order = self.in_order_trav() + self._pre_order = self.pre_order_trav() + self._post_order = self.post_order_trav() def insert(self, val): """Take a value, inserts into Binary Search Tree at correct placement.""" @@ -137,6 +139,39 @@ def in_order_trav(self): yield vertex.value vertex = vertex.right + def pre_order(self): + """Return.""" + return next(self._pre_order) + + def pre_order_trav(self): + """Traverse pre_order, yielding via generator.""" + vertex = self.root + visited = [] + while (visited or vertex is not None): + if vertex is not None: + yield vertex.value + visited.append(vertex) + vertex = vertex.left + else: + vertex = visited.pop() + vertex = vertex.right + + def post_order(self): + """Return.""" + return next(self._post_order) + + def post_order_trav(self): + """Traverse pre_order, yielding via generator.""" + vertex = self.root + visited = [] + while (visited or vertex is not None): + if vertex is not None: + visited.append(vertex) + vertex = vertex.left + else: + vertex = visited.pop() + vertex = vertex.right + # """Return.""" diff --git a/src/test_bst.py b/src/test_bst.py index fb8ef39..3f48a1f 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -135,48 +135,53 @@ 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 def test_in_order_traversal_first_node_traversed_is_1(filled_bst): """In-order traversal will get """ assert filled_bst.in_order() == 1 + + +def test_pre_order_traversal_first_node_traversed_is_1(filled_bst): + """Pre-order traversal will get """ + assert filled_bst.pre_order() == 8 From dde70aeef8509a4e6ba9ccfc5b96e8e9bbc60f7a Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Wed, 18 Jan 2017 14:07:44 -0800 Subject: [PATCH 16/23] commit to pull post_order trav. --- src/bst.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bst.py b/src/bst.py index 0a8a6f0..5962f05 100644 --- a/src/bst.py +++ b/src/bst.py @@ -173,6 +173,7 @@ def post_order_trav(self): vertex = vertex.right + # """Return.""" # def recursive(self, node): From f7f2aba6ccaba43973d2f04042d92da956983ab9 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Wed, 18 Jan 2017 14:09:25 -0800 Subject: [PATCH 17/23] trying again, post order. --- src/bst.py | 11 +++++++++-- src/test_bst.py | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/bst.py b/src/bst.py index e27a1bd..f67ab7d 100644 --- a/src/bst.py +++ b/src/bst.py @@ -39,6 +39,7 @@ def __init__(self, if_iter=None): self._in_order = self.in_order_trav() self._pre_order = self.pre_order_trav() self._post_order = self.post_order_trav() + self._breadth_first = self.breadth_first_trav() def insert(self, val): """Take a value, inserts into Binary Search Tree at correct placement.""" @@ -175,11 +176,17 @@ def post_order_trav(self): peek_vertex = visited[-1] if peek_vertex.right and peek_vertex.right is not last_vertex: vertex = peek_vertex.right - yield vertex.value else: - visited.append(peek_vertex) + yield peek_vertex.value last_vertex = visited.pop() + def breadth_first(self): + """Fill in later.""" + return next(self._breadth_first) + + def breadth_first_trav(self): + """Traverse breadth first order, yielding a generator.""" + # """Return.""" diff --git a/src/test_bst.py b/src/test_bst.py index 5de76a7..7097d5b 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -193,3 +193,7 @@ def test_in_order_traversal_first_node_traversed_is_1(filled_bst): def test_pre_order_traversal_first_node_traversed_is_1(filled_bst): """Pre-order traversal will get """ assert filled_bst.pre_order() == 8 + +def test_post_order_traversal(filled_bst): + """Post-order traversal.""" + \ No newline at end of file From ba81221b738ae47abf7725bfbe1f741772dac813 Mon Sep 17 00:00:00 2001 From: William Benjamin Shields Date: Wed, 18 Jan 2017 14:10:24 -0800 Subject: [PATCH 18/23] added one space. --- src/bst.py | 70 ------------------------------------------------------ 1 file changed, 70 deletions(-) diff --git a/src/bst.py b/src/bst.py index 0ba40db..b702153 100644 --- a/src/bst.py +++ b/src/bst.py @@ -179,73 +179,3 @@ def post_order_trav(self): else: visited.append(peek_vertex) last_vertex = visited.pop() - - - -# """Return.""" - -# def recursive(self, node): -# """Return.""" -# print('before t.l', node.left, node.left.value) -# if node.left: -# print('in t.l') -# yield next(self.recursive(node.left)) -# self.recursive(node.left) -# print('after rec') -# print('hey', node.left, node.value) -# self.collect.append(node.value) -# print('before yield', node.value) -# yield node.value -# print('after yield', node.right) -# if node.right: -# self.recursive(node.right) -# # print(node.value) - -# def pre_order(self): -# """Return.""" -# num = 0 -# while num < 10: -# yield num -# num += 1 - -# def post_order(self): -# """Return.""" -# num = 0 -# while num < 10: -# yield num -# num += 1 - -# def breadth_first(self): -# """Return.""" -# num = 0 -# while num < 10: -# yield num -# num += 1 - -# def in_order_trav(self, tree): -# """Return.""" -# import pdb; pdb.set_trace() -# if tree is None: -# return -# if tree.left: -# for each in self.in_order_trav(tree.left): -# yield self.in_order_trav(tree.left) -# yield tree.value -# if tree.right: -# for each in self.in_order_trav(tree.right): -# yield tree.value - -# # for i in self.in_order_trav(tree.left): -# # if tree.left is not None: -# # yield tree.left.value -# # yield None -# # if tree.left: -# # for i in self.in_order_trav(tree.left): -# # yield tree.value -# # yield tree.value -# # if tree is None: -# # yield -# # num = 0 -# # while num < 10: -# # yield num -# # num += 1 From b52b36f4a1e7fa95438004af434675951c82e455 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Wed, 18 Jan 2017 17:11:25 -0800 Subject: [PATCH 19/23] bst work. --- setup.py | 2 +- src/bst.py | 8 -------- src/test_bst.py | 1 - 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 70142e3..e35bd4b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ name="data-structures", description="this module will be expanded as we write more data-structures", version=1.1, - author="Ted Callahan and Colin Lamont", + author="Ben Shields and Colin Lamont, initially forked from Ted Callahan's repo, which he worked on with Colin Lamont.", author_email="", license="MIT", package_dir={'': 'src'}, diff --git a/src/bst.py b/src/bst.py index f67ab7d..d41d80d 100644 --- a/src/bst.py +++ b/src/bst.py @@ -39,7 +39,6 @@ def __init__(self, if_iter=None): self._in_order = self.in_order_trav() self._pre_order = self.pre_order_trav() self._post_order = self.post_order_trav() - self._breadth_first = self.breadth_first_trav() def insert(self, val): """Take a value, inserts into Binary Search Tree at correct placement.""" @@ -180,13 +179,6 @@ def post_order_trav(self): yield peek_vertex.value last_vertex = visited.pop() - def breadth_first(self): - """Fill in later.""" - return next(self._breadth_first) - - def breadth_first_trav(self): - """Traverse breadth first order, yielding a generator.""" - # """Return.""" diff --git a/src/test_bst.py b/src/test_bst.py index 7097d5b..979538e 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -196,4 +196,3 @@ def test_pre_order_traversal_first_node_traversed_is_1(filled_bst): def test_post_order_traversal(filled_bst): """Post-order traversal.""" - \ No newline at end of file From e689faeb79a74367093c09b845f341894c5da080 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Thu, 19 Jan 2017 09:06:31 -0800 Subject: [PATCH 20/23] older code 2. --- src/bst.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bst.py b/src/bst.py index f4a051b..b53e2aa 100644 --- a/src/bst.py +++ b/src/bst.py @@ -181,7 +181,6 @@ def post_order_trav(self): yield peek_vertex.value last_vertex = visited.pop() -<<<<<<< HEAD # """Return.""" From 68ff199d80963816cb4a56b1b1f4751199db75f9 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Sun, 22 Jan 2017 23:52:35 -0800 Subject: [PATCH 21/23] tried to fix generator test. Added readme coverage stats. --- src/bst.py | 69 ------------------------------------------------- src/test_bst.py | 14 +++++----- 2 files changed, 8 insertions(+), 75 deletions(-) diff --git a/src/bst.py b/src/bst.py index b53e2aa..166ef7e 100644 --- a/src/bst.py +++ b/src/bst.py @@ -181,75 +181,6 @@ def post_order_trav(self): yield peek_vertex.value last_vertex = visited.pop() - -# """Return.""" - -# def recursive(self, node): -# """Return.""" -# print('before t.l', node.left, node.left.value) -# if node.left: -# print('in t.l') -# yield next(self.recursive(node.left)) -# self.recursive(node.left) -# print('after rec') -# print('hey', node.left, node.value) -# self.collect.append(node.value) -# print('before yield', node.value) -# yield node.value -# print('after yield', node.right) -# if node.right: -# self.recursive(node.right) -# # print(node.value) - -# def pre_order(self): -# """Return.""" -# num = 0 -# while num < 10: -# yield num -# num += 1 - -# def post_order(self): -# """Return.""" -# num = 0 -# while num < 10: -# yield num -# num += 1 - -# def breadth_first(self): -# """Return.""" -# num = 0 -# while num < 10: -# yield num -# num += 1 - -# def in_order_trav(self, tree): -# """Return.""" -# import pdb; pdb.set_trace() -# if tree is None: -# return -# if tree.left: -# for each in self.in_order_trav(tree.left): -# yield self.in_order_trav(tree.left) -# yield tree.value -# if tree.right: -# for each in self.in_order_trav(tree.right): -# yield tree.value - -# # for i in self.in_order_trav(tree.left): -# # if tree.left is not None: -# # yield tree.left.value -# # yield None -# # if tree.left: -# # for i in self.in_order_trav(tree.left): -# # yield tree.value -# # yield tree.value -# # if tree is None: -# # yield -# # num = 0 -# # while num < 10: -# # yield num -# # num += 1 - def breadth_first(self): """Fill in later.""" return next(self._breadth_first) diff --git a/src/test_bst.py b/src/test_bst.py index 979538e..7a46d20 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -177,12 +177,12 @@ def test_balance(): assert a.balance() == 0 -def test_in_order_traversal_first_node_traversed_is_1(filled_bst): +def test_in_order_traversal_with_generator_first_node_traversed_is_1(filled_bst): """In-order traversal will start with 1. """ in_order_list = [] - for x in filled_bst.in_order(): - in_order_list.append(x) - assert in_order_list[0] == 1 + import pdb; pdb.set_trace() + [x for x in filled_bst.in_order()] + assert in_order_list == [1, 2, 3, 4, 5, 6] def test_in_order_traversal_first_node_traversed_is_1(filled_bst): @@ -190,9 +190,11 @@ def test_in_order_traversal_first_node_traversed_is_1(filled_bst): assert filled_bst.in_order() == 1 -def test_pre_order_traversal_first_node_traversed_is_1(filled_bst): - """Pre-order traversal will get """ +def test_pre_order_traversal_first_node_traversed_is_8(filled_bst): + """Pre-order traversal will get an 8 first.""" assert filled_bst.pre_order() == 8 + def test_post_order_traversal(filled_bst): """Post-order traversal.""" + assert filled_bst.post_order() == 1 From 2a1e82aa5e7150e4a1fdb99bf7c4dd737c9df925 Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Sun, 22 Jan 2017 23:53:15 -0800 Subject: [PATCH 22/23] test coverage in read me. --- README.MD | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/README.MD b/README.MD index eaae067..6d925b8 100644 --- a/README.MD +++ b/README.MD @@ -1,32 +1,19 @@ #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. - + in_order(self): will return a generator that will return the values in the tree using in-order traversal, one at a time. + pre_order(self): will return a generator that will return the values in the tree using pre-order traversal, one at a time. + post_order(self): will return a generator that will return the values in the tree using post_order traversal, one at a time. + breadth_first(self): will return a generator that will return the values in the tree using breadth-first traversal, one at a time. # Coverage: ----------- coverage: platform darwin, python 3.5.2-final-0 ----------- - - -| 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 121 36 70% 39-40, 73, 92-94, 124, 143, 156-160, 164, 168-182, 186, 190-19 +---------- coverage: platform darwin, python 3.5.2-final-0 ----------- +Name Stmts Miss Cover Missing +----------------------------------------------------------- +src/bst.py 121 36 70% 39-40, 73, 92-94, 124, 143, 156-160, 164, 168-182, 186, 190-198 \ No newline at end of file From 26d6e0972383d0c0d76a4248bba165a99015656d Mon Sep 17 00:00:00 2001 From: Colin Lamont Date: Mon, 23 Jan 2017 09:00:49 -0800 Subject: [PATCH 23/23] removed pdb. --- src/test_bst.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/test_bst.py b/src/test_bst.py index 7a46d20..2eb03de 100644 --- a/src/test_bst.py +++ b/src/test_bst.py @@ -178,11 +178,9 @@ def test_balance(): def test_in_order_traversal_with_generator_first_node_traversed_is_1(filled_bst): - """In-order traversal will start with 1. """ - in_order_list = [] - import pdb; pdb.set_trace() - [x for x in filled_bst.in_order()] - assert in_order_list == [1, 2, 3, 4, 5, 6] + """In-order traversal will fill a list in order.""" + results = [x for x in filled_bst.in_order()] + assert results == [1, 3, 4, 6, 7, 8, 10, 13, 14] def test_in_order_traversal_first_node_traversed_is_1(filled_bst):