Решенеия на задачите от упражненията

Решения на задачите от 17.01.2007

Решения на задачите от 17.01.2007

by Калин Николов -
Number of replies: 2
Решения на задачите от 17.01.2007
In reply to Калин Николов

Re: Решения на задачите от 17.01.2007

by Ralitza Todorowa -

data BinTree a=Empty|Node a (BinTree a) (BinTree a)
             deriving (Show,Eq)
t=Node 10 (Node 5 (Node 3 Empty Empty) (Node 7 (Node 6 Empty Empty) Empty)) (Node 15 (Node 12 Empty Empty) Empty)

minTree Empty=0
minTree (Node v Empty _)=v
minTree (Node v l r)=minTree l
insertBot Empty x=Node x Empty Empty
insertBot (Node v l r) x
                        |v<x=Node v l (insertBot r x)
                        |v>x=Node v (insertBot l x) r
                        |otherwise=Node v l r
delRoot (Node v Empty Empty)=Empty
delRoot (Node v l Empty)=l
delRoot (Node v Empty r)=r
delRoot (Node v l r)=Node (minTree r) l (delTree  r (minTree r))
delTree Empty x=Empty
delTree (Node v l r) x
                      |v<x=Node v l (delTree r x)
                      |v>x=Node v (delTree l x) r
                      |otherwise=delRoot (Node v l r)

t2=Node (10,100) (Node (10,50) Empty (Node (10,60) Empty Empty)) (Node (5,105) (Node (7,102) Empty Empty) Empty)              
isElem Empty x=False
isElem (Node v l r) x
                     |x<snd v && x>fst v=True
                     |otherwise=isElem r x
t3=Node ("hello") (Node ("cat") Empty (Node ("dog") Empty Empty)) (Node ("world") (Node ("pig") Empty Empty) Empty)  
count1 [] c=0
count1 s c
           |c==head s=1 + count1 (tail s) c
           |otherwise=count1 (tail s) c
countS Empty c=0
countS (Node v l r) c=(count1 v c)+(countS l c)+(countS r c)

In reply to Калин Николов

Re: Решения на задачите от 17.01.2007

by Климентина Русева -

data BinTree a = Empty | Node a (BinTree a) (BinTree a)
               deriving (Show,Eq)
 
{-
insertBot :: BinTree -> Int -> BinTree (former definitoin)
insertBot :: BinTree Int -> Int -> BinTree Int
insertBot :: BinTree String -> String -> BinTree String
 -}

t = Node (10,100)(Node (10,50) Empty (Node (10,60) Empty Empty))(Node (5,102) (Node (7,102) Empty Empty) Empty)
 
isElem x Empty = False
isElem x (Node v l r)
 |x<(fst v) || x>(snd v) = isElem x r
 |x>=(fst v) && x<=(snd v) = True

d = Node ("hello")(Node ("cat") Empty (Node ("dog") Empty Empty))(Node ("world") (Node ("pig") Empty Empty) Empty)

-- foldr (+) 0 (map (\c-> if c==x then 1 else 0) s) --

howMany x "" = 0
howMany x (h:t)
 |x==h  = 1+ howMany x t
 |otherwise = howMany x t
 
countS Empty x = 0
countS (Node v l r) x = (howMany x v) + (countS l x) + (countS r x)