Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct Node {
- int valor;
- Node* left;
- Node* right;
- Node(int _valor) {
- valor = _valor;
- left = NULL;
- right = NULL;
- }
- };
- /*
- * todos los valores de los nodos son diferentes
- * assumir que quiero insertar un nuevo nodo
- * void insert(int valor, int valorFather, string lado)
- *
- * void insert(int valor, string direcciones)
- * direcciones = "LLRR"
- */
- //BST binary search tree - arbol binario de busqueda
- struct binaryTree {
- Node* root;
- int height;
- // asumo que lado puede tener cualquier valor
- // asumo que padre no tiene el hijo que quiero colocarle
- //
- void insertar(int valor, Node* father, string lado) { // es una funciona interna
- if(lado == "left") {
- father->left = new Node(valor); //(*father).left
- } if(lado == "right") {
- father->right = new Node(valor);
- } else {
- //imprimir un error o reportar un error
- }
- }
- void searchNode(Node* current, int valorFather, Node*& nodeFather) {
- if( current == NULL) {
- return;
- }
- if( current->valor == valorFather) {
- nodeFather = current;
- return;
- }
- searchNode(current->left, valorFather, nodeFather);
- searchNode(current->right, valorFather, nodeFather);
- }
- //siempre te dare un valor valido, caso contrario (otherwise), el arbol esta vacio
- void insert(int valor, int valorFather, string lado) { // solo usar esta
- Node* tempRoot = root;
- Node* nodeFather = NULL;
- searchNode(tempRoot, valorFather, nodeFather);
- if(nodeFather != NULL){ // cambio su valor
- insertar(valor, nodeFather, lado);
- }else {
- root = new Node(valorFather);
- }
- }
- void insert(int valor, string direcciones) { // y esta insert(5, "")
- }
- };
- int main() {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement