-
Notifications
You must be signed in to change notification settings - Fork 0
Bst traverse #12
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
jefimenko
wants to merge
22
commits into
bst
Choose a base branch
from
bst-traverse
base: bst
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 traverse #12
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b524234
Merge.
2513a16
Merged.
996e65e
I understand what I'm getting from a call on in_order() is a generato…
b580d92
Fixed generator and added test.
971d5e5
Changed in_order() to reflect nature of the recursive call only yield…
9e87d91
add breadth first, post_order, and pre_order generators to bst.py and…
henry808 6271642
Merged.
c36afbc
Fixed unicode decode error.
312b764
Added .travis file for this branch.
2d88e5a
Added requirements.txt.
37c5e60
Updated .yml file.
7691f13
updated README.md
henry808 84d1be1
Merge branch 'bst-traverse' of github.com:jefimenko/data-structures i…
henry808 fd16ce8
Updated to run all tests.
0ff9c3e
Merge branch 'bst-traverse' of https://github.com/jefimenko/data-stru…
80f6369
Changed generators.
5e7f867
added helper functions to bst.py to make functions more clear
henry808 37d5807
changes to try to fix in order, pre order, and post order traversals
henry808 aeaba6e
tests now look at all values in expected case
henry808 a8c6f85
added a deque to breadth first traverse and fixed in, pre, and post o…
henry808 7ce920e
Added reference from children to parents.
dbb4e47
reverted to older commit because lost changes
henry808 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| language: python | ||
| python: | ||
| - "2.7" | ||
| install: "pip install -r requirements.txt" | ||
| script: py.test |
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
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,6 +1,7 @@ | ||
| #!/usr/bin/env python | ||
| import random | ||
| import subprocess | ||
| from collections import deque | ||
|
|
||
|
|
||
| class Bst(object): | ||
|
|
@@ -17,6 +18,14 @@ def __init__(self, value=None): | |
| self._size += 1 | ||
| self._depth += 1 | ||
|
|
||
| def left(self, current): | ||
| return self.tree[current].get('left') | ||
|
|
||
|
|
||
| def right(self, current): | ||
| return self.tree[current].get('right') | ||
|
|
||
|
|
||
| def insert(self, value): | ||
| """Insert a node with value in order. | ||
|
|
||
|
|
@@ -34,10 +43,10 @@ def insert(self, value): | |
| if current == value: | ||
| return | ||
| if current < value: | ||
| traverse = self.tree[current].get('right') | ||
| traverse = self.right(current) | ||
| child = 'right' | ||
| else: | ||
| traverse = self.tree[current].get('left') | ||
| traverse = self.left(current) | ||
| child = 'left' | ||
| if traverse is None: | ||
| #actual insert | ||
|
|
@@ -49,7 +58,6 @@ def insert(self, value): | |
| return | ||
| current = traverse | ||
|
|
||
|
|
||
| def balance(self): | ||
| """Returns the balance of the tree: | ||
|
|
||
|
|
@@ -91,7 +99,6 @@ def get_dot(self): | |
| ) | ||
| )) | ||
|
|
||
|
|
||
| def _get_dot(self, current): | ||
| """recursively prepare a dot graph entry for this node.""" | ||
| left = self.tree[current].get('left') | ||
|
|
@@ -113,14 +120,65 @@ def _get_dot(self, current): | |
| yield "\tnull%s [shape=point];" % r | ||
| yield "\t%s -> null%s;" % (current, r) | ||
|
|
||
| def in_order(self, current='start'): | ||
| """ | ||
| Generator that traverses the binary tree in order. | ||
| """ | ||
| if current == 'start': | ||
| current = self.top | ||
|
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. What if your BST is containing strings, like perhaps one might if you were building a dictionary for spell-checking or something. What if the word |
||
| if current is not None: | ||
| for node in self.in_order(self.left(current)): | ||
| yield node | ||
| yield current | ||
| for node in self.in_order(self.right(current)): | ||
| yield node | ||
|
|
||
|
|
||
| def pre_order(self, current='dutch'): | ||
| """Generator that traverses the binary tree pre order.""" | ||
| if current == 'dutch': | ||
| current = self.top | ||
| if current is not None: | ||
| yield current | ||
| for node in self.pre_order(self.left(current)): | ||
| yield node | ||
| for node in self.pre_order(self.right(current)): | ||
| yield node | ||
|
|
||
| def post_order(self, current='dutch'): | ||
| """Generator that traverses the binary tree post order.""" | ||
| if current == 'dutch': | ||
| current = self.top | ||
| if current is not None: | ||
| for node in self.post_order(self.left(current)): | ||
| yield node | ||
| for node in self.post_order(self.right(current)): | ||
| yield node | ||
| yield current | ||
|
|
||
| def breadth_first(self): | ||
| """Generator that traverses the binary tree in breadth first order.""" | ||
| q1 = deque() | ||
| q1.appendleft(self.top) | ||
| current = self.top | ||
| while q1: | ||
| current = q1.pop() | ||
| if self.left(current) is not None: | ||
| q1.appendleft(self.left(current)) | ||
| if self.right(current) is not None: | ||
| q1.appendleft(self.right(current)) | ||
| yield current | ||
|
|
||
|
|
||
| def main(): | ||
| """Best case and worst case are the same.""" | ||
| tree = Bst() | ||
| for num in reversed(range(10)): | ||
| tree.insert(num) | ||
| for num in range(10, 15): | ||
| tree.insert(num) | ||
| inserts = [7, 4, 11, 2, 9, 6, 12, 5, 13, 0, 10, 8, 3, 1] | ||
| for i in inserts: | ||
| tree.insert(i) | ||
| print tree.tree | ||
| # for num in enumerate(tree.pre_order()): | ||
| # print num | ||
| dot_graph = tree.get_dot() | ||
| t = subprocess.Popen(["dot", "-Tpng"], stdin=subprocess.PIPE) | ||
| t.communicate(dot_graph) | ||
|
|
||
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
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,2 @@ | ||
| py==1.4.26 | ||
| pytest==2.6.4 |
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
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
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.
These methods will be helpful!