Skip to content

Commit 94de331

Browse files
ClémentClément
authored andcommitted
Some more precisions about trees.
1 parent bfed605 commit 94de331

5 files changed

Lines changed: 293 additions & 225 deletions

File tree

source/code/projects/CQueue/CQueue/CQueue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void Resize()
6262
// Increment must be done carefully:
6363
// if we reached the "end" of mArray,
6464
// then the value needs to become 0:
65-
// we resume "circling" through the
65+
// we resume "circling" through the
6666
// array.
6767
private void Increment(ref int value)
6868
{
Lines changed: 107 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,138 @@
11
using System;
22
using System.Collections.Generic;
33

4-
/*
5-
* with the key of each internal node being greater than all
6-
* the keys in the respective node's left subtree and
7-
* less than the ones in its right subtree.
8-
*/
9-
10-
public class BSTree<T> : BTree<T> where T : IComparable<T>
4+
public class BSTree<T> : BTree<T>
5+
where T : IComparable<T>
116
{
12-
public void Insert (T dataP)
7+
public override void Insert(T dataP)
8+
{
9+
root = Insert(dataP, root);
10+
}
11+
12+
private Node Insert(T dataP, Node nodeP)
13+
{
14+
if (nodeP == null)
1315
{
14-
root = Insert(dataP, root);
16+
return new Node(dataP, null, null);
1517
}
16-
private Node Insert(T dataP, Node nodeP) {
17-
if (nodeP == null)
18-
{
19-
return new Node(dataP, null, null);
20-
}
21-
else if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
22-
{
23-
nodeP.left = Insert(dataP, nodeP.left);
24-
}
25-
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
18+
else if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
19+
{
20+
nodeP.left = Insert(dataP, nodeP.left);
21+
}
22+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
23+
{
24+
nodeP.right = Insert(dataP, nodeP.right);
25+
}
26+
else
27+
{
28+
throw new ApplicationException(
29+
"Value " + dataP + " already in tree."
30+
);
31+
}
32+
return nodeP;
33+
}
34+
35+
public override bool Delete(T dataP)
36+
{
37+
return Delete(dataP, ref root);
38+
}
39+
40+
private bool Delete(T dataP, ref Node nodeP)
41+
{
42+
bool found = false;
43+
if (nodeP != null)
44+
{
45+
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
46+
{
47+
found = Delete(dataP, ref nodeP.left);
48+
}
49+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
50+
{
51+
found = Delete(dataP, ref nodeP.right);
52+
}
53+
else // We found the value!
54+
{
55+
found = true;
56+
if (nodeP.left != null && nodeP.right != null)
2657
{
27-
nodeP.right = Insert(dataP, nodeP.right);
58+
nodeP.Data = FindMin(nodeP.right);
59+
Delete(nodeP.Data, ref nodeP.right);
2860
}
2961
else
3062
{
31-
throw new ApplicationException("Value " + dataP + " already in tree.");
63+
if (nodeP.left != null)
64+
{
65+
nodeP = nodeP.left;
66+
}
67+
else
68+
{
69+
nodeP = nodeP.right;
70+
}
3271
}
33-
return nodeP;
72+
}
3473
}
74+
return found;
75+
}
3576

36-
public bool Delete(T dataP)
77+
public override bool Find(T dataP)
78+
{
79+
bool found = false;
80+
if (root != null)
3781
{
38-
return Delete(dataP, ref root);
82+
found = Find(root, dataP);
3983
}
84+
return found;
85+
}
4086

41-
private bool Delete(T dataP, ref Node nodeP)
87+
private bool Find(Node nodeP, T dataP)
88+
{
89+
bool found = false;
90+
if (nodeP != null)
4291
{
43-
bool found = false;
44-
if (nodeP != null)
92+
if (nodeP.Data.Equals(dataP))
93+
{
94+
found = true;
95+
}
96+
else
97+
{
98+
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
4599
{
46-
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
47-
{
48-
found = Delete(dataP, ref nodeP.left);
49-
}
50-
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
51-
{
52-
found = Delete(dataP, ref nodeP.right);
53-
}
54-
else // We found the value!
55-
{
56-
found = true;
57-
if (nodeP.left != null && nodeP.right != null)
58-
{
59-
nodeP.Data = FindMin(nodeP.right);
60-
Delete(nodeP.Data, ref nodeP.right);
61-
}
62-
else {
63-
if (nodeP.left != null)
64-
{
65-
nodeP = nodeP.left;
66-
}
67-
else
68-
{
69-
nodeP = nodeP.right;
70-
}
71-
}
72-
}
73-
100+
found = Find(nodeP.left, dataP);
74101
}
75-
return found;
76-
}
77-
78-
public bool Find(T dataP)
79-
{
80-
bool found = false;
81-
if (root != null)
102+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
82103
{
83-
found = Find(root, dataP);
104+
found = Find(nodeP.right, dataP);
84105
}
85-
return found;
106+
}
86107
}
108+
return found;
109+
}
87110

88-
private bool Find(Node nodeP, T dataP)
111+
public T FindMin()
112+
{
113+
if (root == null)
89114
{
90-
bool found = false;
91-
if (nodeP != null)
92-
{
93-
94-
if (nodeP.Data.Equals(dataP))
95-
{
96-
found = true;
97-
}
98-
else
99-
{
100-
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
101-
{
102-
found = Find(nodeP.left, dataP);
103-
}
104-
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
105-
{
106-
found = Find(nodeP.right, dataP);
107-
}
108-
}
109-
}
110-
return found;
111-
115+
throw new ApplicationException(
116+
"Cannot find a value in an empty tree!"
117+
);
112118
}
119+
else
120+
{
121+
return FindMin(root);
122+
}
123+
}
113124

114-
115-
public T FindMin()
125+
private T FindMin(Node nodeP)
126+
{
127+
T minValue;
128+
if (nodeP.left == null)
116129
{
117-
if (root == null)
118-
{
119-
throw new ApplicationException("Cannot find a value in an empty tree!");
120-
}
121-
else
122-
{
123-
return FindMin(root);
124-
}
130+
minValue = nodeP.Data;
125131
}
126-
private T FindMin(Node nodeP)
132+
else
127133
{
128-
T minValue;
129-
if(nodeP.left == null)
130-
{
131-
minValue = nodeP.Data;
132-
}
133-
else
134-
{
135-
minValue = FindMin(nodeP.left);
136-
}
137-
return minValue;
134+
minValue = FindMin(nodeP.left);
138135
}
136+
return minValue;
137+
}
139138
}

0 commit comments

Comments
 (0)