The API for each Cedge class.
- AVL
- Deque
- Heap
- Queue
- Stack
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.
- 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 than0
to sort a before b, equal to0
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 return0
unless you can return the original's value in place of the equivalent's value), or greater than0
to sort b before a. The default comparators are"number"
which isa - b
and"string"
which isa.localeCompare(b)
.
- val
*
void
This method creates a new AVL instance and copies the entire existing state to the new instance.
!AVL
The new AVL instance with the copied state is returned.
This method returns the count of val existing within the tree.
- val
*
number
- 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).
boolean
The return value indicates whether val was found within the tree.
This method reports whether the tree is empty.
boolean
If the tree is empty true
is returned. Otherwise false
is returned.
- val
*
boolean
This method returns an inorder-sorted array of the tree's values.
- 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.
!Array<*>
This method gets the current length for the AVL instance.
number
This method returns the value of the next (i.e. greater) existing value within the tree.
- val
*
*
Returns the next value in the tree or undefined
if no such value exists.
This method returns a postorder-sorted array of the tree's values.
- 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.
!Array<*>
This method returns a preorder-sorted array of the tree's values.
- 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.
!Array<*>
This method returns the value of the previous (i.e. lesser) existing value within the tree.
- val
*
*
Returns the previous value in the tree or undefined
if no such value exists.
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.
- 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.
This method gets the value for the tail of the Deque instance.
*
This method creates a new Deque instance and copies the entire existing state to the new instance.
!Deque
The new Deque instance with the copied state is returned.
This method reports whether the deque is empty.
boolean
If the deque is empty true
is returned. Otherwise false
is returned.
This method gets the value for the head of the Deque instance.
*
This method gets the current length for the Deque instance.
number
This method gets the maximum length for the Deque instance.
number
*
- val
*
void
*
- val
*
void
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))
.
- 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 to0
to sort a before b and a number greater than0
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 isa - b
,"max-number"
which isb - a
,"min-string"
which isa.localeCompare(b)
, and"max-string"
which isb.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).
This method creates a new Heap instance and copies the entire existing state to the new instance.
!Heap
The new Heap instance with the copied state is returned.
This method reports whether the heap is empty.
boolean
If the heap is empty true
is returned. Otherwise false
is returned.
This method gets the current length of the Heap instance.
number
This method gets the maximum length of the Heap instance.
number
*
- val
*
void
*
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.
- vals
!Array<*>
optional default =[]
This method gets the value for the tail of the Queue instance.
*
This method creates a new Queue instance and copies the entire existing state to the new instance.
!Queue
The new Queue instance with the copied state is returned.
This method reports whether the queue is empty.
boolean
If the queue is empty true
is returned. Otherwise false
is returned.
This method gets the value for the head of the Queue instance.
*
This method gets the current length of the Queue instance.
number
*
- val
*
void
*
- val
*
void
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).
- vals
!Array<*>
optional default =[]
This method creates a new Stack instance and copies the entire existing state to the new instance.
!Stack
The new Stack instance with the copied state is returned.
This method reports whether the stack is empty.
boolean
If the stack is empty true
is returned. Otherwise false
is returned.
This method gets the current length of the stack.
number
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.
*
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.
This method adds a value to the top of the stack.
- val
*
void
This method gets the value at the top of the stack (i.e. the last value pushed to the stack).
*