Skip to content

Latest commit

 

History

History
620 lines (405 loc) · 16 KB

API.md

File metadata and controls

620 lines (405 loc) · 16 KB

Cedge API

The API for each Cedge class.

AVL.prototype.constructor

The AVL constructor. This is a self-balancing binary search tree. You may provide an array of initial values. You may provide a custom comparator method.

Parameters

  • vals !Array<*> optional default = []
    If vals is type "function" or "string" the compare parameter is set to vals, and vals is set to [].
  • compare (!function(*, *): number)|string optional default = "number"
    The method used to determine the order of the values. Must return a number less than 0 to sort a before b, equal to 0 to increase the count of the existing value (i.e. duplicate values are not saved directly to the tree; instead they increase an internal counter saved within the original value's node; so do not return 0 unless you can return the original's value in place of the equivalent's value), or greater than 0 to sort b before a. The default comparators are "number" which is a - b and "string" which is a.localeCompare(b).

AVL.prototype.add

Parameters

  • val *

Returns

void

AVL.prototype.clone

This method creates a new AVL instance and copies the entire existing state to the new instance.

Returns

!AVL
The new AVL instance with the copied state is returned.

AVL.prototype.count

This method returns the count of val existing within the tree.

Parameters

  • val *

Returns

number

AVL.prototype.delete

Parameters

  • val *
  • all boolean optional default = false
    Denote whether you want to delete one occurrence of val or all occurrences of val (i.e. do you want to delete the duplicates).

Returns

boolean
The return value indicates whether val was found within the tree.

AVL.prototype.empty

This method reports whether the tree is empty.

Returns

boolean
If the tree is empty true is returned. Otherwise false is returned.

AVL.prototype.has

Parameters

  • val *

Returns

boolean

AVL.prototype.inorder

This method returns an inorder-sorted array of the tree's values.

Parameters

  • dups boolean optional default = true
    Denote whether you want duplicate values to be added to the resulting array. Note that duplicate values are added adjacent to each other since they represent one node.

Returns

!Array<*>

AVL.prototype.length

This method gets the current length for the AVL instance.

Returns

number

AVL.prototype.next

This method returns the value of the next (i.e. greater) existing value within the tree.

Parameters

  • val *

Returns

*
Returns the next value in the tree or undefined if no such value exists.

AVL.prototype.postorder

This method returns a postorder-sorted array of the tree's values.

Parameters

  • dups boolean optional default = true
    Denote whether you want duplicate values to be added to the resulting array. Note that duplicate values are added adjacent to each other since they represent one node.

Returns

!Array<*>

AVL.prototype.preorder

This method returns a preorder-sorted array of the tree's values.

Parameters

  • dups boolean optional default = true
    Denote whether you want duplicate values to be added to the resulting array. Note that duplicate values are added adjacent to each other since they represent one node.

Returns

!Array<*>

AVL.prototype.previous

This method returns the value of the previous (i.e. lesser) existing value within the tree.

Parameters

  • val *

Returns

*
Returns the previous value in the tree or undefined if no such value exists.

Deque.prototype.constructor

The Deque constructor. This is a double-ended queue. You may provide an array of values which it pushes from index zero (i.e. index zero is the head) to the deque. If the maxLength parameter is defined the length of the deque is limited to maxLength. If limited the deque will purge the tail once the length of the deque exceeds the maxLength.

Parameters

  • vals !Array<*> optional default = []
    If vals is type "number" the maxLength parameter is set to vals, and vals is set to [].
  • maxLength number optional default = Infinity
    maxLength is the maximum values the deque can hold. It will cut the tail before adding a new value when the maximum length is reached.

Deque.prototype.back

This method gets the value for the tail of the Deque instance.

Returns

*

Deque.prototype.clone

This method creates a new Deque instance and copies the entire existing state to the new instance.

Returns

!Deque
The new Deque instance with the copied state is returned.

Deque.prototype.empty

This method reports whether the deque is empty.

Returns

boolean
If the deque is empty true is returned. Otherwise false is returned.

Deque.prototype.front

This method gets the value for the head of the Deque instance.

Returns

*

Deque.prototype.length

This method gets the current length for the Deque instance.

Returns

number

Deque.prototype.maxLength

This method gets the maximum length for the Deque instance.

Returns

number

Deque.prototype.pop

Returns

*

Deque.prototype.push

Parameters

  • val *

Returns

void

Deque.prototype.shift

Returns

*

Deque.prototype.unshift

Parameters

  • val *

Returns

void

Heap.prototype.constructor

The Heap constructor. You may provide an unsorted array of values which it clones and then heapifies in linear time (faster than individually pushing each value into an empty heap which takes O(n*log(n)) time). You may provide a custom comparator method. You may provide a limit for the length of Heap. If a limit is provided the heap will only keep the lowest maxLength count of the values provided and will only add new values that would sort lower than the top value. Note that limiting the heap increases the constructor's time complexity to O((n-maxLength)*log(n)).

Parameters

  • vals !Array<*> optional default = []
    If vals is type "function" or "string" the compare parameter is set to vals, and vals is set to []. If vals is type "number" the maxLength parameter is set to vals, and vals is set to [].
  • compare (!function(*, *): number)|string optional default = "min-number"
    If compare is type "number" the maxLength parameter is set to compare, and compare is set to "number". compare is the method used to determine the order of the values. It must return a number less than or equal to 0 to sort a before b and a number greater than 0 to sort b before a. If maxLength is set compare decides if a value can stay or must go (the top value(s) is always cut). The default comparators are "min-number" which is a - b, "max-number" which is b - a, "min-string" which is a.localeCompare(b), and "max-string" which is b.localeCompare(a).
  • maxLength number optional default = Infinity
    maxLength is the maximum values the heap can hold. It will only keep the lowest values within the heap (i.e. the top is cut).

Heap.prototype.clone

This method creates a new Heap instance and copies the entire existing state to the new instance.

Returns

!Heap
The new Heap instance with the copied state is returned.

Heap.prototype.empty

This method reports whether the heap is empty.

Returns

boolean
If the heap is empty true is returned. Otherwise false is returned.

Heap.prototype.length

This method gets the current length of the Heap instance.

Returns

number

Heap.prototype.maxLength

This method gets the maximum length of the Heap instance.

Returns

number

Heap.prototype.pop

Returns

*

Heap.prototype.push

Parameters

  • val *

Returns

void

Heap.prototype.top

Returns

*

Queue.prototype.constructor

The Queue constructor (named MyQueue when copying for competitive programming use to avoid a name collision with LeetCode's public Queue class). You may provide an array of values which it pushes from index zero (i.e. index zero is the head) to the queue. Note that Queue.prototype.pop has a time complexity of O(n). Use Deque to get a time complexity of O(1) for queue.pop.

Parameters

  • vals !Array<*> optional default = []

Queue.prototype.back

This method gets the value for the tail of the Queue instance.

Returns

*

Queue.prototype.clone

This method creates a new Queue instance and copies the entire existing state to the new instance.

Returns

!Queue
The new Queue instance with the copied state is returned.

Queue.prototype.empty

This method reports whether the queue is empty.

Returns

boolean
If the queue is empty true is returned. Otherwise false is returned.

Queue.prototype.front

This method gets the value for the head of the Queue instance.

Returns

*

Queue.prototype.length

This method gets the current length of the Queue instance.

Returns

number

Queue.prototype.pop

Returns

*

Queue.prototype.push

Parameters

  • val *

Returns

void

Queue.prototype.shift

Returns

*

Queue.prototype.unshift

Parameters

  • val *

Returns

void

Stack.prototype.constructor

The Stack constructor. You may provide an array of values to initiate the Stack instance with. If you do provide initial values they are pushed to the stack in ascending order (i.e. the last value in the array is the top value).

Parameters

  • vals !Array<*> optional default = []

Stack.prototype.clone

This method creates a new Stack instance and copies the entire existing state to the new instance.

Returns

!Stack
The new Stack instance with the copied state is returned.

Stack.prototype.empty

This method reports whether the stack is empty.

Returns

boolean
If the stack is empty true is returned. Otherwise false is returned.

Stack.prototype.length

This method gets the current length of the stack.

Returns

number

Stack.prototype.pop

This method removes the value at the top of the stack (i.e. the last value pushed to the stack) and returns the removed value.

Returns

*
The value at the top of the stack (i.e. the last value pushed to the stack). If the stack is empty undefined is returned.

Stack.prototype.push

This method adds a value to the top of the stack.

Parameters

  • val *

Returns

void

Stack.prototype.top

This method gets the value at the top of the stack (i.e. the last value pushed to the stack).

Returns

*