Guest User

Untitled

a guest
May 28th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. Пример реализвации АТД дерево на языке С++
  2.  
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6. #include "tree.hh"
  7.  
  8. using namespace std;
  9.  
  10. int main(int, char **)
  11. {
  12. tree<string> tr;
  13. tree<string>::iterator top, one, two, loc, banana;
  14.  
  15. top=tr.begin();
  16. one=tr.insert(top, "one");
  17. two=tr.append_child(one, "two");
  18. tr.append_child(two, "apple");
  19. banana=tr.append_child(two, "banana");
  20. tr.append_child(banana,"cherry");
  21. tr.append_child(two, "peach");
  22. tr.append_child(one,"three");
  23.  
  24. loc=find(tr.begin(), tr.end(), "two");
  25. if(loc!=tr.end()) {
  26. tree<string>::sibling_iterator sib=tr.begin(loc);
  27. while(sib!=tr.end(loc)) {
  28. cout << (*sib) << endl;
  29. ++sib;
  30. }
  31. cout << endl;
  32. tree<string>::iterator sib2=tr.begin(loc);
  33. tree<string>::iterator end2=tr.end(loc);
  34. while(sib2!=end2) {
  35. for(int i=0; i<tr.depth(sib2)-2; ++i)
  36. cout << " ";
  37. cout << (*sib2) << endl;
  38. ++sib2;
  39. }
  40. }
  41. }
  42.  
  43.  
  44.  
  45. Пример реализвации АТД дерево на языке Python
  46.  
  47.  
  48.  
  49. >>> T = [["a", "b"], ["c"], ["d", ["e", "f"]]]
  50. >>> T[0][1]
  51. >>> T[2][1][0]
  52. class Tree:
  53. def __init__(self, left, right):
  54. self.left = left
  55. self.right = right
  56. >>> t = Tree(Tree("a", "b"), Tree("c", "d"))
  57. >>> t.right.left
  58. >>> t = Tree(Tree("a", Tree("b", Tree("c", Tree("d")))))
  59. >>> t.kids.next.next.val
  60.  
  61.  
  62.  
  63. Пример реализвации АТД дерево на языке Java
  64.  
  65.  
  66.  
  67.  
  68. public class BSTree<T1 extends Comparable<T1>, T2> {
  69. static class Node<T1, T2> {
  70. T1 key;
  71. T2 value;
  72. Node<T1, T2> left, right;
  73.  
  74. Node(T1 key, T2 value) {
  75. this.key = key;
  76. this.value = value;
  77. }
  78. }
  79. private Node<T1, T2> root = null;
  80.  
  81. public boolean containsKey(T1 k) {
  82. Node<T1, T2> x = root;
  83. while (x != null) {
  84. int cmp = k.compareTo(x.key);
  85. if (cmp == 0) {
  86. return true;
  87. }
  88. if (cmp < 0) {
  89. x = x.left;
  90. } else {
  91. x = x.right;
  92. }
  93. }
  94. return false;
  95. }
  96.  
  97. public T2 get(T1 k) {
  98. Node<T1, T2> x = root;
  99. while (x != null) {
  100. int cmp = k.compareTo(x.key);
  101. if (cmp == 0) {
  102. return x.value;
  103. }
  104. if (cmp < 0) {
  105. x = x.left;
  106. } else {
  107. x = x.right;
  108. }
  109. }
  110. return null;
  111. }
  112.  
  113. public void add(T1 k, T2 v) {
  114. Node<T1, T2> x = root, y = null;
  115. while (x != null) {
  116. int cmp = k.compareTo(x.key);
  117. if (cmp == 0) {
  118. x.value = v;
  119. return;
  120. } else {
  121. y = x;
  122. if (cmp < 0) {
  123. x = x.left;
  124. } else {
  125. x = x.right;
  126. }
  127. }
  128. }
  129. Node<T1, T2> newNode = new Node<T1, T2>(k, v);
  130. if (y == null) {
  131. root = newNode;
  132. } else {
  133. if (k.compareTo(y.key) < 0) {
  134. y.left = newNode;
  135. } else {
  136. y.right = newNode;
  137. }
  138. }
  139. }
  140.  
  141. public void remove(T1 k) {
  142. Node<T1, T2> x = root, y = null;
  143. while (x != null) {
  144. int cmp = k.compareTo(x.key);
  145. if (cmp == 0) {
  146. break;
  147. } else {
  148. y = x;
  149. if (cmp < 0) {
  150. x = x.left;
  151. } else {
  152. x = x.right;
  153. }
  154. }
  155. }
  156. if (x == null) {
  157. return;
  158. }
  159. if (x.right == null) {
  160. if (y == null) {
  161. root = x.left;
  162. } else {
  163. if (x == y.left) {
  164. y.left = x.left;
  165. } else {
  166. y.right = x.left;
  167. }
  168. }
  169. } else {
  170. Node<T1, T2> leftMost = x.right;
  171. y = null;
  172. while (leftMost.left != null) {
  173. y = leftMost;
  174. leftMost = leftMost.left;
  175. }
  176. if (y != null) {
  177. y.left = leftMost.right;
  178. } else {
  179. x.right = leftMost.right;
  180. }
  181. x.key = leftMost.key;
  182. x.value = leftMost.value;
  183. }
  184. }
  185. }
  186.  
  187.  
  188.  
  189.  
  190. Пример реализвации АТД дерево на языке Haskell
  191.  
  192.  
  193.  
  194. data Ord key => BSTree key value = Leaf | Branch key value (BSTree key value) (BSTree key value)
  195. deriving (Show)
  196.  
  197. containsKey :: Ord key => BSTree key value -> key -> Bool
  198. containsKey Leaf _ = False
  199. containsKey (Branch key _ left right) k
  200. | k < key = containsKey left k
  201. | k > key = containsKey right k
  202. | k == key = True
  203.  
  204. get :: Ord key => BSTree key value -> key -> Maybe value
  205. get Leaf _ = Nothing
  206. get (Branch key value left right) k
  207. | k < key = get left k
  208. | k > key = get right k
  209. | k == key = Just value
  210.  
  211. add :: Ord key => BSTree key value -> key -> value -> BSTree key value
  212. add Leaf k v = Branch k v Leaf Leaf
  213. add (Branch key value left right) k v
  214. | k < key = Branch key value (add left k v) right
  215. | k > key = Branch key value left (add right k v)
  216. | k == key = Branch key value left right
  217.  
  218. remove :: Ord key => BSTree key value -> key -> BSTree key value
  219. remove Leaf _ = Leaf
  220. remove (Branch key value left right) k
  221. | k < key = Branch key value (remove left k) right
  222. | k > key = Branch key value left (remove right k)
  223. | k == key = if isLeaf right
  224. then left
  225. else Branch leftmostA leftmostB left right'
  226. where
  227. isLeaf Leaf = True
  228. isLeaf _ = False
  229. ((leftmostA, leftmostB), right') = deleteLeftmost right
  230. deleteLeftmost (Branch key value Leaf right) = ((key, value), right)
  231. deleteLeftmost (Branch key value left right) = (pair, Branch key value left' right)
  232. where (pair, left') = deleteLeftmost left
  233.  
  234. fromList :: Ord key => [(key, value)] -> BSTree key value
  235. fromList = foldr (\(key, value) tree -> add tree key value) Leaf
  236.  
  237. toList :: Ord key => BSTree key value -> [(key, value)]
  238. toList tree = toList' tree []
  239. where
  240. toList' Leaf list = list
  241. toList' (Branch key value left right) list = toList' left ((key, value):(toList' left list))
Add Comment
Please, Sign In to add comment