08 Binary Trees 4up
08 Binary Trees 4up
David
Binary Trees
Ernesto Chris
Elsa Anna
2 of 37 4 of 37
4
Elsa Anna
E F G D E F G
Preface Part A Part B References
J H I J K L M N O
¶ ... ¶ Ch. 1 ... Ch. 5 Ch. 6 ... Ch. 9 ¶ ... ¶
15 subtrees
omplete binary tree full[ binary
i.e., subtrees
tree rooted at each node ]
ary tree of height h is somewhere S IZE OF S UBTREE R OOTS OF S UBTREES § 1.1 ... § 1.4 § 5.1 ... § 5.7 § 6.1 ... § 6.5 § 9.1 ... § 9.6
l binary tree of height h and a full
ary tree of height (h-1)) 1 H, I, J, K, L, M, N, O
3 D, E, F, G ¶ ... ¶ ... ¶ ... ¶
7 B, C
15 A
6 of 37 8 of 37
Tree ADT (cont.)
General Trees: Unordered Trees Binary Trees: Complete
Terminology (1)
Complete Binary Tree – binary tree that is completely fille
Binary Tree
For an internal node n : possible exception of the bottom
A tree is unordered if the order among the children of each
● Subtree rooted at its left child is called left subtree
is filled .from left to right
node does not matter.
n has no left child ⇒ n ’s left subtree is empty
Full Binary Tree – completely filled binary tree, with no mis
/user/rt/courses/
● Subtree rooted at its right child is called right
i.e. all subtree
leaves are at. level h, and all other
cs016/ cs252/
n has no right child ⇒ n ’s right subtree is empty
two children
A A
grades
homeworks/ programs/ projects/
grades B C B
D E F G D E
hw1 hw2 hw3 pr1 pr2 pr3
papers/ demos/
H I J H I J K L
H ’s left subtree(complete
and right subtree
binary are both
tree of height empty.
h is somewhere
9 of 37 11 of 37 between a full binary tree of height h and a full
binary tree of height (h-1))
You may pass with Do you ask for help whenever getting stuck?
another 30% confidence!
yes no
( ((3 + 1) * 3) / ((9 - 5) + 2) ) - ( (3 * (7 - 4)) + 6 ) ● Preorder: Visit parent, then left subtree, then right subtree.
preorder (r ): if(r != null) {/*subtree with root r is not empty*/
−
B C B C
D E F G D E F G
H I J H I J
inorder traversal from the root A: postorder traversal from the root A:
H D I B J E A F C G H I D J E B F G C A
��� ��� ��� ��� ��� ��� ��� ��� ��� ���
inorder (H) inorder (I) inorder (J) inorder (F ) inorder (G) postorder (H) postorder (I) postorder (J) postorder (F ) postorder (G)
��� � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� ��� � � � � � � � � � � � ��� � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� ��� � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �
inorder (D) inorderE inorder (C) postorder (D) postorder (E) postorder (C)
��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �
inorderB postorder (B)
��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �
inorder (A) postorder (A)
17 of 37 19 of 37
B C / +
∗ + ∗ 6
D E F G
+ 3 − 2 3 −
H I J
3 1 9 5 7 4
preorder traversal from the root A:
A B D H I E J C F G ● inorder traversal from the root:
��� ��� ��� ��� ���
preorder (H) preorder (I) preorder (J) preoder (F ) preoreder (G) 3+1*3/9-5+2-3*7-4+6
��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ● preorder traversal from the root:
preorder (D) preorder (E) preorder (C)
��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � -/*+313+-952+*3-746
preorder (B) ● postorder traversal from the root:
��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �
preorder (A) 31+3*95-2+/374-*6+-
18 of 37 20 of 37
Binary Tree in Java: Linked Node Binary Tree in Java: Adding Nodes (1)
public class BinaryTree {
public class BTNode { private BTNode root;
private String element; public void addToLeft(BTNode n, String element) {
private BTNode left; if(n.getLeft() != null) {
private BTNode right; throw new IllegalArgumentException("Left is already there");
}
BTNode(String element) { n.setLeft(new BTNode(element));
this.element = element; }
} public void addToRight(BTNode n, String element) {
if(n.getRight() != null) {
public String getElement() { return element; } throw new IllegalArgumentException("Right is already there");
public BTNode getLeft() { return left; } }
public BTNode getRight() { return right; } n.setRight(new BTNode(element));
}
public void setElement(String element) { this.element = element; } }
Binary Tree in Java: Root Note Binary Tree in Java: Adding Nodes (2)
Exercise: Write Java code to construct the following binary tree:
root
D
left right
public class BinaryTree {
private BTNode root;
B F
left right left right
public BinaryTree() {
/* Initialize an empty binary tree with root being null. */
} A C E G
Binary Tree in Java: Counting Size (2) Binary Tree in Java: Membership (2)
@Test
public void testBTMembership() {
@Test BinaryTree bt = new BinaryTree();
public void testBTSize() { assertFalse(bt.has("D"));
BinaryTree bt = new BinaryTree(); BTNode root = new BTNode("D");
assertEquals(0, bt.size()); bt.setRoot(root);
assertTrue(bt.has("D"));
BTNode root = new BTNode("D"); assertFalse(bt.has("A"));
bt.setRoot(root); bt.addToLeft(root, "B");
assertEquals(1, bt.size()); bt.addToRight(root, "F");
bt.addToLeft(root.getLeft(), "A");
bt.addToLeft(root, "B"); bt.addToRight(root.getLeft(), "C");
bt.addToRight(root, "F"); bt.addToLeft(root.getRight(), "E");
bt.addToLeft(root.getLeft(), "A"); bt.addToRight(root.getRight(), "G");
bt.addToRight(root.getLeft(), "C"); assertTrue(bt.has("A")); assertTrue(bt.has("B"));
bt.addToLeft(root.getRight(), "E"); assertTrue(bt.has("C")); assertTrue(bt.has("D"));
bt.addToRight(root.getRight(), "G"); assertTrue(bt.has("E")); assertTrue(bt.has("F"));
assertEquals(7, bt.size()); assertTrue(bt.has("G"));
} assertFalse(bt.has("H"));
assertFalse(bt.has("I"));
}
26 of 37 28 of 37
Binary Tree in Java: Inorder Traversal (1) Binary Tree in Java: Preorder Traversal (1)
29 of 37 31 of 37
Binary Tree in Java: Inorder Traversal (2) Binary Tree in Java: Preorder Traversal (2)
@Test @Test
public void testBT_inorder() { public void testBT_inorder() {
BinaryTree bt = new BinaryTree(); BinaryTree bt = new BinaryTree();
BTNode root = new BTNode("D"); BTNode root = new BTNode("D");
bt.setRoot(root); bt.setRoot(root);
bt.addToLeft(root, "B"); bt.addToLeft(root, "B");
bt.addToRight(root, "F"); bt.addToRight(root, "F");
bt.addToLeft(root.getLeft(), "A"); bt.addToLeft(root.getLeft(), "A");
bt.addToRight(root.getLeft(), "C"); bt.addToRight(root.getLeft(), "C");
bt.addToLeft(root.getRight(), "E"); bt.addToLeft(root.getRight(), "E");
bt.addToRight(root.getRight(), "G"); bt.addToRight(root.getRight(), "G");
ArrayList<String> list = bt.inroder() ; ArrayList<String> list = bt.preorder() ;
assertEquals(list.get(0), "A"); assertEquals(list.get(0), "D");
assertEquals(list.get(1), "B"); assertEquals(list.get(1), "B");
assertEquals(list.get(2), "C"); assertEquals(list.get(2), "A");
assertEquals(list.get(3), "D"); assertEquals(list.get(3), "C");
assertEquals(list.get(4), "E"); assertEquals(list.get(4), "F");
assertEquals(list.get(5), "F"); assertEquals(list.get(5), "E");
assertEquals(list.get(6), "G"); assertEquals(list.get(6), "G");
} }
30 of 37 32 of 37
Binary Tree in Java: Postorder Traversal (1) Index (1)
General Trees
public class BinaryTree { General Trees: Terminology (1)
private BTNode root; General Trees: Terminology (2)
public ArrayList<String> preorder() { General Trees: Terminology (3)
ArrayList<String> list = new ArrayList<>();
Exercise: Identifying Subtrees
postorderHelper (root, list);
return list; General Tree: Important Characteristics
} General Trees: Ordered Trees
private void postorderHelper (BTNode root, ArrayList<String> list) {
if(root != null) { General Trees: Unordered Trees
list.add(root.getElement()); Binary Trees
postorderHelper (root.getLeft(), list);
Binary Trees: Terminology (1)
postorderHelper (root.getRight(), list);
} Binary Trees: Recursive Definition
} Binary Trees: Application (1)
}
Binary Trees: Application (2)
Tree Traversal Algorithms: Definition
33 of 37 35 of 37
37 of 37