0% found this document useful (0 votes)
7 views10 pages

08 Binary Trees 4up

Uploaded by

amenworku3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

08 Binary Trees 4up

Uploaded by

amenworku3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

General Trees: Terminology (1)

David

Binary Trees
Ernesto Chris

Elsa Anna

Shirley Vanessa Peter


EECS2030: Advanced
Object Oriented Programming ○ root of tree : top element of the tree
e.g., root of the above family tree: David
Fall 2017
○ parent of node v : node immediately above and connected to v
C HEN -W EI WANG e.g., parent of Vanessa: Elsa
○ children of node v : nodes immediately below and connected to v
e.g., children of Elsa: Shirley, Vanessa, and Peter
e.g., children of Ernesto: �
3 of 37

General Trees General Trees: Terminology (2)

● A linear data structure is a sequence, where stored objects can David

be related via the “before” and “after ” relationships.


Ernesto Chris
e.g., arrays, singly-linked lists, and doubly-linked lists
● A tree is a non-linear collection of nodes. Elsa Anna
○ Each node stores some data object.
○ Nodes stored in a tree is organized in a non-linear manner. Shirley Vanessa Peter
○ In a tree , the relationships between stored objects are
○ ancestors of node v : v + v ’s parent + v ’s grand parent + . . .
hierarchical : some objects are “above” others, and some are e.g., ancestors of Vanessa: Vanessa, Elsa, Chris, and David
“below” others. e.g., ancestors of David: David
● The main terminology for the tree data structure comes from ○ descendants of node v : v + v ’s children + v ’s grand children + . . .
that of family trees: parents, siblings, children, ancestors, e.g., descendants of Vanessa: Vanessa
descendants. e.g., descendants of David: the entire family tree

2 of 37 4 of 37
4

Tree ADT (cont.) 4

General Trees: Terminology (3) General Tree: Tree ADT


Important (cont.)
Characteristics
Important Characteristics – there is a single unique path along the
David of Trees
Important
There Characteristics
is a single unique path–edges
therefrom
along the root
is a single
the to any
unique
edges particular
path
from along thenode
the root to
of Trees edges from the root to any particular node
any particular node.
Ernesto Chris

Elsa Anna

Shirley Vanessa Peter


legal tree organization
○ siblings of node v : nodes whose parents are the same as v ’s legal tree organization

e.g., siblings of Vanessa: Shirley and Peter


○ subtree rooted at v : a tree formed by all descendant of v
○ external nodes (leaves) : nodes that have no children
e.g., leaves of the above tree: Ernesto, Anna, Shirley, Vanessa,
Peter
○ internal nodes : nodes that has at least one children
illegal
illegal tree
tree organization(nontrees)
organization (nontrees)
e.g., non-leaves of the above tree: David, Chris, Elsa
9
5 of 37 7 of 37
Tree ADT (cont.)

e Binary Tree – binary tree that is completely filled, with the


possible exception of the bottom level, which
is filled from left to right
ary Tree – completely
Exercise: filledIdentifying
binary tree, withSubtrees
no missing nodes, General Trees: Ordered Trees
i.e. all leaves are at level h, and all other nodes have
two
Howchildren
many subtrees are there?
A A A tree is ordered if there is a meaningful linear order among
the children of each node.
B C B C Book

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

buylow sellhigh market A ’s left subtree is rooted at B and


complete binaryright
tree subtree rooted at C . full binary tr

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))

Binary Trees Binary Trees: Recursive Definition


A binary tree is either:
● An empty tree; or
● A nonempty tree with a root node r that
● A binary tree is an ordered tree which satisfies the following ○ has a left binary subtree rooted at its left child
properties: ○ has a right binary subtree rooted at its right child

⇒ To solve problems recursively on a binary tree rooted at r :


1. Each node has at most two children.
2. Each child node is labeled as either a left child or a right child .
3. A left child precedes a right child in the order of children of a node. ● Do something with root r .
● Recur on r ’s left subtree. [ strictly smaller problem ]
● Recur on r ’s right subtree. [ strictly smaller problem ]
Similar to how we recur on subarrays (by passing the from and
to indices), we recur on subtrees by passing their roots (i.e.,
the current root’s left child and right child).
10 of 37 12 of 37
Binary Trees: Application (1) Tree Traversal Algorithms: Definition
A decision tree is a binary tree used to to express the
decision-making process:
● Each internal node has two children (yes and no).
● Each external node represents a decision.
● A traversal of a tree T is a systematic way of visiting all the
Do you attend classes regularly?
nodes of T .
yes no ● The visit of each node may be associated with an action: e.g.,
○ print the node element
You may pass with
Do try out all code examples? ○ determine if the node element satisfies certain property
○ accumulate the node element value to some global counter
30% confidence!
yes no

You may pass with Do you ask for help whenever getting stuck?
another 30% confidence!
yes no

You may pass with You may pass if


another 30% confidence! there is a miracle!
13 of 37 15 of 37

Binary Trees: Application (2) Tree Traversal Algorithms: Common Types


● Inorder: Visit left subtree, then parent, then right subtree.
An arithmetic expression can be represented using a binary tree:
● Each internal node denotes an operator (unary or binary).
inorder (r ): if(r != null) {/*subtree with root r is not empty*/

● Each external node denotes an operand (i.e., a number).


inorder (r ’s left child)
visit and act on the subtree rooted at r
e.g., Use a binary tree to represent the arithmetic expression inorder (r ’s right child) }

( ((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*/

visit and act on the subtree rooted at r


/ +
preorder (r ’s left child)
∗ + ∗ 6 preorder (r ’s right child) }

● Postorder: Visit left subtree, then right subtree, then parent.


+ 3 − 2 3 −
postorder (r ): if(r != null) {/*subtree with root r is not empty*/
3 1 9 5 7 4
postorder (r ’s left child)
● To print, or to evaluate, the expression that is represented by a postorder (r ’s right child)
visit and act on the subtree rooted at r }
binary tree, certain traversal over the entire tree is required.
14 of 37 16 of 37
Tree Traversal: Inorder Tree Traversal: Postorder
A A

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

Tree Traversal: Preorder Tree Traversal: Exercises


A

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; } }

● The way we implement the add methods is not recursive.


public void setLeft(BTNode left) { this.left = left; }
public void setRight(BTNode right) { this.right = right; }
} ● These two add methods assume that the caller calls them by
passing references of the parent nodes.
21 of 37 23 of 37

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

public void setRoot(BTNode root) {


BinaryTree bt = new BinaryTree(); /* empty binary tree */
this.root = root;
BTNode root = new BTNode("D"); /* node disconnected from BT */
}
bt.setRoot(root); /* node connected to BT */
bt.addToLeft(root, "B");
...
bt.addToRight(root, "F");
}
bt.addToLeft(root.getLeft(), "A");
bt.addToRight(root.getLeft(), "C");
bt.addToLeft(root.getRight(), "E");
bt.addToRight(root.getRight(), "G");
22 of 37 24 of 37
Binary Tree in Java: Counting Size (1) Binary Tree in Java: Membership (1)
Size of a tree rooted at r is 1 (counting r itself) plus the size of An element e exists in a tree rooted at r if either r contains e,
or r ’s left subtree contains e, or r ’s right subtree contains e.
r ’s left subtree and plus the size of r ’s right subtree.
public class BinaryTree {
public class BinaryTree { private BTNode root;
private BTNode root;
public int size() { return sizeHelper (root); } public boolean has(String e) { return hasHelper (root, e); }
private int sizeHelper (BTNode root) { private boolean hasHelper (BTNode root, String e) {
if(root == null) { if(root == null) {
return 0; return false;
} }
else { else {
return return
1 root.getElement().equals(e)
+ sizeHelper (root.getLeft()) || hasHelper (root.getLeft(), e)
+ sizeHelper (root.getRight()); || hasHelper (root.getRight(), e);
} }
} }
} }
25 of 37 27 of 37

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)

public class BinaryTree { public class BinaryTree {


private BTNode root; private BTNode root;

public ArrayList<String> inroder() { public ArrayList<String> preorder() {


ArrayList<String> list = new ArrayList<>(); ArrayList<String> list = new ArrayList<>();
inorderHelper (root, list); preorderHelper (root, list);
return list; return list;
} }
private void inorderHelper (BTNode root, ArrayList<String> list) { private void preorderHelper (BTNode root, ArrayList<String> list) {
if(root != null) { if(root != null) {
inorderHelper (root.getLeft(), list); list.add(root.getElement());
list.add(root.getElement()); preorderHelper (root.getLeft(), list);
inorderHelper (root.getRight(), list); preorderHelper (root.getRight(), list);
} }
} }
} }

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

Binary Tree in Java: Postorder Traversal (2) Index (2)


@Test Tree Traversal Algorithms: Common Types
public void testBT_inorder() {
BinaryTree bt = new BinaryTree();
Tree Traversal: Inorder
BTNode root = new BTNode("D"); Tree Traversal: Preorder
bt.setRoot(root);
bt.addToLeft(root, "B"); Tree Traversal: Postorder
bt.addToRight(root, "F"); Tree Traversal: Exercises
bt.addToLeft(root.getLeft(), "A");
bt.addToRight(root.getLeft(), "C"); Binary Tree in Java: Linked Node
bt.addToLeft(root.getRight(), "E"); Binary Tree in Java: Root Node
bt.addToRight(root.getRight(), "G");
ArrayList<String> list = bt.postorder() ; Binary Tree in Java: Adding Nodes (1)
assertEquals(list.get(0), "A"); Binary Tree in Java: Adding Nodes (2)
assertEquals(list.get(1), "C");
assertEquals(list.get(2), "B"); Binary Tree in Java: Counting Size (1)
assertEquals(list.get(3), "E"); Binary Tree in Java: Counting Size (2)
assertEquals(list.get(4), "G");
assertEquals(list.get(5), "F"); Binary Tree in Java: Membership (1)
assertEquals(list.get(6), "D");
}
Binary Tree in Java: Membership (2)
Binary Tree in Java: Inorder Traversal (1)
34 of 37 36 of 37
Index (3)
Binary Tree in Java: Inorder Traversal (2)

Binary Tree in Java: Preorder Traversal (1)

Binary Tree in Java: Preorder Traversal (2)

Binary Tree in Java: Postorder Traversal (1)

Binary Tree in Java: Postorder Traversal (2)

37 of 37

You might also like