File tree 2 files changed +11
-2
lines changed
2 files changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,15 @@ def height(value, node = find(value))
111
111
[ left_height , right_height ] . max + 1
112
112
end
113
113
114
+ def depth ( value , current_node = @root , result = 0 )
115
+ return nil if find ( value ) . nil?
116
+ return result if value == current_node . data
117
+
118
+ return depth ( value , current_node . left , result + 1 ) if value < current_node . data
119
+
120
+ depth ( value , current_node . right , result + 1 ) if value > current_node . data
121
+ end
122
+
114
123
def balanced?
115
124
simulated_rebalance = rebalance ( change_root : false )
116
125
return true if simulated_rebalance == @root
Original file line number Diff line number Diff line change 26
26
puts '--- #postorder ---'
27
27
p tree . postorder # returns all nodes in postorder
28
28
puts '--- #height ---'
29
- p tree . height ( 5 ) # returns 3, the amount of nodes away it is from the lowest leaf
29
+ p tree . height ( 5 ) # returns 3, the amount of nodes it is from the lowest leaf ("height")
30
30
puts '--- #depth ---'
31
-
31
+ p tree . depth ( 7 ) # returns 3, the amount of nodes it is from the root node ("depth")
32
32
puts '--- #balanced? ---'
33
33
p tree . balanced? # returns true
34
34
puts '--- #rebalance ---'
You can’t perform that action at this time.
0 commit comments