From 5b834ba1cac88e2d82f3f7c0abc520763ae4ecd3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 9 Feb 2018 19:18:22 +0100 Subject: [PATCH] Fix all remaining bugs in the binary tree code. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- Arbres/ArbreBinaire.java | 61 +++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/Arbres/ArbreBinaire.java b/Arbres/ArbreBinaire.java index ca569c2..a99b47a 100644 --- a/Arbres/ArbreBinaire.java +++ b/Arbres/ArbreBinaire.java @@ -73,10 +73,7 @@ public class ArbreBinaire { currentNode.setLeftNode(inserer_rec(currentNode.getLeftNode(), value)); } else if (value > currentNode.getData()) { currentNode.setRightNode(inserer_rec(currentNode.getRightNode(), value)); - } else { - /* equality */ - return currentNode; - } + } /* skip the equality case */ return currentNode; } @@ -95,6 +92,12 @@ public class ArbreBinaire { } else if (currentNode.getLeftNode() == null) { return currentNode.getRightNode(); } else { + /* + * First, we need to find the node that will replace the deleted node. + * We’ll use the smallest node of the node to be deleted’s right sub-tree. + * Then, we assign the smallest value to the node to delete and after that, + * we’ll delete it from the right subtree. + */ int smallestValue = findSmallestData(currentNode.getRightNode()); currentNode.setData(smallestValue); currentNode.setRightNode(supprimer_rec(currentNode.getRightNode(), smallestValue)); @@ -123,33 +126,65 @@ public class ArbreBinaire { } public boolean hasData(int value) { - return hasDataRec(rootNote, value); + return hasDataRec(rootNode, value); } private int findSmallestData(IntNode node) { return node.getLeftNode() == null ? node.getData() : findSmallestData(node.getLeftNode()); } - public afficher_rec(IntNode currentNode) { - if (currentNode.getLeftNode() != null) { + private void afficher_rec(IntNode currentNode) { + if (currentNode != null) { afficher_rec(currentNode.getLeftNode()); - } else if (currentNode.getRightNode() != null) { + System.out.print(currentNode.getData() + " "); afficher_rec(currentNode.getRightNode()); } - System.out.println("Valeur dans le noeud : "); + } + + public void afficher() { + afficher_rec(rootNode); + System.out.println(); + } + + private void afficher_arbre_rec(IntNode currentNode, int column) { + int i; + + if (currentNode != null) { + afficher_arbre_rec(currentNode.getRightNode(), column + 1); + for (i = 0; i < column; i++) { + System.out.print(" "); + } + System.out.println(currentNode.getData()); + afficher_arbre_rec(currentNode.getLeftNode(), column + 1); + } + } + + public void afficher_arbre() { + afficher_arbre_rec(rootNode, 1); } public static void main(String[] args) { ArbreBinaire Btree = new ArbreBinaire(); - Btree.inserer(5); Btree.inserer(2); - Btree.inserer(7); + Btree.inserer(6); + Btree.inserer(4); + Btree.inserer(5); Btree.inserer(1); + Btree.inserer(0); + + Btree.afficher(); + Btree.afficher_arbre(); + + Btree.supprimer(4); + + Btree.afficher(); + Btree.afficher_arbre(); - String Btreeout = Btree.toString(); - System.out.println(Btreeout); + Btree.supprimer(6); + Btree.afficher(); + Btree.afficher_arbre(); } } -- 2.34.1