summaryrefslogtreecommitdiff
path: root/week3/BinaryTree.cs
diff options
context:
space:
mode:
Diffstat (limited to 'week3/BinaryTree.cs')
-rw-r--r--week3/BinaryTree.cs57
1 files changed, 57 insertions, 0 deletions
diff --git a/week3/BinaryTree.cs b/week3/BinaryTree.cs
new file mode 100644
index 0000000..8bed181
--- /dev/null
+++ b/week3/BinaryTree.cs
@@ -0,0 +1,57 @@
+using System;
+
+namespace ALGA {
+ public class BinaryTree {
+ public Node root;
+
+ public void insert(int number) {
+ if (root == null) {
+ root = new Node(number);
+ return;
+ }
+ root.insert(number);
+ }
+
+ public void delete(int number) {
+ if (root == null) return;
+ Node node = root.delete(number);
+ if (node == null) root = null;
+ }
+
+ public bool exists(int number) {
+ if (root == null) return false;
+ return root.find(number) != null;
+ }
+
+ public int min() {
+ if (root == null) return -1;
+ return root.min().number;
+ }
+
+ public int max() {
+ if (root == null) return -1;
+ return root.max().number;
+ }
+
+ public int depth() {
+ if (root == null) return 0;
+ return root.depth();
+ }
+
+ public int count() {
+ if (root == null) return 0;
+ return root.count();
+ }
+
+ public void print() {
+ if (root == null) return;
+ Console.WriteLine(string.Join(" ", root.flatten()));
+ }
+
+ public void printInRange(int min, int max) {
+ if (root == null) return;
+ Console.WriteLine(string.Join(" ", root.flatten(min, max)));
+ }
+ }
+}
+