Advertisement
Guest User

Untitled

a guest
May 7th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.68 KB | None | 0 0
  1.  
  2.  
  3. type
  4.   NodeKind = enum opValue, opAdd, opSub, opMul, opCall
  5.   Node = ref object
  6.      case k: NodeKind
  7.      of opValue: value: int
  8.      of opAdd, opSub, opMul, opCall: kids: seq[Node]
  9.  
  10. proc safeLen(n: Node): int = if n.k == opValue: 0 else: n.kids.len
  11.  
  12. proc sameTree(a, b: Node): bool =
  13.   if a.k == b.k:
  14.     if a.k == opValue: result = a.value == b.value
  15.     elif a.kids.len == b.kids.len:
  16.       result = true
  17.       for i in 0..<a.kids.len:
  18.          if not sameTree(a.kids[i], b.kids[i]): return false
  19.  
  20. proc containsSubtree(n, subtree: Node): bool =
  21.   if sameTree(n, subtree): return true
  22.   for i in 0 .. <n.safeLen:
  23.     if n.kids[i].containsSubtree(subtree): return true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement