Advertisement
Guest User

Untitled

a guest
May 22nd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.00 KB | None | 0 0
  1. let words = "aabbccdd"
  2. let listChars = Seq.toList words
  3.  
  4. let rec cortMatch headCort tailCort el =
  5.     match tailCort with
  6.     |hd::tl ->
  7.         let symb, counter = hd
  8.         if symb = el
  9.             then
  10.                 let counter' = counter + 1
  11.                let newCort = (symb, counter')
  12.                 headCort @ [newCort] @ tl
  13.             else
  14.                 let headCort' = headCort @ [hd]
  15.                cortMatch headCort' tl el
  16.     |_ -> headCort @ [(el, 1)]
  17.  
  18. let rec charsToCorts listCh listCort =
  19.     match listCh with
  20.     |hd::tl ->
  21.         let listCort' = cortMatch [] listCort hd
  22.        charsToCorts tl listCort'
  23.     |_ -> listCort
  24.  
  25. let cortsList = charsToCorts listChars []
  26.  
  27. //Переводим список кортежей в деревья
  28. type BinaryTree =
  29. | Node of int * BinaryTree * BinaryTree
  30. | Leaf of int * char
  31. | Empty
  32.  
  33. let rec cortsToTrees cortsList treesList =
  34.     match cortsList with
  35.     |hd::tl ->
  36.         let (symb, value) = hd
  37.         let tree = Leaf (value, symb)
  38.         cortsToTrees tl treesList @ [tree]
  39.     |_ -> treesList
  40.  
  41. let treesList = cortsToTrees cortsList []
  42.  
  43. open System
  44. printfn "Before sorting: "
  45. treesList |> printfn "%A"
  46. let sortFunction (t1:BinaryTree) (t2:BinaryTree) =    
  47.     let val1 =
  48.         match t1 with
  49.         |Node (value, _, _) -> value
  50.         |Leaf (value, string) -> value
  51.         |_ -> 0
  52.  
  53.     let val2 =
  54.         match t2 with
  55.         |Node (value, _, _) -> value
  56.         |Leaf (value, string) -> value
  57.         |_ -> 0
  58.  
  59.     if (val1 > val2) then
  60.        1
  61.     else -1
  62. let sortedTreeList = List.sortWith sortFunction treesList
  63. printfn "After sorting:\n%A" sortedTreeList
  64.  
  65. let combinateTrees tree1 tree2 =
  66.     let val1 =
  67.         match tree1 with
  68.         |Node (value, _, _) -> value
  69.         |Leaf (value, string) -> value
  70.         |_ -> 0
  71.     let val2 =
  72.         match tree2 with
  73.         |Node (value, _, _) -> value
  74.         |Leaf (value, string) -> value
  75.         |_ -> 0
  76.     Node (val1 + val2, tree1, tree2)
  77. //не проверено
  78.  
  79. let rec creatingMainTree list =
  80.     match list with
  81.     |fT::tailfT ->
  82.         match tailfT with
  83.         |sT::tailsT ->
  84.             let tree = combinateTrees fT sT
  85.             let sortedTreeList = List.sortWith sortFunction ([tree] @ tailsT)
  86.             creatingMainTree sortedTreeList
  87.         |_ -> fT
  88.     |_ -> Node (0, Empty, Empty)
  89.  
  90. let mainTree = creatingMainTree sortedTreeList
  91.  
  92. printf "\n%A" mainTree
  93. //самое ппечальное
  94. let rec printInOrder tree key codeList =
  95.     match tree with
  96.     |Node (value, left, right) ->
  97.         printInOrder left (key @ [1]) codeList
  98.         printInOrder right (key @ [0]) codeList
  99.     |Leaf (value, symb) ->
  100.         printfn "Symbol '%c' %A" symb key
  101.         let codeList' = codeList @ [(symb, key)]
  102.        if treesList.Length = codeList'.Length then codeList'
  103.    |Empty ->
  104.        if treesList.Length = codeList.Length then codeList
  105.  
  106. printf "\n"
  107. let a = printInOrder mainTree [] []
  108.  
  109. System.Console.ReadKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement