Draw binary tree

0

I must make a binary tree that keeps people with their age. I already have the tree code keeping records but I need to show it in the following way (attached image):

This is the code I have done so far, thanks for your help.

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <iostream>


struct Nodo{
    char nombre[30];
    int edad;
    Nodo *der;
    Nodo *izq;
};

using namespace std;
//prototipos de funcion

Nodo *crearNodo(int,char[30]);
void insertarNodo(Nodo *&, int, char[30]);
void mostrarArbol(Nodo *, int);
void menu();

Nodo *arbol = NULL;


int main(int argc, char** argv) {
    menu();
    return 0;
}

//menu

void menu(){
    int ed, opcion, contador=0;
    char nom[30];
    do{
        cout<<"MENU"<<endl;
        cout<<"1. insertar un nuevo nodo"<<endl;
        cout<<"2.mostrar arbol"<<endl;
        cout<<"3.salir"<<endl;
        cout<<"opcion"<<endl;
        cin>>opcion;
        switch(opcion){
        case 1: cout<<"digite su edad"<<endl;
        cin>>ed;
        fflush(stdin);
        cout<<"digite su nombre"<<endl;
        cin.getline(nom,30);
        insertarNodo(arbol, ed, nom );
        cout<<"\n";
        system("pause");
        break;
        case 2: cout<<"mostrar el arbol\n\n";
        mostrarArbol(arbol,contador);
        cout<<"\n";
        system("pause");
        }
        system("cls");
    }while(opcion!=3);
}

Nodo *crearNodo(int ed, char nom[30]){
    Nodo *nuevo_nodo = new Nodo();
    nuevo_nodo->edad = ed;
    strcpy(nuevo_nodo->nombre,nom);
    nuevo_nodo->der = NULL;
    nuevo_nodo->izq = NULL;

    return nuevo_nodo;
}

void insertarNodo(Nodo *&arbol, int ed, char nom[30]){
    if(arbol == NULL){
        Nodo *nuevo_nodo = crearNodo(ed,nom);
        arbol = nuevo_nodo;
    }
    else{
        int valorRaiz = arbol->edad;
        if(ed<valorRaiz){
            insertarNodo(arbol->izq,ed,nom);
        }
        else{
            insertarNodo(arbol->der,ed,nom);
        }
    }
}


void mostrarArbol(Nodo *arbol, int cont){
    if(arbol==NULL){
        return;
    }
    else{
        mostrarArbol(arbol->der,cont+1);
        for(int i=0;i<cont;i++){
            cout<<" ";
        }
        cout<<arbol->edad<<endl;
        cout<<arbol->nombre<<endl;
        mostrarArbol(arbol->izq,cont+1);
    }
}
    
asked by Edwin Garcia 24.05.2018 в 22:07
source

0 answers