Create a generic Node class and make use of it.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 4 May 2018 09:02:10 +0000 (11:02 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 4 May 2018 09:02:10 +0000 (11:02 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
Arbres/ArbreBinaire.java
Arbres/Makefile
Arbres/Node.java [new file with mode: 0644]

index fc19203a7e4b9025a49e3628be08079abc12629e..89fccc761ccfcfe98c352e1c027e9e5c25c00cb9 100644 (file)
@@ -4,61 +4,17 @@
  * A binary tree is a ordered value tree with only two childs per node
  */
 public class ArbreBinaire {
  * 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<Integer> rootNode;
 
     ArbreBinaire() {
         setRootNode(null);
     }
 
 
     ArbreBinaire() {
         setRootNode(null);
     }
 
-    private void setRootNode(IntNode node) {
+    private void setRootNode(Node<Integer> node) {
         rootNode = node;
     }
 
         rootNode = node;
     }
 
-    private IntNode getRootNode() {
+    private Node<Integer> getRootNode() {
         return rootNode;
     }
 
         return rootNode;
     }
 
@@ -66,9 +22,9 @@ public class ArbreBinaire {
         return (getRootNode() == null);
     }
 
         return (getRootNode() == null);
     }
 
-    private IntNode inserer_rec(IntNode currentNode, int value) {
+    private Node<Integer> inserer_rec(Node<Integer> currentNode, int value) {
         if (currentNode == null) {
         if (currentNode == null) {
-            return new IntNode(value);
+            return new Node<Integer>(value);
         }
         if (value < currentNode.getData()) {
             currentNode.setLeftNode(inserer_rec(currentNode.getLeftNode(), value));
         }
         if (value < currentNode.getData()) {
             currentNode.setLeftNode(inserer_rec(currentNode.getLeftNode(), value));
@@ -82,7 +38,7 @@ public class ArbreBinaire {
         setRootNode(inserer_rec(rootNode, value));
     }
 
         setRootNode(inserer_rec(rootNode, value));
     }
 
-    private IntNode supprimer_rec(IntNode currentNode, int value) {
+    private Node<Integer> supprimer_rec(Node<Integer> currentNode, int value) {
         if (currentNode == null) {
             return null;
         }
         if (currentNode == null) {
             return null;
         }
@@ -117,7 +73,7 @@ public class ArbreBinaire {
         supprimer_rec(rootNode, value);
     }
 
         supprimer_rec(rootNode, value);
     }
 
-    private boolean hasDataRec(IntNode currentNode, int value) {
+    private boolean hasDataRec(Node<Integer> currentNode, int value) {
         if (currentNode == null) {
             return false;
         }
         if (currentNode == null) {
             return false;
         }
@@ -131,11 +87,11 @@ public class ArbreBinaire {
         return hasDataRec(rootNode, value);
     }
 
         return hasDataRec(rootNode, value);
     }
 
-    private int findSmallestData(IntNode node) {
+    private int findSmallestData(Node<Integer> node) {
         return node.getLeftNode() == null ? node.getData() : findSmallestData(node.getLeftNode());
     }
 
         return node.getLeftNode() == null ? node.getData() : findSmallestData(node.getLeftNode());
     }
 
-    private void afficher_rec(IntNode currentNode) {
+    private void afficher_rec(Node<Integer> currentNode) {
         if (currentNode != null) {
             afficher_rec(currentNode.getLeftNode());
             System.out.print(currentNode.getData() + " ");
         if (currentNode != null) {
             afficher_rec(currentNode.getLeftNode());
             System.out.print(currentNode.getData() + " ");
@@ -148,7 +104,7 @@ public class ArbreBinaire {
         System.out.println();
     }
 
         System.out.println();
     }
 
-    private void afficher_arbre_rec(IntNode currentNode, int column) {
+    private void afficher_arbre_rec(Node<Integer> currentNode, int column) {
         if (currentNode != null) {
             afficher_arbre_rec(currentNode.getRightNode(), column + 1);
             for (int i = 0; i < column; i++) {
         if (currentNode != null) {
             afficher_arbre_rec(currentNode.getRightNode(), column + 1);
             for (int i = 0; i < column; i++) {
index 0d2af562b0c310105a2708181b7daf7574958513..7ca90fe8bb84b35260926c67bef68cd5a0e1d7ed 100644 (file)
@@ -46,7 +46,8 @@ JVM = java
 # NAME = Camilo        Juan
 
 CLASSES = \
 # NAME = Camilo        Juan
 
 CLASSES = \
-        ArbreBinaire.java
+               Node.java \
+               ArbreBinaire.java
 
 #
 # MAIN is a variable with the name of the file containing the main method
 
 #
 # 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 (file)
index 0000000..1e8dd01
--- /dev/null
@@ -0,0 +1,42 @@
+public class Node<E> {
+    private E data;
+    private Node<E> leftNode;
+    private Node<E> rightNode;
+
+    Node(E value) {
+        setData(value);
+        setLeftNode(null);
+        setRightNode(null);
+    }
+
+    Node(E value, Node<E> leftNode, Node<E> rightNode) {
+        setData(value);
+        setLeftNode(leftNode);
+        setRightNode(rightNode);
+    }
+
+    public E getData() {
+        return data;
+    }
+
+    public void setData(E value) {
+        data = value;
+    }
+
+    public Node<E> getLeftNode() {
+        return leftNode;
+    }
+
+    public void setLeftNode(Node<E> Node) {
+        leftNode = Node;
+    }
+
+    public Node<E> getRightNode() {
+        return rightNode;
+    }
+
+    public void setRightNode(Node<E> Node) {
+        rightNode = Node;
+    }
+
+}