Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6. int valor;
  7. Node* left;
  8. Node* right;
  9.  
  10. Node(int _valor) {
  11. valor = _valor;
  12. left = NULL;
  13. right = NULL;
  14. }
  15. };
  16.  
  17. /*
  18. * todos los valores de los nodos son diferentes
  19. * assumir que quiero insertar un nuevo nodo
  20. * void insert(int valor, int valorFather, string lado)
  21. *
  22. * void insert(int valor, string direcciones)
  23. * direcciones = "LLRR"
  24. */
  25.  
  26. //BST binary search tree - arbol binario de busqueda
  27.  
  28. struct binaryTree {
  29. Node* root;
  30. int height;
  31. // asumo que lado puede tener cualquier valor
  32. // asumo que padre no tiene el hijo que quiero colocarle
  33. //
  34. void insertar(int valor, Node* father, string lado) { // es una funciona interna
  35. if(lado == "left") {
  36. father->left = new Node(valor); //(*father).left
  37. } if(lado == "right") {
  38. father->right = new Node(valor);
  39. } else {
  40. //imprimir un error o reportar un error
  41. }
  42. }
  43.  
  44. void searchNode(Node* current, int valorFather, Node*& nodeFather) {
  45. if( current == NULL) {
  46. return;
  47. }
  48.  
  49. if( current->valor == valorFather) {
  50. nodeFather = current;
  51. return;
  52. }
  53.  
  54. searchNode(current->left, valorFather, nodeFather);
  55. searchNode(current->right, valorFather, nodeFather);
  56. }
  57. //siempre te dare un valor valido, caso contrario (otherwise), el arbol esta vacio
  58. void insert(int valor, int valorFather, string lado) { // solo usar esta
  59. Node* tempRoot = root;
  60. Node* nodeFather = NULL;
  61. searchNode(tempRoot, valorFather, nodeFather);
  62.  
  63. if(nodeFather != NULL){ // cambio su valor
  64. insertar(valor, nodeFather, lado);
  65. }else {
  66. root = new Node(valorFather);
  67. }
  68.  
  69.  
  70. }
  71.  
  72. void insert(int valor, string direcciones) { // y esta insert(5, "")
  73. }
  74. };
  75.  
  76. int main() {
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement