Escribí la clase pequeña a continuación para jugar con un árbol binario. ¿Es posible hacer una primera búsqueda en profundidad sin recursividad?
Parece que podría necesitar una cola para recordar dónde se encuentra en el recorrido, pero no estoy seguro.
¿Hay alguna manera de hacerlo sin agregar otra estructura de datos?
class Node { constructor(data) { this.left = null; this.right = null; this.data = data; } } class BinaryTree { constructor(data) { this.root = new Node(data); this.makeTree(); } makeTree(){ this.insert(15); this.insert(5); this.insert(7); this.insert(3); this.insert(20); this.insert(13); } insert(data) { let iter = this.root; let node = new Node(data); while(iter) { if(iter.left && (node.data < iter.data)){ iter = iter.left; } if(!iter.left && (node.data < iter.data)){ iter.left = node; return; } if(iter.right && (node.data >= iter.data)){ iter = iter.right; } if(!iter.right && (node.data >= iter.data)){ iter.right = node; return; } } } df() { function dfInner (node) { if(node === null) { return node; } // pre order dfInner(node.left); // in order dfInner(node.right); // post order } dfInner(this.root); } } const tree = new BinaryTree(10); tree.df();