Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // todo dentro de la clase ABB
- public void insertarABB(int valor) {
- if (this.raiz == null) {
- this.raiz = new Nodo(valor);
- } else {
- agregarABB(this.raiz, valor);
- }
- }
- private void agregarABB(Nodo nodo, int valor) {
- if (nodo.dato > valor) {
- if (nodo.izq == null) {
- nodo.izq = new Nodo(valor);
- } else {
- agregarABB(nodo.izq, valor);
- }
- } else if (nodo.dato < valor) {
- if (nodo.der == null) {
- nodo.der = new Nodo(valor);
- } else {
- agregarABB(nodo.der, valor);
- }
- } else {
- throw new RuntimeException("El dato ya existe");
- }
- }
- private void ingresar (Nodo nodo){ // recorre el ab y los agrega ordenados
- if (nodo.izq != null){
- this.insertarABB(nodo.izq.dato);
- ingresar(nodo.izq);
- }
- if (nodo.der != null){
- this.insertarABB(nodo.der.dato);
- ingresar(nodo.der);
- }
- }
- public void toABB(AB arbol){
- if (arbol.raiz != null){
- this.insertarABB(arbol.raiz.dato);
- }
- this.ingresar(arbol.raiz); //le pasas la raiz para q empiece a recorrer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement