Skip to content

Commit ad18a15

Browse files
committed
Merge branch 'main' of github.com:princomp/princomp.github.io
2 parents 7c71871 + 7cee230 commit ad18a15

File tree

26 files changed

+1537
-4
lines changed

26 files changed

+1537
-4
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class AVLTree<T> : BSTree<T>
5+
where T : IComparable<T>
6+
{
7+
public override void Insert(T valueP)
8+
{
9+
root = Insert(valueP, root);
10+
}
11+
12+
private Node Insert(T valueP, Node nodeP)
13+
{
14+
if (nodeP == null)
15+
return new Node(valueP, null, null);
16+
else if (valueP.CompareTo(nodeP.Data) < 0) // valueP < nodeP.Data --> go left
17+
{
18+
nodeP.left = Insert(valueP, nodeP.left);
19+
int nodePLH =
20+
(nodeP.left == null) ? -1 : nodeP.left.Height;
21+
int nodePRH =
22+
(nodeP.right == null) ? -1 : nodeP.right.Height;
23+
24+
if (nodePLH - nodePRH == 2)
25+
{
26+
if (valueP.CompareTo(nodeP.left.Data) < 0)
27+
{
28+
nodeP = RotateleftChild(nodeP);
29+
}
30+
else
31+
{
32+
nodeP = DoubleleftChild(nodeP);
33+
}
34+
}
35+
}
36+
else if (valueP.CompareTo(nodeP.Data) > 0) // valueP > nodeP.Data --> go right
37+
{
38+
nodeP.right = Insert(valueP, nodeP.right);
39+
int nodePLH =
40+
(nodeP.left == null) ? -1 : nodeP.left.Height;
41+
int nodePRH =
42+
(nodeP.right == null) ? -1 : nodeP.right.Height;
43+
if (nodePRH - nodePLH == 2)
44+
{
45+
if (valueP.CompareTo(nodeP.right.Data) > 0)
46+
{
47+
nodeP = RotaterightChild(nodeP);
48+
}
49+
else
50+
{
51+
nodeP = DoublerightChild(nodeP);
52+
}
53+
}
54+
}
55+
else // valueP == nodeP.Data
56+
{
57+
throw new ApplicationException(
58+
"Tree did not insert "
59+
+ valueP
60+
+ " since an item with that value is already in the tree"
61+
);
62+
}
63+
return nodeP;
64+
}
65+
66+
public override bool Delete(T dataP)
67+
{
68+
return Delete(dataP, ref root);
69+
}
70+
71+
private bool Delete(T value, ref Node nodeP)
72+
{
73+
bool found = false;
74+
if (nodeP != null)
75+
{
76+
if (value.CompareTo(nodeP.Data) < 0) // value < nodeP.Data, check left subtree
77+
{
78+
found = Delete(value, ref nodeP.left); // similar to BST's find and remove method
79+
if (SubtreeBalance(nodeP) <= -2) // negative balance means heavy on right side
80+
{
81+
if (SubtreeBalance(nodeP.right) <= 0) // children in straight line
82+
nodeP = RotaterightChild(nodeP); // rotate middle up to balance
83+
else
84+
nodeP = DoublerightChild(nodeP); // children in zig patter - needs double rotate to balance
85+
}
86+
}
87+
else if (value.CompareTo(nodeP.Data) > 0) // value > nodeP.Data, check right subtree
88+
{
89+
found = Delete(value, ref nodeP.right);
90+
if (SubtreeBalance(nodeP) >= 2)
91+
{
92+
if (SubtreeBalance(nodeP.left) >= 0)
93+
nodeP = RotateleftChild(nodeP);
94+
else
95+
nodeP = DoubleleftChild(nodeP);
96+
}
97+
}
98+
else // The value was found!
99+
{
100+
found = true;
101+
if (nodeP.left != null && nodeP.right != null) // Two children
102+
{
103+
nodeP.Data = FindMin(nodeP.right);
104+
Delete(nodeP.Data, ref nodeP.right);
105+
if (SubtreeBalance(nodeP) == 2) // Need to rebalance
106+
{
107+
if (SubtreeBalance(nodeP.left) >= 0)
108+
nodeP = RotateleftChild(nodeP);
109+
else
110+
nodeP = DoubleleftChild(nodeP);
111+
}
112+
}
113+
else
114+
{
115+
nodeP = nodeP.left ?? nodeP.right; // replace with one or no child
116+
// This is equivalent to
117+
// if (nodeP.left == null){
118+
// nodeP = nodeP.right;
119+
// } else { nodeP = nodeP.left;}
120+
// Observe that if both are null, then nodeP simply
121+
// becomes null, as expected.
122+
}
123+
}
124+
}
125+
return found;
126+
}
127+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class BSTree<T> : IBTree<T>
5+
where T : IComparable<T>
6+
{
7+
// Inserting into a BSTree
8+
public override void Insert(T dataP)
9+
{
10+
root = Insert(dataP, root);
11+
}
12+
13+
private Node Insert(T dataP, Node nodeP)
14+
{
15+
if (nodeP == null)
16+
{
17+
return new Node(dataP, null, null);
18+
}
19+
else if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
20+
{
21+
nodeP.left = Insert(dataP, nodeP.left);
22+
}
23+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
24+
{
25+
nodeP.right = Insert(dataP, nodeP.right);
26+
}
27+
else
28+
{
29+
throw new ApplicationException(
30+
"Value " + dataP + " already in tree."
31+
);
32+
}
33+
return nodeP;
34+
}
35+
36+
// Finding into a BSTree
37+
38+
public override bool Find(T dataP)
39+
{
40+
bool found = false;
41+
if (root != null)
42+
{
43+
found = Find(dataP, root);
44+
}
45+
return found;
46+
}
47+
48+
private bool Find(T dataP, Node nodeP)
49+
{
50+
bool found = false;
51+
if (nodeP != null)
52+
{
53+
if (nodeP.Data.Equals(dataP))
54+
{
55+
found = true;
56+
}
57+
else
58+
{
59+
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
60+
{
61+
found = Find(dataP, nodeP.left);
62+
}
63+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
64+
{
65+
found = Find(dataP, nodeP.right);
66+
}
67+
}
68+
}
69+
return found;
70+
}
71+
72+
public T FindMin()
73+
{
74+
if (root == null)
75+
{
76+
throw new ApplicationException(
77+
"Cannot find a value in an empty tree!"
78+
);
79+
}
80+
else
81+
{
82+
return FindMin(root);
83+
}
84+
}
85+
86+
protected T FindMin(Node nodeP)
87+
{
88+
T minValue;
89+
if (nodeP.left == null)
90+
{
91+
minValue = nodeP.Data;
92+
}
93+
else
94+
{
95+
minValue = FindMin(nodeP.left);
96+
}
97+
return minValue;
98+
}
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.
147+
}

0 commit comments

Comments
 (0)