Skip to content

Commit 56ef87d

Browse files
ClémentClément
authored andcommitted
Simplifying basics Tree implementation.
1 parent 28a6c08 commit 56ef87d

3 files changed

Lines changed: 170 additions & 300 deletions

File tree

source/code/projects/AVLTree/AVLTree/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ static void Main()
1616
Console.WriteLine(avltree1);
1717
avltree1.Remove(5);
1818
Console.WriteLine(avltree1);
19-
20-
2119
}
2220
}

source/code/projects/AVLTree_I/AVLTree/BTree.cs

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public int Height
4646
{
4747
height = right.Height + 1;
4848
}
49-
else
50-
{
51-
height = 0;
52-
}
49+
// The last case is if both children
50+
// are null, in which case the height
51+
// remains 0.
5352
return height;
5453
}
5554
}
@@ -78,54 +77,18 @@ public bool IsEmpty()
7877
}
7978

8079
// We now look at how to
81-
// compute the Depth of a tree.
82-
// Remember that the depth and height
83-
// of a tree are both the root height.
84-
public int Depth()
80+
// compute the height of a tree.
81+
// Remember that the height
82+
// of a tree is the height of
83+
// its root node.
84+
public int Height()
8585
{
86-
int depth = 0;
86+
int height = 0;
8787
if (root != null)
8888
{
89-
depth = Depth(root, 0);
90-
}
91-
return depth;
92-
}
93-
94-
private int Depth(Node nodeP, int depth)
95-
{
96-
// "Unless proven otherwise",
97-
// we assume that the depth of the
98-
// node is the depth it received
99-
// as argument.
100-
int result = depth;
101-
// We assume the depth of
102-
// its right sub-tree
103-
// is 0.
104-
int depthL = 0;
105-
if (nodeP.left != null)
106-
{
107-
// If its left sub-tree is not null,
108-
// we inquire about its depth,
109-
// knowing that it will be 1 more
110-
// than the depth of the current node.
111-
depthL = Depth(nodeP.left, result + 1);
112-
}
113-
// We proceed similarly for the
114-
// left sub-tree.
115-
int depthR = 0;
116-
if (nodeP.right != null)
117-
{
118-
depthR = Depth(nodeP.right, result + 1);
119-
}
120-
// Finally, if at least one sub-tree
121-
// is not null, we take the max of their
122-
// depths to be the depth of the tree
123-
// starting with our current node.
124-
if (nodeP.left != null || nodeP.right != null)
125-
{
126-
result = Math.Max(depthL, depthR);
89+
height = root.Height;
12790
}
128-
return result;
91+
return height;
12992
}
13093

13194
// Finding is also recursive.
@@ -246,7 +209,7 @@ private string TraversePo(Node nodeP)
246209

247210
public override string ToString()
248211
{
249-
string returned = "Tree depth: " + Depth() + "\n";
212+
string returned = "Tree height: " + Height() + "\n";
250213
int tBalance = SubtreeBalance(root);
251214
returned += "Tree balance: " + tBalance;
252215
if (tBalance <= -2)
@@ -266,12 +229,12 @@ public override string ToString()
266229
return returned;
267230
}
268231

269-
private string Stringify(Node nodeP, int depth)
232+
private string Stringify(Node nodeP, int height)
270233
{
271234
string returned = "";
272235
if (nodeP != null)
273236
{
274-
for (int i = 0; i < depth; i++)
237+
for (int i = 0; i < height; i++)
275238
{
276239
returned += " ";
277240
}
@@ -283,7 +246,7 @@ private string Stringify(Node nodeP, int depth)
283246
+ " (h: "
284247
+ nodeP.left.Height
285248
+ ")"
286-
+ Stringify(nodeP.left, depth + 1);
249+
+ Stringify(nodeP.left, height + 1);
287250
}
288251
if (nodeP.right != null)
289252
{
@@ -292,7 +255,7 @@ private string Stringify(Node nodeP, int depth)
292255
+ " (h: "
293256
+ nodeP.right.Height
294257
+ ")"
295-
+ Stringify(nodeP.right, depth + 1);
258+
+ Stringify(nodeP.right, height + 1);
296259
}
297260
}
298261
return returned;

0 commit comments

Comments
 (0)