Skip to content
This repository was archived by the owner on May 22, 2026. It is now read-only.

Commit 67b0788

Browse files
feat: linear search with guard
1 parent 7156980 commit 67b0788

5 files changed

Lines changed: 44 additions & 15 deletions

File tree

data_structure/tree/tree.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
data Tree a = Node a [Tree a] deriving (Show)
1+
data Tree a = Node a [Tree a] deriving (Show) -- 型コンストラクタとしてTreeをデータコンストラクタとしてNodeを作成
22

33
-- 葉ノード(子を持たないノード)を作成
44
leaf :: a -> Tree a
@@ -11,14 +11,14 @@ rootValue (Node x _) = x
1111
-- 特定の値を持つノードの子として新しい値を挿入
1212
-- 見つからない場合は元の木を返す
1313
insertAt :: (Eq a) => a -> a -> Tree a -> Tree a
14-
insertAt target newVal (Node x cs)
15-
| x == target = Node x (leaf newVal : cs)
16-
| otherwise = Node x (map (insertAt target newVal) cs)
14+
insertAt target newVal (Node x children)
15+
| x == target = Node x (leaf newVal : children)
16+
| otherwise = Node x (map (insertAt target newVal) children) -- mapするのはすべての子要素について調査するため
1717

1818
-- 特定の値を持つノードを削除(子ノードは親に昇格)
1919
-- ルートノードは削除できない
2020
delete :: (Eq a) => a -> Tree a -> Tree a
21-
delete target (Node x cs) = Node x (concatMap (deleteHelper target) cs)
21+
delete target (Node x children) = Node x (concatMap (deleteHelper target) children)
2222
where
2323
deleteHelper :: (Eq a) => a -> Tree a -> [Tree a]
2424
deleteHelper t (Node v children')
@@ -27,33 +27,33 @@ delete target (Node x cs) = Node x (concatMap (deleteHelper target) cs)
2727

2828
-- 木に特定の値が含まれているか検索
2929
contains :: (Eq a) => a -> Tree a -> Bool
30-
contains target (Node x cs) = x == target || any (contains target) cs
30+
contains target (Node x children) = x == target || any (contains target) children
3131

3232
-- 木のサイズ(ノード数)を取得
3333
size :: Tree a -> Int
34-
size (Node _ cs) = 1 + sum (map size cs)
34+
size (Node _ children) = 1 + sum (map size children)
3535

3636
-- 木の深さを取得
3737
depth :: Tree a -> Int
3838
depth (Node _ []) = 1
39-
depth (Node _ cs) = 1 + maximum (map depth cs)
39+
depth (Node _ children) = 1 + maximum (map depth children)
4040

4141
-- 木を整形して表示(枝を使って階層構造を明確化)
4242
prettyPrint :: (Show a) => Tree a -> String
4343
prettyPrint tree = go "" "" tree
4444
where
45-
go prefix childPrefix (Node x cs) =
45+
go prefix childPrefix (Node x children) =
4646
prefix
4747
++ show x
4848
++ "\n"
49-
++ drawChildren childPrefix cs
49+
++ drawChildren childPrefix children
5050

5151
drawChildren _ [] = ""
5252
drawChildren prefix [c] =
5353
go (prefix ++ "└── ") (prefix ++ " ") c
54-
drawChildren prefix (c : cs') =
54+
drawChildren prefix (c : children') =
5555
go (prefix ++ "├── ") (prefix ++ "") c
56-
++ drawChildren prefix cs'
56+
++ drawChildren prefix children'
5757

5858
main :: IO ()
5959
main = do

search/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@
55
- 先頭から対象となる値を探す。
66
- 計算量は O(n)。
77

8+
### 実装例
9+
10+
- [x] C
11+
- [x] Python
12+
- [x] Haskell
13+
814
### 線形探索 with 番兵法(Sentinel loop)あり
915

1016
- 前提として線形探索においてループの終了条件は,以下の 2 つがある。
1117
- 探索する値が見つかったか
1218
- 探索する値が見つからなかった場合に配列の末尾に到達したか
1319
- 探索する配列に番兵として探索する値を末尾に追加することで,必ず探索する値が見つかるようになるため,ループの終了条件を探索する値が見つかったかの 1 つにすることができる。
1420

21+
#### 実装例
22+
23+
- [x] C
24+
- [x] Python
25+
- [x] Haskell
26+
1527
---
1628

1729
## 二分探索(Binary search)
@@ -22,12 +34,16 @@
2234
- 配列を半分にわけて,ピボットを基準にターゲットとなる値が左右のどちらにあるか探索する。
2335
- 計算効率は、1 ステップごとに探索対象が半分になっていく。これは 2 進数においては 1 桁減ることを表しているので計算量は O(log2 n)と表せる。
2436

25-
### 関数を満たす最大/最小の値の探索
37+
### 関数を満たす最大/最小の値を二分探索で探す
2638

2739
- 二分探索を使い、関数f(x)がピボットに対してTrueかFalseかを判定することで、関数f(x)を満たす最大/最小のxの範囲を探索できる。
2840

2941
---
3042

43+
## BST(Binary Search Tree)
44+
45+
---
46+
3147
## bit 全探索(bit search)
3248

3349
---
@@ -43,5 +59,3 @@
4359
## h幅優先探索(Breadth First Search)
4460

4561
---
46-
47-
## 2 分木探索(Binary search tree)
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
linearSearch :: [Int] -> Int -> Int
2+
linearSearch xs target =
3+
let xs' = xs ++ [target] -- 番兵を追加
4+
in head
5+
[ i
6+
| i <- [0 .. length xs' - 1],
7+
xs' !! i == target
8+
]
9+
10+
main :: IO ()
11+
main = do
12+
let randomList = [30, 75, 69, 16, 47, 77, 60, 80, 74, 8, 77, 1, 60, 33, 70, 29, 24, 91, 60, 69]
13+
print randomList
14+
let target = 77
15+
print $ linearSearch randomList 77

0 commit comments

Comments
 (0)