summaryrefslogtreecommitdiff
path: root/week3/BinaryTree.cs
blob: 8bed18144c1d8dd4cc0050a9a1f1f2fc49f8e61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)));
		}
	}
}