Spell fix in comment.
[TP_POO.git] / Arbres / ArbreBinaire.java
CommitLineData
d7dd93e3
JB
1
2/**
3 * Binary tree class.
a7b1affa 4 * A binary tree is a ordered value tree with only two childs per node
d7dd93e3
JB
5 */
6public class ArbreBinaire {
7
8 private class IntNode {
9 private int data;
10 private IntNode leftIntNode;
11 private IntNode rightIntNode;
12
13 IntNode(int value) {
14 setData(value);
15 setLeftNode(null);
16 setRightNode(null);
17 }
18
19 IntNode(int value, IntNode leftNode, IntNode rightNode) {
20 setData(value);
21 setLeftNode(leftNode);
22 setRightNode(rightNode);
23 }
24
25 private int getData() {
26 return data;
27 }
28
29 private void setData(int value) {
30 data = value;
31 }
32
33 private IntNode getLeftNode() {
34 return leftIntNode;
35 }
36
37 private void setLeftNode(IntNode leftNode) {
38 leftIntNode = leftNode;
39 }
40
41 private IntNode getRightNode() {
42 return rightIntNode;
43 }
44
45 private void setRightNode(IntNode rightNode) {
46 rightIntNode = rightNode;
47 }
48
49 }
50
51 private IntNode rootNode;
52
53 ArbreBinaire() {
54 setRootNode(null);
55 }
56
57 private void setRootNode(IntNode node) {
58 rootNode = node;
59 }
60
61 private IntNode getRootNode() {
62 return rootNode;
63 }
64
65 private boolean isEmpty() {
66 return (getRootNode() == null);
67 }
68
a323eab8
JB
69 private IntNode inserer_rec(IntNode currentNode, int value) {
70 if (currentNode == null) {
71 return new IntNode(value);
72 } else if (value < currentNode.getData()) {
73 currentNode.setLeftNode(inserer_rec(currentNode.getLeftNode(), value));
74 } else if (value > currentNode.getData()) {
75 currentNode.setRightNode(inserer_rec(currentNode.getRightNode(), value));
5b834ba1 76 } /* skip the equality case */
a323eab8
JB
77 return currentNode;
78 }
79
d7dd93e3 80 public void inserer(int value) {
a323eab8
JB
81 setRootNode(inserer_rec(rootNode, value));
82 }
d7dd93e3 83
a323eab8
JB
84 private IntNode supprimer_rec(IntNode currentNode, int value) {
85 if (currentNode == null) {
86 return null;
87 } else if (value == currentNode.getData()) {
88 if (currentNode.getLeftNode() == null && currentNode.getRightNode() == null) {
89 return null;
90 } else if (currentNode.getRightNode() == null) {
91 return currentNode.getLeftNode();
92 } else if (currentNode.getLeftNode() == null) {
93 return currentNode.getRightNode();
94 } else {
5b834ba1
JB
95 /*
96 * First, we need to find the node that will replace the deleted node.
97 * We’ll use the smallest node of the node to be deleted’s right sub-tree.
98 * Then, we assign the smallest value to the node to delete and after that,
99 * we’ll delete it from the right subtree.
100 */
a323eab8
JB
101 int smallestValue = findSmallestData(currentNode.getRightNode());
102 currentNode.setData(smallestValue);
103 currentNode.setRightNode(supprimer_rec(currentNode.getRightNode(), smallestValue));
104 return currentNode;
105 }
106 } else if (value < currentNode.getData()) {
107 currentNode.setLeftNode(supprimer_rec(currentNode.getLeftNode(), value));
108 } else {
109 currentNode.setRightNode(supprimer_rec(currentNode.getRightNode(), value));
110 }
111 return currentNode;
d7dd93e3
JB
112 }
113
114 public void supprimer(int value) {
a323eab8
JB
115 supprimer_rec(rootNode, value);
116 }
117
118 private boolean hasDataRec(IntNode currentNode, int value) {
119 if (currentNode == null) {
120 return false;
121 }
122 if (value == currentNode.getData()) {
123 return true;
124 }
125 return value < currentNode.getData() ? hasDataRec(currentNode.getLeftNode(), value) : hasDataRec(currentNode.getRightNode(), value);
126 }
127
128 public boolean hasData(int value) {
5b834ba1 129 return hasDataRec(rootNode, value);
a323eab8
JB
130 }
131
132 private int findSmallestData(IntNode node) {
133 return node.getLeftNode() == null ? node.getData() : findSmallestData(node.getLeftNode());
134 }
135
5b834ba1
JB
136 private void afficher_rec(IntNode currentNode) {
137 if (currentNode != null) {
a323eab8 138 afficher_rec(currentNode.getLeftNode());
5b834ba1 139 System.out.print(currentNode.getData() + " ");
a323eab8
JB
140 afficher_rec(currentNode.getRightNode());
141 }
5b834ba1
JB
142 }
143
144 public void afficher() {
145 afficher_rec(rootNode);
146 System.out.println();
147 }
148
149 private void afficher_arbre_rec(IntNode currentNode, int column) {
150 int i;
151
152 if (currentNode != null) {
153 afficher_arbre_rec(currentNode.getRightNode(), column + 1);
154 for (i = 0; i < column; i++) {
155 System.out.print(" ");
156 }
157 System.out.println(currentNode.getData());
158 afficher_arbre_rec(currentNode.getLeftNode(), column + 1);
159 }
160 }
161
162 public void afficher_arbre() {
163 afficher_arbre_rec(rootNode, 1);
a323eab8
JB
164 }
165
166 public static void main(String[] args) {
289395c5 167 ArbreBinaire bTree = new ArbreBinaire();
a323eab8 168
289395c5
JB
169 bTree.inserer(2);
170 bTree.inserer(6);
171 bTree.inserer(4);
172 bTree.inserer(5);
173 bTree.inserer(1);
174 bTree.inserer(0);
5b834ba1 175
289395c5
JB
176 bTree.afficher();
177 bTree.afficher_arbre();
5b834ba1 178
289395c5 179 bTree.supprimer(4);
5b834ba1 180
289395c5
JB
181 bTree.afficher();
182 bTree.afficher_arbre();
a323eab8 183
289395c5 184 bTree.supprimer(6);
d7dd93e3 185
289395c5
JB
186 bTree.afficher();
187 bTree.afficher_arbre();
42256966 188
289395c5
JB
189 bTree.inserer(2);
190 bTree.inserer(7);
191 bTree.inserer(3);
192 bTree.inserer(9);
193 bTree.inserer(11);
194 bTree.inserer(10);
42256966 195
289395c5
JB
196 bTree.afficher();
197 bTree.afficher_arbre();
d7dd93e3
JB
198 }
199
200}