Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active June 13, 2017 05:50
Show Gist options
  • Select an option

  • Save abiodun0/84bcf2842313a019683c3af71bb6e5aa to your computer and use it in GitHub Desktop.

Select an option

Save abiodun0/84bcf2842313a019683c3af71bb6e5aa to your computer and use it in GitHub Desktop.
linkedList and tress functional
const Nil = {};
function _LinkedList(h, t) {
this.head = h;
this.tail = t;
}
const LinkedList = (h, t) => new _LinkedList(h, t);
const newLinkedList = LinkedList(5, LinkedList(7, LinkedList(10, Nil)))
const Empty = {};
function _Leaf(x) { this.x = x}
const Leaf = (x) => new _Leaf(x)
function _Node(l, x, r) {
this.left = l;
this.x = x;
this.right = r;
}
const Node = (l,x,r) => new _Node(l,x,r)
const newTree = Node(Node(Leaf(3), 1, Leaf(2)), 1, Node(Leaf(4), 1, Leaf(7)))
console.log(newTree, 'trees')
const newTree2 = Node(Node(Node(Leaf(4)), 2), 1, Node(Node(Leaf(4)), 3, Node(undefined, 6, Leaf(7))))
console.log(JSON.stringify(newTree2, null, 4), 'second Tree');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment