0 | module Data.Container.Base.TreeUtils
2 | import Decidable.Equality
3 | import Language.Reflection
4 | import Derive.Prelude
6 | import Data.Container.Base.Object.Definition
7 | import Data.Container.Base.Extension.Definition
9 | import Data.Container.SubTerm
11 | %language ElabReflection
32 | namespace BinaryTrees
35 | data BinTreeShape : Type where
36 | LeafS : BinTreeShape
37 | NodeS : BinTreeShape -> BinTreeShape -> BinTreeShape
39 | %runElab derive "BinTreeShape" [Eq, Show]
40 | %name BinTreeShape
b, b1, b2, b3, b4, b5
43 | numLeaves : BinTreeShape -> Nat
45 | numLeaves (NodeS lt rt) = numLeaves lt + numLeaves rt
48 | numNodes : BinTreeShape -> Nat
50 | numNodes (NodeS lt rt) = numNodes lt + (1 + numNodes rt)
53 | numNodesAndLeaves : BinTreeShape -> Nat
54 | numNodesAndLeaves LeafS = 1
55 | numNodesAndLeaves (NodeS lt rt)
56 | = numNodesAndLeaves lt + (1 + numNodesAndLeaves rt)
58 | namespace NodesAndLeaves
61 | data BinTreePos : (b : BinTreeShape) -> Type where
62 | AtLeaf : BinTreePos LeafS
63 | AtNode : {l, r : BinTreeShape} -> BinTreePos (NodeS l r)
64 | GoLeft : {l, r : BinTreeShape} -> BinTreePos l -> BinTreePos (NodeS l r)
65 | GoRight : {l, r : BinTreeShape} -> BinTreePos r -> BinTreePos (NodeS l r)
67 | %runElab deriveIndexed "BinTreePos" [Eq, Show]
72 | MOrd (BinTreePos b) where
73 | mcompare AtLeaf AtLeaf = Just EQ
74 | mcompare AtNode AtNode = Just EQ
75 | mcompare (GoLeft b1) (GoLeft b2) = mcompare b1 b2
76 | mcompare (GoRight b1) (GoRight b2) = mcompare b1 b2
77 | mcompare AtNode (GoLeft _) = Just LT
78 | mcompare AtNode (GoRight _) = Just LT
79 | mcompare (GoLeft _) AtNode = Just GT
80 | mcompare (GoRight _) AtNode = Just GT
81 | mcompare (GoLeft _) (GoRight _) = Nothing
82 | mcompare (GoRight _) (GoLeft _) = Nothing
89 | data BinTreePosNode : (b : BinTreeShape) -> Type where
90 | AtNode : {l, r : BinTreeShape} -> BinTreePosNode (NodeS l r)
91 | GoLeft : {l, r : BinTreeShape} -> BinTreePosNode l -> BinTreePosNode (NodeS l r)
92 | GoRight : {l, r : BinTreeShape} -> BinTreePosNode r -> BinTreePosNode (NodeS l r)
94 | %runElab deriveIndexed "BinTreePosNode" [Eq, Show]
97 | MOrd (BinTreePosNode b) where
98 | mcompare AtNode AtNode = Just EQ
99 | mcompare (GoLeft b1) (GoLeft b2) = mcompare b1 b2
100 | mcompare (GoRight b1) (GoRight b2) = mcompare b1 b2
101 | mcompare AtNode (GoLeft _) = Just LT
102 | mcompare AtNode (GoRight _) = Just LT
103 | mcompare (GoLeft _) AtNode = Just GT
104 | mcompare (GoRight _) AtNode = Just GT
105 | mcompare (GoLeft _) (GoRight _) = Nothing
106 | mcompare (GoRight _) (GoLeft _) = Nothing
111 | data BinTreePosLeaf : (b : BinTreeShape) -> Type where
112 | AtLeaf : BinTreePosLeaf LeafS
113 | GoLeft : {l, r : BinTreeShape} -> BinTreePosLeaf l -> BinTreePosLeaf (NodeS l r)
114 | GoRight : {l, r : BinTreeShape} -> BinTreePosLeaf r -> BinTreePosLeaf (NodeS l r)
116 | %runElab deriveIndexed "BinTreePosLeaf" [Eq, Show]
119 | MOrd (BinTreePosLeaf b) where
120 | mcompare AtLeaf AtLeaf = Just EQ
121 | mcompare (GoLeft b1) (GoLeft b2) = mcompare b1 b2
122 | mcompare (GoRight b1) (GoRight b2) = mcompare b1 b2
123 | mcompare (GoLeft _) (GoRight _) = Nothing
124 | mcompare (GoRight _) (GoLeft _) = Nothing