Skip to content

Commit d0b583b

Browse files
ClémentClément
authored andcommitted
Tidying code.
1 parent 56ef87d commit d0b583b

4 files changed

Lines changed: 220 additions & 220 deletions

File tree

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

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
using System;
22
using System.Collections.Generic;
33

4-
/*
4+
/*
55
* This implementation of AVL tree is
6-
* "from scratch", and stores the height
7-
* of a node as an attribute instead of
6+
* "from scratch", and stores the height
7+
* of a node as an attribute instead of
88
* re-comptuing it when needed.
9-
* This class does not inherit from any
9+
* This class does not inherit from any
1010
* other class and is "standalone".
1111
*/
1212

1313
public class AVLTree<T>
1414
where T : IComparable<T>
15-
// We need T to realize IComparable
16-
// so that we can decide where to
17-
// insert node, exactly like BSTrees.
15+
// We need T to realize IComparable
16+
// so that we can decide where to
17+
// insert node, exactly like BSTrees.
1818
{
1919
private class Node
2020
{
2121
public T Data { get; set; }
2222
public Node left;
2323
public Node right;
24-
// Height is implemented as an attribute
25-
// with a property.
24+
25+
// Height is implemented as an attribute
26+
// with a property.
2627
private int height;
2728
public int Height
2829
{
@@ -110,37 +111,39 @@ private int UpdateHeight(Node nodeP)
110111
return height;
111112
}
112113

113-
// The following will return
114-
// a negative number if subtree is right-heavy
115-
// a positive number if subtree is left-heavy
116-
// 0 if the subtree is perfectly balanced.
114+
// The following will return
115+
// a negative number if subtree is right-heavy
116+
// a positive number if subtree is left-heavy
117+
// 0 if the subtree is perfectly balanced.
117118

118-
private int SubtreeBalance(Node nodeP)
119+
private int SubtreeBalance(Node nodeP)
119120
{
120121
UpdateHeight(nodeP.left);
121122
UpdateHeight(nodeP.right);
122-
// The resulting value is essentially
123-
// nodeP.left.Height - nodeP.right.Height
124-
// but we need to account for null values.
125-
int balance = 0;
126-
if (!(nodeP == null) &&
127-
!(nodeP.left == null && nodeP.right == null))
128-
{
129-
// If nodeP is null, or if nodeP has no children,
130-
// then balanceP is 0. Otherwise, we compute it:
131-
if (nodeP.left == null)
132-
{
133-
balance = -(nodeP.right.Height + 1);
134-
}
135-
else if (nodeP.right == null)
136-
{
137-
balance = nodeP.left.Height + 1;
138-
}
139-
else
140-
{
141-
balance = nodeP.left.Height - nodeP.right.Height;
142-
}
143-
}
123+
// The resulting value is essentially
124+
// nodeP.left.Height - nodeP.right.Height
125+
// but we need to account for null values.
126+
int balance = 0;
127+
if (
128+
!(nodeP == null)
129+
&& !(nodeP.left == null && nodeP.right == null)
130+
)
131+
{
132+
// If nodeP is null, or if nodeP has no children,
133+
// then balanceP is 0. Otherwise, we compute it:
134+
if (nodeP.left == null)
135+
{
136+
balance = -(nodeP.right.Height + 1);
137+
}
138+
else if (nodeP.right == null)
139+
{
140+
balance = nodeP.left.Height + 1;
141+
}
142+
else
143+
{
144+
balance = nodeP.left.Height - nodeP.right.Height;
145+
}
146+
}
144147
return balance;
145148
}
146149

@@ -270,10 +273,10 @@ private Node DoublerightChild(Node nodeP)
270273
return RotaterightChild(nodeP);
271274
}
272275

273-
// Note that the following method could also use
274-
// SubtreeBalance to compute if the tree needs to be
275-
// re-balanced, instead of using GetHeight directly.
276-
private Node Insert(T valueP, Node nodeP)
276+
// Note that the following method could also use
277+
// SubtreeBalance to compute if the tree needs to be
278+
// re-balanced, instead of using GetHeight directly.
279+
private Node Insert(T valueP, Node nodeP)
277280
{
278281
if (nodeP == null)
279282
return new Node(valueP, null, null, 0);
@@ -330,7 +333,7 @@ private Node Insert(T valueP, Node nodeP)
330333
return nodeP;
331334
}
332335

333-
public bool Remove(T value)
336+
public bool Remove(T value)
334337
{
335338
return Remove(value, ref root);
336339
}
@@ -395,7 +398,7 @@ private bool Remove(T value, ref Node nodeP)
395398
// The ToString method is simply here to help us debug.
396399
// It is not really pretty, but using pre-order and spaces
397400
// to make it easier to understand how the tree is
398-
// constructed.
401+
// constructed.
399402

400403
public override string ToString()
401404
{
@@ -428,4 +431,4 @@ private string Stringify(Node nodeP, int depth)
428431
}
429432
return returned;
430433
}
431-
}
434+
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ static void Main()
66
{
77
AVLTree<int> avltree1 = new AVLTree<int>();
88

9-
avltree1.Insert(10);
10-
avltree1.Insert(5);
11-
avltree1.Insert(4);
12-
avltree1.Insert(6);
13-
avltree1.Insert(20);
14-
Console.WriteLine(avltree1);
15-
avltree1.Insert(2);
16-
Console.WriteLine(avltree1);
17-
avltree1.Remove(5);
18-
Console.WriteLine(avltree1);
19-
}
9+
avltree1.Insert(10);
10+
avltree1.Insert(5);
11+
avltree1.Insert(4);
12+
avltree1.Insert(6);
13+
avltree1.Insert(20);
14+
Console.WriteLine(avltree1);
15+
avltree1.Insert(2);
16+
Console.WriteLine(avltree1);
17+
avltree1.Remove(5);
18+
Console.WriteLine(avltree1);
19+
}
2020
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ public bool IsEmpty()
7979
// We now look at how to
8080
// compute the height of a tree.
8181
// Remember that the height
82-
// of a tree is the height of
82+
// of a tree is the height of
8383
// its root node.
8484
public int Height()
8585
{
8686
int height = 0;
8787
if (root != null)
8888
{
89-
height = root.Height;
89+
height = root.Height;
9090
}
9191
return height;
9292
}

0 commit comments

Comments
 (0)