summaryrefslogtreecommitdiff
path: root/tree/src
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-09 16:03:28 +0200
committer3gg <3gg@shellblade.net>2025-08-09 16:03:28 +0200
commit727e3c59346da4f91284b34b4c18f2e0ba155e53 (patch)
tree807dccd5cba3c6bae2f8d0c9910157e306c6da5b /tree/src
Initial commitHEADmain
Diffstat (limited to 'tree/src')
-rw-r--r--tree/src/main.adb14
-rw-r--r--tree/src/tree.adb12
-rw-r--r--tree/src/tree.ads18
3 files changed, 44 insertions, 0 deletions
diff --git a/tree/src/main.adb b/tree/src/main.adb
new file mode 100644
index 0000000..b9ece1a
--- /dev/null
+++ b/tree/src/main.adb
@@ -0,0 +1,14 @@
1with Ada.Text_IO; use Ada.Text_IO;
2
3with Tree;
4
5procedure Main is
6 package IntTree is new Tree (Integer);
7 T : IntTree.Tree_Access := new IntTree.Tree;
8begin
9 T.Left := new IntTree.Tree;
10 T.Right := new IntTree.Tree;
11 T.Right.Left := new IntTree.Tree;
12
13 Put_Line ("Tree height:" & IntTree.Height (T)'Image);
14end Main;
diff --git a/tree/src/tree.adb b/tree/src/tree.adb
new file mode 100644
index 0000000..7e4a897
--- /dev/null
+++ b/tree/src/tree.adb
@@ -0,0 +1,12 @@
1package body tree is
2
3 function Height (T : Tree_Access) return Integer is
4 begin
5 if T = null then
6 return 0;
7 else
8 return 1 + Integer'Max (Height (T.Left), Height (T.Right));
9 end if;
10 end Height;
11
12end tree;
diff --git a/tree/src/tree.ads b/tree/src/tree.ads
new file mode 100644
index 0000000..1cf26fc
--- /dev/null
+++ b/tree/src/tree.ads
@@ -0,0 +1,18 @@
1generic
2 type T is private;
3
4package tree is
5
6 type Tree;
7 type Tree_Access is access Tree;
8
9 type Tree is record
10 Val : T;
11 Left : Tree_Access;
12 Right : Tree_Access;
13 end record;
14
15 -- Returns the height of the tree.
16 function Height (T : Tree_Access) return Integer;
17
18end tree;