From: Jérôme Benoit Date: Fri, 4 May 2018 09:02:10 +0000 (+0200) Subject: Create a generic Node class and make use of it. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ae33f914c6b8f939fcef9589c0f14bec2ba37e6b;p=TP_POO.git Create a generic Node class and make use of it. Signed-off-by: Jérôme Benoit --- diff --git a/Arbres/ArbreBinaire.java b/Arbres/ArbreBinaire.java index fc19203..89fccc7 100644 --- a/Arbres/ArbreBinaire.java +++ b/Arbres/ArbreBinaire.java @@ -4,61 +4,17 @@ * A binary tree is a ordered value tree with only two childs per node */ public class ArbreBinaire { - - private class IntNode { - private int data; - private IntNode leftIntNode; - private IntNode rightIntNode; - - IntNode(int value) { - setData(value); - setLeftNode(null); - setRightNode(null); - } - - IntNode(int value, IntNode leftNode, IntNode rightNode) { - setData(value); - setLeftNode(leftNode); - setRightNode(rightNode); - } - - private int getData() { - return data; - } - - private void setData(int value) { - data = value; - } - - private IntNode getLeftNode() { - return leftIntNode; - } - - private void setLeftNode(IntNode leftNode) { - leftIntNode = leftNode; - } - - private IntNode getRightNode() { - return rightIntNode; - } - - private void setRightNode(IntNode rightNode) { - rightIntNode = rightNode; - } - - } - - private IntNode rootNode; + private Node rootNode; ArbreBinaire() { setRootNode(null); } - private void setRootNode(IntNode node) { + private void setRootNode(Node node) { rootNode = node; } - private IntNode getRootNode() { + private Node getRootNode() { return rootNode; } @@ -66,9 +22,9 @@ public class ArbreBinaire { return (getRootNode() == null); } - private IntNode inserer_rec(IntNode currentNode, int value) { + private Node inserer_rec(Node currentNode, int value) { if (currentNode == null) { - return new IntNode(value); + return new Node(value); } if (value < currentNode.getData()) { currentNode.setLeftNode(inserer_rec(currentNode.getLeftNode(), value)); @@ -82,7 +38,7 @@ public class ArbreBinaire { setRootNode(inserer_rec(rootNode, value)); } - private IntNode supprimer_rec(IntNode currentNode, int value) { + private Node supprimer_rec(Node currentNode, int value) { if (currentNode == null) { return null; } @@ -117,7 +73,7 @@ public class ArbreBinaire { supprimer_rec(rootNode, value); } - private boolean hasDataRec(IntNode currentNode, int value) { + private boolean hasDataRec(Node currentNode, int value) { if (currentNode == null) { return false; } @@ -131,11 +87,11 @@ public class ArbreBinaire { return hasDataRec(rootNode, value); } - private int findSmallestData(IntNode node) { + private int findSmallestData(Node node) { return node.getLeftNode() == null ? node.getData() : findSmallestData(node.getLeftNode()); } - private void afficher_rec(IntNode currentNode) { + private void afficher_rec(Node currentNode) { if (currentNode != null) { afficher_rec(currentNode.getLeftNode()); System.out.print(currentNode.getData() + " "); @@ -148,7 +104,7 @@ public class ArbreBinaire { System.out.println(); } - private void afficher_arbre_rec(IntNode currentNode, int column) { + private void afficher_arbre_rec(Node currentNode, int column) { if (currentNode != null) { afficher_arbre_rec(currentNode.getRightNode(), column + 1); for (int i = 0; i < column; i++) { diff --git a/Arbres/Makefile b/Arbres/Makefile index 0d2af56..7ca90fe 100644 --- a/Arbres/Makefile +++ b/Arbres/Makefile @@ -46,7 +46,8 @@ JVM = java # NAME = Camilo Juan CLASSES = \ - ArbreBinaire.java + Node.java \ + ArbreBinaire.java # # MAIN is a variable with the name of the file containing the main method diff --git a/Arbres/Node.java b/Arbres/Node.java new file mode 100644 index 0000000..1e8dd01 --- /dev/null +++ b/Arbres/Node.java @@ -0,0 +1,42 @@ +public class Node { + private E data; + private Node leftNode; + private Node rightNode; + + Node(E value) { + setData(value); + setLeftNode(null); + setRightNode(null); + } + + Node(E value, Node leftNode, Node rightNode) { + setData(value); + setLeftNode(leftNode); + setRightNode(rightNode); + } + + public E getData() { + return data; + } + + public void setData(E value) { + data = value; + } + + public Node getLeftNode() { + return leftNode; + } + + public void setLeftNode(Node Node) { + leftNode = Node; + } + + public Node getRightNode() { + return rightNode; + } + + public void setRightNode(Node Node) { + rightNode = Node; + } + +}