Implement a class for a binary tree of integers.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Feb 2018 13:21:53 +0000 (14:21 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 9 Feb 2018 13:21:53 +0000 (14:21 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
Arbres/ArbreBinaire.java

index 836531272860dd88927bf4e84f1c848e62505fde..ca569c287d1f2d2423a7a342d0ecb7ba335ca073 100644 (file)
@@ -66,11 +66,89 @@ public class ArbreBinaire {
         return (getRootNode() == null);
     }
 
+    private IntNode inserer_rec(IntNode currentNode, int value) {
+        if (currentNode == null) {
+            return new IntNode(value);
+        } else if (value < currentNode.getData()) {
+            currentNode.setLeftNode(inserer_rec(currentNode.getLeftNode(), value));
+        } else if (value > currentNode.getData()) {
+            currentNode.setRightNode(inserer_rec(currentNode.getRightNode(), value));
+        } else {
+            /* equality */
+            return currentNode;
+        }
+        return currentNode;
+    }
+
     public void inserer(int value) {
+        setRootNode(inserer_rec(rootNode, value));
+    }
 
+    private IntNode supprimer_rec(IntNode currentNode, int value) {
+        if (currentNode == null) {
+            return null;
+        } else if (value == currentNode.getData()) {
+            if (currentNode.getLeftNode() == null && currentNode.getRightNode() == null) {
+                return null;
+            } else if (currentNode.getRightNode() == null) {
+                return currentNode.getLeftNode();
+            } else if (currentNode.getLeftNode() == null) {
+                return currentNode.getRightNode();
+            } else {
+                int smallestValue = findSmallestData(currentNode.getRightNode());
+                currentNode.setData(smallestValue);
+                currentNode.setRightNode(supprimer_rec(currentNode.getRightNode(), smallestValue));
+                return currentNode;
+            }
+        } else if (value < currentNode.getData()) {
+            currentNode.setLeftNode(supprimer_rec(currentNode.getLeftNode(), value));
+        } else {
+            currentNode.setRightNode(supprimer_rec(currentNode.getRightNode(), value));
+        }
+        return currentNode;
     }
 
     public void supprimer(int value) {
+        supprimer_rec(rootNode, value);
+    }
+
+    private boolean hasDataRec(IntNode currentNode, int value) {
+        if (currentNode == null) {
+            return false;
+        }
+        if (value == currentNode.getData()) {
+            return true;
+        }
+        return value < currentNode.getData() ? hasDataRec(currentNode.getLeftNode(), value) : hasDataRec(currentNode.getRightNode(), value);
+    }
+
+    public boolean hasData(int value) {
+        return hasDataRec(rootNote, value);
+    }
+
+    private int findSmallestData(IntNode node) {
+        return node.getLeftNode() == null ? node.getData() : findSmallestData(node.getLeftNode());
+    }
+
+    public afficher_rec(IntNode currentNode) {
+        if (currentNode.getLeftNode() != null) {
+            afficher_rec(currentNode.getLeftNode());
+        } else if (currentNode.getRightNode() != null) {
+            afficher_rec(currentNode.getRightNode());
+        }
+        System.out.println("Valeur dans le noeud : ");
+    }
+
+    public static void main(String[] args) {
+        ArbreBinaire Btree = new ArbreBinaire();
+
+        Btree.inserer(5);
+        Btree.inserer(2);
+        Btree.inserer(7);
+        Btree.inserer(1);
+
+        String Btreeout = Btree.toString();
+        System.out.println(Btreeout);
 
     }