Professional Documents
Culture Documents
Binary Tree ADT and Tree Node ADT: Page 1 of 61
Binary Tree ADT and Tree Node ADT: Page 1 of 61
Binary Tree ADT and Tree Node ADT: Page 1 of 61
Page 1 of 61
Tree Node ADT and Its Implementations
Implementations:
Page 2 of 61
Binary Tree ADT and Its Implementations
Implementations:
Page 3 of 61
Binary Tree ADT and Its Implementations
t1
TreeNode n1;
BinaryTree t1, t2, t3, t4;
n1
… t2
t1 = new EmptyBinaryTree();
… t3 t4
t2 = new NonemptyBinaryTree(n1, t3, t4);
Page 4 of 61
Binary Tree ADT and Its Implementations
t1
TreeNode n1;
BinaryTree t1, t2; t2 n1
…
t1 = new EmptyBinaryTree();
t2 = new NonemptyBinaryTree(n1,
new EmptyBinaryTree(),new EmptyBinaryTree());
Page 5 of 61
Binary Search Trees
0123849
John/NA/CSC
Key values are
unique.
At every node,
0114938 0150957
Mary/CC/CEG May/NA/IEG
the key value
must be
greater than all
the keys in the
0120739 0147723 0160039
Tom/SC/INE Sue/UC/IDE Will/CC/CSC
left subtree,
and less than
all the keys in
0120730 0134219 0170980 the right
Jack/SC/ELE Ben/CC/ACE Bob/UC/SEG subtree.
Page 6 of 61
How are Binary Search Trees constructed?
Page 7 of 61
Constructing a Binary Search Tree
0123849
0123849
Page 8 of 61
Constructing a Binary Search Tree
0123849
0123849
Page 9 of 61
…
BinaryTree t1, t2;
TreeNode n1;
n1 = new OrdinaryTreeNode(…);
t1 = new EmptyBinaryTree();
t2 = t1.insertNode(n1);
…
Page 10 of 61
public interface TreeNode {
public String getNodeKey();
}
Page 11 of 61
Constructing a Binary Search Tree
0123849
0114938
0114938
Page 12 of 61
Constructing a Binary Search Tree
0123849
0120739
0114938
0120739
Page 13 of 61
Constructing a Binary Search Tree
0123849
0150957
0114938 0150957
0120739
Page 14 of 61
Constructing a Binary Search Tree
0123849
0160039
0114938 0150957
0120739 0160039
Page 15 of 61
and so on …
Page 16 of 61
Now, another viewpoint of node insertion.
Page 17 of 61
Constructing a Binary Search Tree
0120730
0123849
0120730
0114938 0150957
Page 18 of 61
Constructing a Binary Search Tree
0123849
0120730 0120730
0114938 0150957
Page 19 of 61
Constructing a Binary Search Tree
0123849
0120730
0114938 0150957
0120730
Page 20 of 61
Constructing a Binary Search Tree
0123849
0120730
0114938 0150957
0120730
Page 21 of 61
Constructing a Binary Search Tree
0123849
0120730
0114938 0150957
0120730
Page 22 of 61
/* For EmptyBinaryTree Class */
Page 23 of 61
public class EmptyBinaryTree implements BinaryTree {
Note …
public BinaryTree insertNode(TreeNode n){
return new NonemptyBinaryTree(n,
new EmptyBinaryTree(),
/* For */ }
new EmptyBinaryTree());
/* EmptyBinaryTree */ }
0123849
0123849
Page 25 of 61
int sign = n.getNodeKey().compareTo(this.root().getNodeKey());
if (sign == 0) return new NonemptyBinaryTree(n, L, R));
if (sign < 0) return new NonemptyBinaryTree(r, L', R);
return new NonemptyBinaryTree(r, L, R'));
0123849
0114938 0150957
Page 27 of 61
public interface BinaryTree {
public TreeNode root();
public BinaryTree leftSubtree();
public BinaryTree rightSubtree();
public boolean isEmpty();
public boolean equalTo(BinaryTree t);
Page 28 of 61
BinaryTree method: findMinNode
Tree is empty:
} error.
Page 29 of 61
BinaryTree method: findMinNode
Tree is empty:
error.
Page 30 of 61
BinaryTree method: Delete Node
0123849
0114938
0120739
0150957
0147723 0160039
0120730
Page 31 of 61
public interface BinaryTree {
public TreeNode root();
public BinaryTree leftSubtree();
public BinaryTree rightSubtree();
public boolean isEmpty();
public boolean equalTo(BinaryTree t);
Page 32 of 61
BinaryTree method: findMinNode
Page 33 of 61
BinaryTree method: Delete Node
0123849
0114938
0120730
Page 34 of 61
BinaryTree method: Delete Node
0123849
0114938
0120730
Page 35 of 61
BinaryTree method: Delete Node
Page 36 of 61
BinaryTree method: Delete Node
CASE 1
Page 37 of 61
BinaryTree method: Delete Node
CASE 2a
Page 38 of 61
BinaryTree method: Delete Node
CASE 2b
Page 39 of 61
BinaryTree method: Delete Node
CASE 2c
Page 41 of 61
/* Case 2c */
if (!this.leftSubtree().isEmpty() && !this.rightSubtree().isEmpty()) {
TreeNode M = this.rightSubtree().findMinNode();
return new NonemptyBinaryTree(M,
this.leftSubtree(), this.rightSubtree().deleteNode(M));
};
Page 42 of 61
/* Cases 2a and 2b */
if (this.rightSubtree().isEmpty())
return this.leftSubtree();
else
return this.rightSubtree();
}
Page 43 of 61
/* For NonemptyBinaryTree Class */
public class NonemptyBinaryTree implements BinaryTree {
…
public BinaryTree deleteNode(TreeNode n) {
int sign = n.getNodeKey().compareTo(this.root().getNodeKey());
if (sign<0) return new NonemptyBinaryTree(this.root(),
this.leftSubtree().deleteNode(n), this.rightSubtree());
if (sign>0) return new NonemptyBinaryTree(this.root(),
this.leftSubtree(), this.rightSubtree().deleteNode(n));
if (!this.leftSubtree().isEmpty() && !this.rightSubtree().isEmpty()) {
TreeNode M = this.rightSubtree().findMinNode();
return new NonemptyBinaryTree(M,
this.leftSubtree(), this.rightSubtree().deleteNode(M));
};
if (this.rightSubtree().isEmpty())
return this.leftSubtree();
else
return this.rightSubtree();
}
Page 44 of 61
The Binary Tree ADT
leftSubtree
findMinNode
…
Page 45 of 61
*
public interface TreeNode {
public String getNodeKey();
public boolean equalTo(TreeNode N);
}
public interface BinaryTree {
public TreeNode root();
public BinaryTree leftSubtree();
public BinaryTree rightSubtree();
public boolean isEmpty();
public boolean equalTo(BinaryTree t);
public BinaryTree insertNode(TreeNode N);
public TreeNode findMinNode();
public BinaryTree deleteNode(TreeNode N);
}
Page 46 of 61
How to implement the Tree Node ADT and the Binary Tree
ADT?.
Page 47 of 61
Implementation of the Tree Node ADT
Page 48 of 61
public class OrdinaryTreeNode implements TreeNode {
private String key;
private Object contents;
public OrdinaryTreeNode(String k, Object c) {
this.key = k; this.contents = c;
}
public String getNodeKey() {
return this.key;
}
public boolean equalTo(TreeNode N) {
return key.equals(N.getNodeKey());
/* Well, … */
} 0123849
} (data) "0123849"
Page 49 of 61
(data)
public class SpecialErrTreeNode implements TreeNode {
Page 50 of 61
Implementation of the Binary Tree ADT
Page 51 of 61
Implementation of the Binary Tree ADT
L R
R
Page 52 of 61
Constructor for NonemptyBinaryTree
this.Right = R;
}
L
L R
R
Page 53 of 61
public TreeNode root() {
return this.Root; L R
}
return this.Right;
}
Page 54 of 61
/* For EmptyBinaryTree Class */
Page 55 of 61
We now show the complete listing of the implementations.
Page 56 of 61
/* EmptyBinaryTree class */
public class EmptyBinaryTree implements BinaryTree {
Page 57 of 61
public boolean equalTo(BinaryTree t) {
return t.isEmpty();
}
public BinaryTree insertNode(TreeNode n) {
return new NonemptyBinaryTree(n,
new EmptyBinaryTree(), new EmptyBinaryTree());
}
public BinaryTree deleteNode(TreeNode N) {
throw new HeyException();
}
public TreeNode findMinNode() {
return new SpecialErrTreeNode();
}
Page 58 of 61
/* NonemptyBinaryTree class */
public class NonemptyBinaryTree implements BinaryTree {
Page 59 of 61
public BinaryTree leftSubtree() {
return this.Left;
}
public BinaryTree rightSubtree() {
return this.Right;
}
public boolean isEmpty() {
return false;
}
public boolean equalTo(BinaryTree t) {
if (t.isEmpty()) return false;
return this.root().equalTo(t.root()) &&
this.leftSubtree().equalTo(t.leftSubtree()) &&
this.rightSubtree().equalTo(t.rightSubtree());
}
Page 60 of 61
public BinaryTree insertNode(TreeNode n) {
… /* Please refer to the last lecture’s notes. */
}
Page 61 of 61