|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 |
|
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> |
11 | 6 | { |
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) |
13 | 15 | { |
14 | | - root = Insert(dataP, root); |
| 16 | + return new Node(dataP, null, null); |
15 | 17 | } |
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) |
26 | 57 | { |
27 | | - nodeP.right = Insert(dataP, nodeP.right); |
| 58 | + nodeP.Data = FindMin(nodeP.right); |
| 59 | + Delete(nodeP.Data, ref nodeP.right); |
28 | 60 | } |
29 | 61 | else |
30 | 62 | { |
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 | + } |
32 | 71 | } |
33 | | - return nodeP; |
| 72 | + } |
34 | 73 | } |
| 74 | + return found; |
| 75 | + } |
35 | 76 |
|
36 | | - public bool Delete(T dataP) |
| 77 | + public override bool Find(T dataP) |
| 78 | + { |
| 79 | + bool found = false; |
| 80 | + if (root != null) |
37 | 81 | { |
38 | | - return Delete(dataP, ref root); |
| 82 | + found = Find(root, dataP); |
39 | 83 | } |
| 84 | + return found; |
| 85 | + } |
40 | 86 |
|
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) |
42 | 91 | { |
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 |
45 | 99 | { |
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); |
74 | 101 | } |
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 |
82 | 103 | { |
83 | | - found = Find(root, dataP); |
| 104 | + found = Find(nodeP.right, dataP); |
84 | 105 | } |
85 | | - return found; |
| 106 | + } |
86 | 107 | } |
| 108 | + return found; |
| 109 | + } |
87 | 110 |
|
88 | | - private bool Find(Node nodeP, T dataP) |
| 111 | + public T FindMin() |
| 112 | + { |
| 113 | + if (root == null) |
89 | 114 | { |
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 | + ); |
112 | 118 | } |
| 119 | + else |
| 120 | + { |
| 121 | + return FindMin(root); |
| 122 | + } |
| 123 | + } |
113 | 124 |
|
114 | | - |
115 | | - public T FindMin() |
| 125 | + private T FindMin(Node nodeP) |
| 126 | + { |
| 127 | + T minValue; |
| 128 | + if (nodeP.left == null) |
116 | 129 | { |
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; |
125 | 131 | } |
126 | | - private T FindMin(Node nodeP) |
| 132 | + else |
127 | 133 | { |
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); |
138 | 135 | } |
| 136 | + return minValue; |
| 137 | + } |
139 | 138 | } |
0 commit comments