Skip to content

Commit b3081c8

Browse files
committed
Improving notes on BSTree.
1 parent fe18c4a commit b3081c8

6 files changed

Lines changed: 232 additions & 185 deletions

File tree

source/code/projects/Tree/Tree/BSTree.cs

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
public class BSTree<T> : BTree<T>
55
where T : IComparable<T>
66
{
7-
// Inserting into a BSTree
7+
// Inserting into a BSTree
88
public override void Insert(T dataP)
99
{
1010
root = Insert(dataP, root);
@@ -32,72 +32,74 @@ private Node Insert(T dataP, Node nodeP)
3232
}
3333
return nodeP;
3434
}
35-
public override bool Find(T dataP)
35+
36+
public override bool Find(T dataP)
37+
{
38+
bool found = false;
39+
if (root != null)
3640
{
37-
bool found = false;
38-
if (root != null)
39-
{
40-
found = Find(root, dataP);
41-
}
42-
return found;
41+
found = Find(root, dataP);
4342
}
43+
return found;
44+
}
4445

45-
// Finding into a BSTree
46+
// Finding into a BSTree
4647

47-
private bool Find(Node nodeP, T dataP)
48+
private bool Find(Node nodeP, T dataP)
49+
{
50+
bool found = false;
51+
if (nodeP != null)
4852
{
49-
bool found = false;
50-
if (nodeP != null)
53+
if (nodeP.Data.Equals(dataP))
54+
{
55+
found = true;
56+
}
57+
else
58+
{
59+
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
5160
{
52-
if (nodeP.Data.Equals(dataP))
53-
{
54-
found = true;
55-
}
56-
else
57-
{
58-
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
59-
{
60-
found = Find(nodeP.left, dataP);
61-
}
62-
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
63-
{
64-
found = Find(nodeP.right, dataP);
65-
}
66-
}
61+
found = Find(nodeP.left, dataP);
6762
}
68-
return found;
63+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
64+
{
65+
found = Find(nodeP.right, dataP);
66+
}
67+
}
6968
}
69+
return found;
70+
}
7071

71-
public T FindMin()
72+
public T FindMin()
73+
{
74+
if (root == null)
7275
{
73-
if (root == null)
74-
{
75-
throw new ApplicationException(
76-
"Cannot find a value in an empty tree!"
77-
);
78-
}
79-
else
80-
{
81-
return FindMin(root);
82-
}
76+
throw new ApplicationException(
77+
"Cannot find a value in an empty tree!"
78+
);
8379
}
80+
else
81+
{
82+
return FindMin(root);
83+
}
84+
}
8485

85-
private T FindMin(Node nodeP)
86+
private T FindMin(Node nodeP)
87+
{
88+
T minValue;
89+
if (nodeP.left == null)
8690
{
87-
T minValue;
88-
if (nodeP.left == null)
89-
{
90-
minValue = nodeP.Data;
91-
}
92-
else
93-
{
94-
minValue = FindMin(nodeP.left);
95-
}
96-
return minValue;
91+
minValue = nodeP.Data;
92+
}
93+
else
94+
{
95+
minValue = FindMin(nodeP.left);
9796
}
98-
// Deleting from a BSTree
97+
return minValue;
98+
}
9999

100-
public override bool Delete(T dataP)
100+
// Deleting from a BSTree
101+
102+
public override bool Delete(T dataP)
101103
{
102104
return Delete(dataP, ref root);
103105
}
@@ -122,8 +124,8 @@ private bool Delete(T dataP, ref Node nodeP)
122124
{
123125
nodeP.Data = FindMin(nodeP.right);
124126
Delete(nodeP.Data, ref nodeP.right);
125-
// Or we could replace with the largest
126-
// value in the left subtree.
127+
// Or we could replace with the largest
128+
// value in the left subtree.
127129
}
128130
else
129131
{
@@ -141,7 +143,5 @@ private bool Delete(T dataP, ref Node nodeP)
141143
return found;
142144
}
143145

144-
// Done with deletion.
145-
146-
146+
// Done with deletion.
147147
}

0 commit comments

Comments
 (0)