From: Jérôme Benoit Date: Fri, 9 Feb 2018 13:21:53 +0000 (+0100) Subject: Implement a class for a binary tree of integers. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a323eab8ab416119b207b482af226382182ac63f;p=TP_POO.git Implement a class for a binary tree of integers. Signed-off-by: Jérôme Benoit --- 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); }