From 4a9405e1b06eaabd243ceffb4e4e16a1b291e879 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Tue, 20 Jun 2017 02:31:03 -0300 Subject: [PATCH 1/8] Add Binary Search Tree in data_structures --- .gitignore | 1 + .../Binary Tree/binary_seach_tree.py | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 data_structures/Binary Tree/binary_seach_tree.py diff --git a/.gitignore b/.gitignore index 4f6813f97..96422cb2e 100644 --- a/.gitignore +++ b/.gitignore @@ -88,3 +88,4 @@ ENV/ # Rope project settings .ropeproject .idea +.DS_Store \ No newline at end of file diff --git a/data_structures/Binary Tree/binary_seach_tree.py b/data_structures/Binary Tree/binary_seach_tree.py new file mode 100644 index 000000000..4ebb4466f --- /dev/null +++ b/data_structures/Binary Tree/binary_seach_tree.py @@ -0,0 +1,101 @@ +''' +A binary search Tree +''' +class Node: + + def __init__(self, label): + self.label = label + self.left = None + self.rigt = None + + def getLabel(self): + return self.label + + def setLabel(self, label): + self.label = label + + def getLeft(self): + return self.left + + def setLeft(self, left): + self.left = left + + def getRight(self): + return self.rigt + + def setRight(self, right): + self.rigt = right + + +class BinarySearchTree: + + def __init__(self): + self.root = None + + def insert(self, label): + + #Create a new Node + + node = Node(label) + + if self.empty(): + self.root = node + else: + dad_node = None + curr_node = self.root + + while True: + if curr_node != None: + + dad_node = curr_node + + if node.getLabel() < curr_node.getLabel(): + curr_node = curr_node.getLeft() + else: + curr_node = curr_node.getRight() + else: + if node.getLabel() < dad_node.getLabel(): + dad_node.setLeft(node) + else: + dad_node.setRight(node) + break + + def empty(self): + if self.root == None: + return True + return False + + def preShow(self, curr_node): + if curr_node != None: + print(curr_node.getLabel()) + print ('\n') + self.preShow(curr_node.getLeft()) + self.preShow(curr_node.getRight()) + + def getRoot(self): + return self.root + + +''' +Example + 8 + / \ + 3 10 + / \ \ + 1 6 14 + / \ / + 4 7 13 +''' + +t = BinarySearchTree() +t.insert(8) +t.insert(3) +t.insert(1) +t.insert(6) +t.insert(4) +t.insert(7) +t.insert(10) +t.insert(14) +t.insert(13) + +t.preShow(t.getRoot()) \ No newline at end of file From 8c9d9498b5beeb12f6c68f0eae4b8f8cc5889f67 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Tue, 20 Jun 2017 04:17:20 -0300 Subject: [PATCH 2/8] print --- data_structures/Binary Tree/binary_seach_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/Binary Tree/binary_seach_tree.py b/data_structures/Binary Tree/binary_seach_tree.py index 4ebb4466f..8e708e660 100644 --- a/data_structures/Binary Tree/binary_seach_tree.py +++ b/data_structures/Binary Tree/binary_seach_tree.py @@ -67,8 +67,8 @@ class BinarySearchTree: def preShow(self, curr_node): if curr_node != None: - print(curr_node.getLabel()) - print ('\n') + print(curr_node.getLabel(), end=" ") + self.preShow(curr_node.getLeft()) self.preShow(curr_node.getRight()) From da414d89d953ba91af032ef8a3f3fda627201d70 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Tue, 20 Jun 2017 04:43:00 -0300 Subject: [PATCH 3/8] fixed error --- .../Binary Tree/binary_seach_tree.py | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/data_structures/Binary Tree/binary_seach_tree.py b/data_structures/Binary Tree/binary_seach_tree.py index 8e708e660..81532ee22 100644 --- a/data_structures/Binary Tree/binary_seach_tree.py +++ b/data_structures/Binary Tree/binary_seach_tree.py @@ -75,27 +75,3 @@ class BinarySearchTree: def getRoot(self): return self.root - -''' -Example - 8 - / \ - 3 10 - / \ \ - 1 6 14 - / \ / - 4 7 13 -''' - -t = BinarySearchTree() -t.insert(8) -t.insert(3) -t.insert(1) -t.insert(6) -t.insert(4) -t.insert(7) -t.insert(10) -t.insert(14) -t.insert(13) - -t.preShow(t.getRoot()) \ No newline at end of file From 7aa8893da96a162075c509e63baf3f6d1f9da0f7 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Tue, 20 Jun 2017 11:58:45 -0300 Subject: [PATCH 4/8] Update .travis.yml --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3e45c05a1..1a31a2af4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,4 +12,5 @@ install: - if [ "$TRAVIS_PYTHON_VERSION" == "3.2" ]; then travis_retry pip install coverage==3.7.1; fi - if [ "$TRAVIS_PYTHON_VERSION" != "3.2" ]; then travis_retry pip install coverage; fi - "pip install pytest pytest-cov" -script: py.test --doctest-modules --cov ./ \ No newline at end of file +script: py.test --doctest-modules --cov ./ +os: osx From 6998aa0875e1b53e192699c8766da47d11b1d78f Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Tue, 20 Jun 2017 12:12:10 -0300 Subject: [PATCH 5/8] Update .travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1a31a2af4..f9950fa5c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,4 +13,3 @@ install: - if [ "$TRAVIS_PYTHON_VERSION" != "3.2" ]; then travis_retry pip install coverage; fi - "pip install pytest pytest-cov" script: py.test --doctest-modules --cov ./ -os: osx From 194bd43bffbe2acd906c06d944ad80f58ab7d24e Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Tue, 20 Jun 2017 12:13:10 -0300 Subject: [PATCH 6/8] Update .travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f9950fa5c..812fdadd4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ python: - "3.5" - "3.6" - "3.6-dev" - - "3.7-dev" - "nightly" install: - if [ "$TRAVIS_PYTHON_VERSION" == "3.2" ]; then travis_retry pip install coverage==3.7.1; fi From 0e02818cc2b232c957447ab4ed4b0aab72a94cb6 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Tue, 20 Jun 2017 22:00:14 -0300 Subject: [PATCH 7/8] Update .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 812fdadd4..f9950fa5c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ python: - "3.5" - "3.6" - "3.6-dev" + - "3.7-dev" - "nightly" install: - if [ "$TRAVIS_PYTHON_VERSION" == "3.2" ]; then travis_retry pip install coverage==3.7.1; fi From 1ce58efe1fdd747b53f1104db1a6a1700684ff17 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Wed, 21 Jun 2017 00:44:26 -0300 Subject: [PATCH 8/8] With example --- .../Binary Tree/binary_seach_tree.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/data_structures/Binary Tree/binary_seach_tree.py b/data_structures/Binary Tree/binary_seach_tree.py index 81532ee22..b6aac2990 100644 --- a/data_structures/Binary Tree/binary_seach_tree.py +++ b/data_structures/Binary Tree/binary_seach_tree.py @@ -75,3 +75,27 @@ class BinarySearchTree: def getRoot(self): return self.root + +''' +Example + 8 + / \ + 3 10 + / \ \ + 1 6 14 + / \ / + 4 7 13 +''' + +t = BinarySearchTree() +t.insert(8) +t.insert(3) +t.insert(1) +t.insert(6) +t.insert(4) +t.insert(7) +t.insert(10) +t.insert(14) +t.insert(13) + +t.preShow(t.getRoot())