Skip to content

Commit 40242df

Browse files
ClémentClément
authored andcommitted
Fixing issue with BTree.
2 parents 6bb9c85 + de10eb2 commit 40242df

11 files changed

Lines changed: 407 additions & 221 deletions

File tree

source/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ $(FLO_DIR)%.png: $(FLO_DIR)%.txt
378378
# Graph diagram are only used for
379379
# binary trees.
380380
$(GRA_DIR)%.md: $(GRA_DIR)%.txt | $(GRA_DIR)%.svg $(GRA_DIR)%.png
381-
echo "![A binary tree diagram ([text version](./$(basename $@).txt), [image version](./$(basename $@).png), [svg version](./$(basename $@).svg))]($(notdir $(basename $@)).png)" > $@
381+
echo "![$$(sed -n '1p' $< | sed -r 's/^.{3}//') ([text version](./$(basename $@).txt), [image version](./$(basename $@).png), [svg version](./$(basename $@).svg))]($(notdir $(basename $@)).png)" > $@
382382

383383
$(GRA_DIR)%.svg: $(GRA_DIR)%.txt
384384
@echo "Creating $@"

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

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
public class BSTree<T> : BTree<T>
55
where T : IComparable<T>
66
{
7+
// Inserting into a BSTree
78
public override void Insert(T dataP)
89
{
910
root = Insert(dataP, root);
@@ -32,61 +33,19 @@ private Node Insert(T dataP, Node nodeP)
3233
return nodeP;
3334
}
3435

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)
57-
{
58-
nodeP.Data = FindMin(nodeP.right);
59-
Delete(nodeP.Data, ref nodeP.right);
60-
// Or we could replace with the largest
61-
// value in the left subtree.
62-
}
63-
else
64-
{
65-
if (nodeP.left != null)
66-
{
67-
nodeP = nodeP.left;
68-
}
69-
else
70-
{
71-
nodeP = nodeP.right;
72-
}
73-
}
74-
}
75-
}
76-
return found;
77-
}
36+
// Finding into a BSTree
7837

7938
public override bool Find(T dataP)
8039
{
8140
bool found = false;
8241
if (root != null)
8342
{
84-
found = Find(root, dataP);
43+
found = Find(dataP, root);
8544
}
8645
return found;
8746
}
8847

89-
private bool Find(Node nodeP, T dataP)
48+
private bool Find(T dataP, Node nodeP)
9049
{
9150
bool found = false;
9251
if (nodeP != null)
@@ -99,11 +58,11 @@ private bool Find(Node nodeP, T dataP)
9958
{
10059
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
10160
{
102-
found = Find(nodeP.left, dataP);
61+
found = Find(dataP, nodeP.left);
10362
}
10463
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
10564
{
106-
found = Find(nodeP.right, dataP);
65+
found = Find(dataP, nodeP.right);
10766
}
10867
}
10968
}
@@ -137,4 +96,52 @@ private T FindMin(Node nodeP)
13796
}
13897
return minValue;
13998
}
99+
100+
// Deleting from a BSTree
101+
102+
public override bool Delete(T dataP)
103+
{
104+
return Delete(dataP, ref root);
105+
}
106+
107+
private bool Delete(T dataP, ref Node nodeP)
108+
{
109+
bool found = false;
110+
if (nodeP != null)
111+
{
112+
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
113+
{
114+
found = Delete(dataP, ref nodeP.left);
115+
}
116+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
117+
{
118+
found = Delete(dataP, ref nodeP.right);
119+
}
120+
else // We found the value!
121+
{
122+
found = true;
123+
if (nodeP.left != null && nodeP.right != null)
124+
{
125+
nodeP.Data = FindMin(nodeP.right);
126+
Delete(nodeP.Data, ref nodeP.right);
127+
// Or we could replace with the largest
128+
// value in the left subtree.
129+
}
130+
else
131+
{
132+
if (nodeP.left != null)
133+
{
134+
nodeP = nodeP.left;
135+
}
136+
else
137+
{
138+
nodeP = nodeP.right;
139+
}
140+
}
141+
}
142+
}
143+
return found;
144+
}
145+
146+
// Done with deletion.
140147
}

0 commit comments

Comments
 (0)