Class: BST

Inherits:
Object
  • Object
show all
Defined in:
lib/bst.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comparator) ⇒ BST

Returns a new instance of BST.



7
8
9
10
# File 'lib/bst.rb', line 7

def initialize(comparator)
	self.comparator = comparator
	self.head = nil
end

Instance Attribute Details

#comparatorObject

Returns the value of attribute comparator.



5
6
7
# File 'lib/bst.rb', line 5

def comparator
  @comparator
end

#headObject

Returns the value of attribute head.



4
5
6
# File 'lib/bst.rb', line 4

def head
  @head
end

Instance Method Details

#infix(runner = nil, parent = nil, content_array = []) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bst.rb', line 46

def infix(runner = nil, parent = nil, content_array = [])
	runner = self.head if runner.nil? && parent.nil?
	if runner.nil? && parent.nil?
		return content_array
	elsif runner.nil? && !parent.nil?
		content_array += []
	else
		content_array = infix(runner.left_node, runner, content_array)
		content_array += [runner.content]
		content_array = infix(runner.right_node, runner, content_array)
	end
	content_array
end

#postfix(runner = nil, parent = nil, content_array = []) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bst.rb', line 60

def postfix(runner = nil, parent = nil, content_array = [])
	runner = self.head if runner.nil? && parent.nil?
	if runner.nil? && parent.nil?
		return content_array
	elsif runner.nil? && !parent.nil?
		content_array += []
	else
		content_array = postfix(runner.left_node, runner, content_array)
		content_array = postfix(runner.right_node, runner, content_array)
		content_array += [runner.content]
	end
	content_array
end

#prefix(runner = nil, parent = nil, content_array = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bst.rb', line 32

def prefix(runner = nil, parent = nil, content_array = [])
	runner = self.head if runner.nil? && parent.nil?
	if runner.nil? && parent.nil?
		return content_array
	elsif runner.nil? && !parent.nil?
		content_array += []
	else
		content_array += [runner.content]
		content_array = prefix(runner.left_node, runner, content_array)
		content_array = prefix(runner.right_node, runner, content_array)
	end
	content_array
end

#push(new_object, is_right = 1, parent = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bst.rb', line 12

def push(new_object,is_right=1,parent=nil)
	new_node = Node.new(new_object)
	if parent.nil?
		runner = self.head
	elsif is_right == 1
		runner = parent.right_node
	else
		runner = parent.left_node
	end
	if runner.nil? && parent.nil?
		self.head = new_node
	elsif runner.nil? && !parent.nil?
		parent.right_node = new_node if is_right == 1
		parent.left_node = new_node if is_right == -1
	else
		result = self.comparator.call runner.content, new_node.content
		self.push(new_object,result,runner)
	end
end

#remove_headObject



74
75
76
# File 'lib/bst.rb', line 74

def remove_head
	
end