From a323eab8ab416119b207b482af226382182ac63f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 9 Feb 2018 14:21:53 +0100 Subject: [PATCH] Implement a class for a binary tree of integers. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- Arbres/ArbreBinaire.java | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/Arbres/ArbreBinaire.java b/Arbres/ArbreBinaire.java index 8365312..ca569c2 100644 --- a/Arbres/ArbreBinaire.java +++ b/Arbres/ArbreBinaire.java @@ -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); } -- 2.34.1