Title: Tree
1Tree
- Pradondet Nilagupta
- (pom_at_ku.ac.th)
- Department of Computer Engineering
- Kasetsart University
2Outline of Lecture
- Definition of Binary Tree
- Basic Properties
- Representation of Binary Trees
- Formula-Based
- Linked Representation
- Binary Tree Traversal
- The ADT Binary Tree
- Implementation
3Trees
- A tree imposes a hierarchical structure on a
collection of items - e.g., genealogies, organization charts
Algorithms and Data Structures
Analysis of Algorithms
Algorithm Correctness
Problems and Specifications
Recursive Algorithms
Characteristic Operations
- Used long before computer science
- However, arise naturally in many different areas
of computer science where ?
4Basic Terminology
- A tree is collection of elements called nodes,
one of which is distinguished as the root - Relationship (parenthood) which places
hierarchical structure on the nodes - Node can be of whatever type we wish
5Recursive Definition
- 1. A single node by itself is a tree. This node
is also the root of the tree. - 2. Suppose n is a node and T1, T2, , Tk are
trees with roots n1, n2, , nk respectively. We
can construct a new tree by making n be the
parent of nodes n1, n2, , nk. In this tree n is
the root and T1, T2, , Tk are the subtrees of
the root. Nodes n1, n2, , nk are called the
children of node n. - The null tree is a tree with no nodes,
represented by ?
6Example - Terminology Contd
Book
C1
C2
C3
s1.1
s1.2
s2.1
s2.2
s2.3
s2.1.1
s2.1.2
path sequence of nodes n1,n2,n3, , nk length
of path height of node/tree depth of node
(proper) ancestor (proper) descendant leaf
7Definition
- A binary tree is either
- 1. an empty tree, or
- 2. a tree in which every node has either no
children, a left child, a right child, or both a
left and a right child - Each element in a binary tree has exactly two
subtrees (one or both may be empty binary trees) - The subtrees of each element in a binary tree are
ordered (distinguish between right an left
subtrees)
8Examples of Binary Trees
A
nodes 9 height of root 4
B
C
D
E
F
G
H
I
A
A
B
B
empty
empty
9Binary Expression Trees
- Use binary trees to represent arithmetic
expressions - e.g.,
/
a
e
-
c
d
10Properties of Binary Trees
- 1. A binary tree with n nodes has n-1 edges
- 2. A binary tree of height h, h?0, has at least h
and at most 2h-1 elements in it - 3. The height of a binary tree that contains n
elements is at most n and at least ?log2(n1)?
11Proof
- Number of Elements in Binary Tree
- Number of elements at least h since there must be
at least one element per level - No more than 2 children per node ? number of
elements at level i is 2i-1, igt0 - root 1 then 2, 4, 8,
- total number of elements ?
12Proof
- Height of a Binary Tree
- n elements, height cannot exceed n (1 element per
level) - from previous result, binary tree with height h
has no more than 2h -1 elements - solve for h
- n ? 2h -1 h ? log2(n1)
- h must be integer ? take ceiling
13Full Binary Tree
1
2
3
4
5
6
7
12
14
15
13
8
10
11
9
- Definition
- Full binary tree of height h has exactly 2h-1
elements - Example
- height h 4
- 24-1 15 elements
14Complete Binary Tree
1
2
3
4
5
6
- Definition
- binary tree of height h
- all levels except possibly level h-1 are full
- full binary tree is a special case of a complete
binary tree
15More Properties
- Let i, 1 ? i ?n, be the number assigned to an
element of a complete binary tree of height h as
follows - Begin at level 1 and go down to level h,
numbering left to right (see next slide). - Then
- If i1, then element is root of binary tree. If
igt1, then its parent has number ?i/2? - If 2igtn, then element has no left child. O.w.,
its left child has been assigned the number 2i - If 2i1gtn, then this element has no right child.
O.w., its right child has been assigned the
number 2i1
16Intuition
1
2
3
4
5
6
7
12
14
15
13
8
10
11
9
Rigorous proof by induction on i
17Formula-Based Representation
- Use array to store elements
- Element numbers are used as array indices
- Assume complete binary tree (with missing
elements) - Leave empty cells in the array
- Use properties of complete b. t. to calculate
array indices (see previous slide)
1
A
?
A
B
C
2
3
B
0
1
2
3
4
5
6
7
4
5
7
6
C
18Contd
- Let r be index of element, then
- Parent(r) ?(r-1)/2?, 0ltrltn
- Left child(r) 2r1, if 2r1 lt n
- Right child(r) 2r2 if 2r2ltn
- Left sibling(r) r-1 if even and 0ltrltn
- Right sibling(r) r1 if r odd and r1ltn
- Compact representation
- However, only useful when number of missing
elements is small
19Worst Case Scenario
- Right-skewed binary tree
- With n elements, requires array of size up to
2n-1
A
3
B
7
C
7
D
A
B
C
D
20Linked Representation
- Each element is represented by by a node (chain
node) that has two link fields (leftChild and
rightChild) - each node has element field to hold data
- edge in tree is represented by a pointer from
parent node to child node
t
A binary tree with n nodes has 2n - (n-1) n1
NULL pointers
A
B
C
D
E
21Contd
- Popular way to represent trees
- large fraction of space is devoted to tree
structure overhead (not to storing data) - Tree traversal through pointer chasing
- need methods such as getLeftChild(),
getRightChild(), etc.
22Review
A
B
C
D
E
F
G
H
I
J
K
L
- Which nodes are leaves?
- Which node is the root?
- What is the parent of C?
- Which nodes are ancestors of E?
- What is the depth of node C?
- What is the height of node C/the tree?
- How many different paths of length 3 are there?
23Common Operations on Binary Trees
- Determine height
- Determine number of elements of tree
- Copy tree
- Evaluate tree (i.e., if it is an expression tree)
- etc.
24Binary Tree Traversal
- Often, wish to process a binary tree by
visiting each of its nodes - At each node perform some specific action, e.g.,
print the contents of the node - Process for visiting these nodes in some order is
called traversal - preorder
- inorder
- postorder
- level order
- A traversal is a systematic ordering of all nodes
in a tree
25Traversal
Let T be a binary tree with root n and subtrees
T1 and T2
n
T1
T2
- Preorder, inorder, postorder are defined
recursively - Difference lies in the time at which a node is
visited
26Preorder Traversal
- If a tree T is null, then the preorder traversal
is the empty list - If T consists of a single node, then that node by
itself is the preorder listing (traversal) of T - Otherwise, the preorder listing of the nodes of T
is the root n of T followed by the nodes of T1 in
preorder, followed by the nodes of T2 in preorder - Each node is visited BEFORE its left and right
subtrees are traversed
27Example
A
B
C
D
E
F
G
H
I
On the path shown in this figure, list each node
the first time you pass to produce the preorder
traversal
28Inorder Traversal
- If a tree T is null, then the inorder traversal
is the empty list - If T consists of a single node, then that node by
itself is the inorder listing (traversal) of T - Otherwise, the inorder listing of the nodes of T
is the nodes of T1 in inorder, followed by node
n, followed by the nodes of T2 in inorder - Root is visited AFTER its left subtree has been
traversed but BEFORE its right is examined
29Example
A
B
C
D
E
F
G
H
I
On the path shown in this figure, list a leaf
the first time we pass it, but list an interior
node the second time we pass it
30Postorder Traversal
- If a tree T is null, then the postorder traversal
is the empty list - If T consists of a single node, then that node by
itself is the postorder listing (traversal) of T - Otherwise, the postorder listing of the nodes of
T is the nodes of T1 in postorder, followed by
the nodes of T2 in postorder, followed, followed
by node n - Each root is visited after its left and right
subtrees have been visited
31Example
A
B
C
D
E
F
G
H
I
On the path shown in this figure, list a node
the last time we pass it, as we move up to its
parent
32Expression Tress - Revisited
- Recall, we can use binary trees to represent
arithmetic expressions
1. Every leaf is labeled by an operand 2. Every
interior node is labeled by an operator 3. Each
subtree represents an arithmetic expression
/
a
e
-
c
d
33Expression Trees Contd
- Given an expression tree T
- The preorder, inorder, postorder traversal of T
gives us what is known as the prefix, infix,
postfix (polish) form of the expression,
respectively
34Example
Preorder / a e - c d Inorder
a / e c - d Postorder
a e c d - / 1. An expression presented as a
binary tree is unambiguous. 2. When listed,
preorder and postorder are unambiguous. Infix
expressions need priority rules and parenthesis
/
a
e
-
c
d
35Level Order Traversal
- Elements are visited by level from top to bottom
- Within levels, elements are visited left-to-right
A
B
C
D
E
F
G
H
I
36Level Order Contd
- Hard to write recursive procedure
- Use queue instead
- For each node, put all of its children on queue
- Remove next element from queue, print, and add
its children (left to right)
37ADT Binary Tree
- Mathematical Model
- Collection of elements root together with a left
subtree and right subtree each subtree is also a
binary tree or empty. - Operations many only list common ones
- isEmpty()
- root()
- makeTree(root, left,right)
- removeLeftSubtree()
- removeRightSubtree()
- preOrder, inOrder, postOrder, levelOrder
38Binary Tree - Java Interface
- public interface BinaryTree
-
- public boolean isEmpty()
- public Object root()
- public void makeTree(Object root, Object left,
Object right) - public BinaryTree removeLeftSubtree()
- public BinaryTree removeRightSubtree()
- public void preOrder(Method visit)
- public void inOrder(Method visit)
- public void postOrder(Method visit)
- public void levelOrder(Method visit)