Beruflich Dokumente
Kultur Dokumente
Stock
Fraud
Ponzi
Scheme
Trees
Bank
Robbery
What is a Tree
In computer science,
a tree is an abstract
model of a
hierarchical structure
A tree consists of
nodes with a parentchild relation
Applications:
ComputersRUs
Sales
US
International
Organization charts
File systems
Europe
Programming
environments
Manufacturing
Trees
Asia
Laptops
R&D
Desktops
Canada
Tree Terminology
Trees
Subtree: tree
consisting of a node
and its descendants
A
subtree3
Binary Trees
Trees
arithmetic
expressions
decision processes
searching
Applications:
I
4
2
a
2010 Goodrich, Tamassia
1
Trees
Decision Tree
Yes
On expense account?
No
Yes
Trees
No
Caf Paragon
6
a
b
Binary Trees
Binary Trees
example
internal node
external node
Notation
Properties:
e i 1
n 2e 1
h i
h (n 1)2
e 2h
h log e
2
n number of nodes
e number of
external nodes
i number of
internal nodes
h height
Trees
h log2 (n 1) 1
12
Property 1
d
Level
d
has
at
most
2
n number of nodes
nodes
e number of
external nodes
0=1
i number of
1=2
internal nodes
2 = 4 = 22
h height
3 = 8 = 2 * 4 = 23
Let it be true for d-1 level
d-1 = 2d-1
D = 2* 2d-1 = 2d
Notation
Trees
13
Property 2
A full binary tree of height h
n number of nodes
h+1
has
(2
1) nodes.
e number of
N = 20 + 21 + ... 2h
external nodes
= 2h+1 1
i number of
Notation
internal nodes
h height
Property 2.1
Height of a binary tree of N
nodes = log(N)
N = 2h+1 1 = N + 1 = 2h+1
h + 1 = log(N+1)
H = log(N+1) 1 = Log(N)
Trees
14
Notation
Property 4
Notation
n number of nodes
e number of
N = 2e 1;
Proof ??? .. Based on
previous ?
Trees
16
BinaryTree ADT
The BinaryTree
ADT extends the
Tree ADT, i.e., it
inherits all the
methods of the
Tree ADT
Additional
methods:
position p.left()
position p.right()
Trees
Update methods
may be defined
by data structures
implementing the
BinaryTree ADT
Proper binary
tree: Each node
has either 0 or 2
children
17
Preorder Traversal
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
1. Motivations
2. Methods
1.1 Greed
1.2 Avidity
2.1 Stock
Fraud
Trees
2.2 Ponzi
Scheme
References
8
2.3 Bank
Robbery
18
Postorder Traversal
In a postorder traversal, a
node is visited after its
descendants
Application: compute space
used by files in a directory
and its subdirectories
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
cs16/
homeworks/
h1c.doc
3K
h1nc.doc
2K
todo.txt
1K
programs/
DDR.cpp
10K
Trees
Stocks.cpp
25K
6
Robot.cpp
20K
19
Inorder Traversal
In an inorder traversal a
node is visited after its
left subtree and before
its right subtree
Application: draw a
binary tree
Algorithm inOrder(v)
if v.isExternal()
inOrder(v.left())
visit(v)
if v.isExternal()
inOrder(v.right())
4
3
5
Trees
20
D
C
/
B
inorder traversal
A/ B * C * D + E
infix expression
preorder traversal
+ * * / AB C D E
prefix expression
postorder traversal
AB / C * D * E +
postfix expression
level order traversal
+ * E * D / C AB
Specialization of an inorder
traversal
2
a
Algorithm printExpression(v)
if v.isExternal()
print(()
inOrder(v.left())
print(v.element())
if v.isExternal()
inOrder(v.right())
print ())
Trees
22
Evaluate Arithmetic
Expressions
Specialization of a
postorder traversal
recursive method
returning the value of a
subtree
when visiting an internal
node, combine the values
of the subtrees
Algorithm evalExpr(v)
if v.isExternal()
return v.element()
else
x evalExpr(v.left())
y evalExpr(v.right())
operator stored at v
return x y
2
5
Trees
23
1
Trees
24
A node is represented
by an object storing
Element
Parent node
Left child node
Right child node
B
A
D
C
E
Trees
25
Array-Based Representation of
Binary Trees
1
A
10
11
3
B
Node
v is stored at A[rank(v)]
4
rank(root) = 1
E
if node is the left child of parent(node),
rank(node) = 2 rank(parent(node))
if node is the right child of parent(node),
10
rank(node) = 2 rank(parent(node)) G
1
Trees
7
C
11
H
26
class Tree {
public:
typedef int datatype;
Tree(TreeNode *rootPtr=NULL){this->rootPtr=rootPtr;};
TreeNode *search(datatype x);
bool insert(datatype x); TreeNode * remove(datatype x);
TreeNode *getRoot(){return rootPtr;};
Tree *getLeftSubtree(); Tree *getRightSubtree();
bool isEmpty(){return rootPtr == NULL;};
private:
TreeNode *rootPtr;
};
28