forked from karinaduque/M1-FT-AUX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestructuras.js
158 lines (125 loc) · 2.98 KB
/
estructuras.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function Queue() {
this.array = [];
}
Queue.prototype.enqueue = function(elemento) {
return this.array.push(elemento);
}
Queue.prototype.dequeue = function() {
return this.array.shift();
}
Queue.prototype.size = function() {
return this.array.length;
}
function Stack(){
this.array = [];
}
Stack.prototype.push = function(elemento){
this.array.push(elemento);
};
Stack.prototype.pop = function(){
return this.array.pop();
};
Stack.prototype.size = function () {
return this.array.length;
};
function LinkedList() {
this.head = null;
this.len = 0;
}
LinkedList.prototype.add = function(valor) {
var nuevoNodo = new Node(valor);
if(!this.head){
this.head = nuevoNodo;
} else {
var tailActual = this.head;
while (tailActual.next !== null) {
tailActual = tailActual.next;
}
tailActual.next = nuevoNodo;
}
this.len++;
}
LinkedList.prototype.remove = function() {
if(!this.head){
return undefined;
}
if(this.head.next === null){
var unicoNodo = this.head;
this.head = null;
return unicoNodo.value;
}
var nodoActual = this.head.next;
var nodoPrevious = this.head;
while (nodoActual.next !== null) {
nodoPrevious = nodoActual;
nodoActual = nodoActual.next;
}
nodoPrevious.next = null;
this.len--;
return nodoActual.value;
}
LinkedList.prototype.search = function(arg) {
var nodoActual = this.head;
if(nodoActual === null){
return null;
}
while (nodoActual !== null) {
if(typeof arg === "function"){
if(arg(nodoActual.value)){
return nodoActual.value;
}
} else if(nodoActual.value === arg){
return nodoActual.value;
}
nodoActual = nodoActual.next;
}
return null;
}
function Node(valor){
this.value = valor;
this.next = null;
}
function BinarySearchTree(valor) {
this.value = valor;
this.left = null;
this.right = null;
}
BinarySearchTree.prototype.insert = function(value) {
if(value < this.value){
if(this.left === null){
var newTree = new BinarySearchTree(value);
this.left = newTree;
} else {
this.left.insert(value);
}
} else {
if(this.right === null){
var newTree = new BinarySearchTree(value);
this.right = newTree;
} else {
this.right.insert(value);
}
}
}
BinarySearchTree.prototype.size = function() {
if(this.value === null){
return 0;
}
if(this.left === null && this.right === null){
return 1;
}
if(this.left === null){
return 1 + this.right.size();
}
if(this.right === null){
return 1 + this.left.size();
}
return 1 + this.left.size() + this.right.size();
}
module.exports = {
Queue,
Stack,
Node,
LinkedList,
BinarySearchTree
};