Java Reference
In-Depth Information
111 toBeRemoved.data = smallest.data;
112 smallestParent.left = smallest.right;
113 }
114
115 /**
116
Prints the contents of the tree in sorted order.
117 */
118
public void
print()
119 {
120
if
(root !=
null
)
121 root.printNodes();
122 System.out.println();
123 }
124
125
private
Node root;
126
127 /**
128
A node of a tree stores a data item and references
129
to the child nodes to the left and to the right.
130 */
131
private class
Node
132 {
133 /**
134
Inserts a new node as a descendant of this node.
135 @param newNode
the node to insert
136 */
137
public void
addNode(Node newNode)
138 {
139
int
comp =
newNode.data.compareTo(data);
140
if
(comp <
0
)
141 {
142
if
(left ==
null
) left = newNode;
143
else
left.addNode(newNode);
144 }
145
if
(comp >
0
)
146 {
147
if
(right ==
null
) right = newNode;
148
else
right.addNode(newNode);
149 }
150 }
151
729
730