Trees and Binary Trees: Become Rich
Trees and Binary Trees: Become Rich
BINARY TREES
Become Rich
leaves
branches
root
COMPUTER SCIENTIST’S VIEW
root
leaves
branches
nodes
WHAT IS A TREE
A tree is a finite nonempty set
of elements.
It is an abstract model of a
hierarchical structure. Computers”R”Us
consists of nodes with a
parent-child relation.
Applications: Sales Manufacturing R&D
Organization charts
File systems
Programming environments
US International Laptops Desktops
subtree
TREE PROPERTIES
Property Value
A Number of nodes
Height
Root Node
B C Leaves
Interior nodes
Ancestors of H
D E F Descendants of B
Siblings of E
Right subtree of A
Degree of this tree
G
H I
TREE ADT
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of links to sub-trees
Data
A node is represented by an
object storing
Element
Parent node B
Sequence of children nodes
A D F
B
A D F
C E
C E
LEFT CHILD, RIGHT SIBLING
REPRESENTATION
Data
Left Right
Child Sibling A
B C D
E F G H I
J K L
TREE TRAVERSAL
Two main methods:
Preorder
Inorder
Postorder
Recursive definition
Preorder:
visit the root
traverse in preorder the children (subtrees)
Postorder
traverse in postorder the children (subtrees)
visit the root
PREORDER TRAVERSAL
A traversal visits the nodes of a tree in a Algorithm preOrder(v)
systematic manner visit(v)
In a preorder traversal, a node is visited for each child w of v
before its descendants
preorder (w)
Application: print a structured document
1
Become Rich
2 5 9
1. Motivations 2. Methods 3. Success Stories
3 4 6 7 8
1.1 Enjoy 1.2 Help 2.1 Get a CS 2.2 Start a 2.3 Acquired
Life Poor Friends PhD Web Site by Google
POSTORDER TRAVERSAL
In a postorder traversal, a node is Algorithm postOrder(v)
visited after its descendants for each child w of v
Application: compute space used by postOrder (w)
files in a directory and its
visit(v)
subdirectories
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc
h1c.doc h1nc.doc
h1nc.doc DDR.java
DDR.java Stocks.java
Stocks.java Robot.java
Robot.java
3K
3K 2K
2K 10K
10K 25K
25K 20K
20K
BINARY TREE
A binary tree is a tree with the following Applications:
properties: arithmetic expressions
Each internal node has at most two children decision processes
(degree of two)
The children of a node are an ordered pair searching
H I
BINARYTREE ADT
The BinaryTree ADT extends Update methods may be
the Tree ADT, i.e., it inherits defined by data structures
all the methods of the Tree implementing the BinaryTree
ADT ADT
Additional methods:
position leftChild(p)
position rightChild(p)
position sibling(p)
Examples of the Binary Tree
Skewed Binary Tree Complete Binary Tree
A 1 A
A
B B 2
B C
C
3 D E F G
D
4 H I
E 5
DIFFERENCES BETWEEN A TREE AND A BINARY
TREE
A A
B B
B A D
A D
C E
C E
ARITHMETIC EXPRESSION TREE
2 - 3 b
a 1
DECISION TREE
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Yes No
Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon
Maximum Number of Nodes in a
Binary Tree
Prove by induction.
k
2 i
i 0
2 k 1
1
FULL BINARY TREE
2 3
4 5 6 7
8 9 10 11 12 13 14 15
NODE NUMBER PROPERTIES
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
1 1
2 2 3
3
4 5 6 7 4 5 6 7
8 9 10 11 12 13 14 15
8 9
Complete binary tree Full binary tree of depth 3
Binary Tree Traversals
Let l, R, and r stand for moving left, visiting
the node, and moving right.
2 8
1 4 7 9
3 5
PRINT ARITHMETIC EXPRESSIONS
Specialization of an inorder traversal Algorithm inOrder (v)
print operand or operator when visiting if isInternal (v){
node
print “(“ before traversing left subtree print(“(’’)
print “)“ after traversing right subtree inOrder (leftChild (v))}
print(v.element ())
if isInternal (v){
inOrder (rightChild (v))
+ print (“)’’)}
a 1
EVALUATE ARITHMETIC EXPRESSIONS
2 - 3 2
5 1
CREATIVITY:
PATHLENGTH(TREE) = DEPTH(V) V TREE
Algorithm pathLength(v, n)
Input: a tree node v and an initial value n
Output: the pathLength of the tree with root v
Usage: pl = pathLength(root, 0);
if isExternal (v)
return n
return
(pathLength(leftChild (v), n + 1) +
pathLength(rightChild (v), n + 1) + n)
EULER TOUR TRAVERSAL
Generic traversal of a binary tree
Includes a special cases the preorder, postorder and inorder traversals
Walk around the tree and visit each node three times:
on the left (preorder)
from below (inorder)
on the right (postorder)
L R
B
2 - 3 2
5 1
EULER TOUR TRAVERSAL
eulerTour(node v) {
perform action for visiting node on the left;
if v is internal then
eulerTour(v->left);
perform action for visiting node from below;
if v is internal then
eulerTour(v->right);
perform action for visiting node on the right;
}
TREE
BINARY TREE
sebuah pengorganisasian secara hirarki dari
beberapa buah simpul, dimana masing-masing
simpul tidak mempunyai anak lebih dari 2.
Simpul yang berada di bawah sebuah simpul
dinamakan anak dari simpul tersebut.
Simpul yang berada di atas sebuah simpul
dinamakan induk dari simpul tersebut.
BINARY TREE
ISTILAH DALAM TREE
Term Definition
Node Sebuah elemen dalam sebuah tree; berisi sebuah informasi
Parent Node yang berada di atas node lain secara langsung; B adalah
parent dari D dan E
Child Cabang langsung dari sebuah node; D dan E merupakan children
dari B
Root Node teratas yang tidak punya parent
Sibling Sebuah node lain yang memiliki parent yang sama; Sibling dari
B adalah C karena memiliki parent yang sama yaitu A
Leaf Sebuah node yang tidak memiliki children. D, E, F, G, I adalah
leaf. Leaf biasa disebut sebagai external node, sedangkan node
selainnya disebut sebagai internal node. B, A, C, H adalah
internal node
Level Semua node yang memiliki jarak yang sama dari root.
Alevel 0; B,Clevel 1; D,E,F,G,Hlevel 2; Ilevel 3
Depth Jumlah level yang ada dalam tree
Complete Semua parent memiliki children yang penuh
Balanced Semua subtree memiliki depth yang sama
STRUKTUR BINARY TREE
B C
D E F G
ABDECFG
INORDER
Kunjungan secara inorder, juga sering disebut dengan
symmetric order, menggunakan urutan:
Kunjungi cabang kiri
B C
D E F G
DBEAFCG
POSTORDER
Kunjungan secara postorder menggunakan urutan:
Kunjungi cabang kiri
B
B C
C
D
D EE F G
DEBFGCA
REFERENCES
http://www.cse.unt.edu/~huangyan/3110/Lectures/Trees.
ppt
CONSTRUCT BINARY TREE
PreOrder : Root L R
InOrder : L Root R
PostOrder : L R Root
BINARY TREE TRAVERSAL
SILAHKAN DI KERJAKAN
Create Tree :
PreOrder : 15 8 2 6 3 7 11 10 12 14 20 27 22 30
InOrder : 2 3 6 7 8 10 11 12 14 15 20 22 27 30
Postorder : ?
YC U N PO G R IAT H E B L
TEST/TUGAS