Skip to content
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

Faiza A. #17

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
working on inorder
  • Loading branch information
Faiza1987 committed Sep 2, 2019
commit 75492e819b6e0ed6984f8746d27b5fb4e20fb572
43 changes: 39 additions & 4 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -33,11 +33,15 @@ def add(key, value)
return
end

# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def find(key)
current = @root

if current == nil
return nil
end

while (current != nil)
if current.key == key
return current.value
@@ -55,9 +59,40 @@ def find(key)
# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
current = @root
array = []

if current == nil
return
end

if array.empty?
return []
end

while (current != nil)
if (current.left == nil)
pre = current.right
else
array.push({:key => current.left.key, :value => current.left.value})
end

if (current.right == nil)
pre = current.right
else
array.push({:key => current.right.key, :value => current.right.value})
end
end
end

# def inorder_helper(current)
# if current == nil
# return
# end


# end

# Time Complexity:
# Space Complexity:
def preorder
@@ -87,4 +122,4 @@ def bfs
def to_s
return "#{self.inorder}"
end
end

2 changes: 1 addition & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
expect(tree.find(50)).must_be_nil
end

xdescribe "inorder" do
describe "inorder" do
it "will give an empty array for an empty tree" do
expect(tree.inorder).must_equal []
end