Академический Документы
Профессиональный Документы
Культура Документы
General Binary
General tree
Binary Tree
Root
Parent
Child
11/9/2015
Root
Node
Parent
Leaf
Child
DFR/JS TREES 1
Node
Leaf
2
11/9/2015
DFR/JS TREES 1
11/9/2015
DFR/JS TREES 1
11/9/2015
Ordered Trees
AVL
Tree
11/9/2015
DFR/JS TREES 1
General tree
Root
In-degree 0
Out-degree n
(max)
11/9/2015
In-degree 1
Out-degree n (max)
Leaf Node
Binary Tree
Root
Node
Node
In-degree 1
Out-degree 0
In-degree 1
Out-degree 2 (max)
Leaf Node
DFR/JS TREES 1
In-degree 0
Out-degree 2
(max)
In-degree 1
Out-degree 0
7
UNORDERED
11/9/2015
Tree Traversals
Breadth
First
Search
level by
level
uses a
Queue.
a, b, c, d, e, f, g, h, i, j
11/9/2015
DFR/JS TREES 1
Cn ::= GT
RN = Root Node C = child
Empty tree; tree with 1 node; tree with n nodes
A collection of nodes and relationships (parent/child)
11/9/2015
DFR/JS TREES 1
10
::= LC RN RC | empty
::= element
::= BT
::= BT
11/9/2015
DFR/JS TREES 1
11
a
b
c
f
h
g
DFR/JS TREES 1
d
i
j
12
11/9/2015
(rule 1)
(rule 2)
(rule 2)
13
Height(empty tree) = 0
Height(one node) = 1
Height(empty tree) = 0
Height(one node) = 1
11/9/2015
14
Height(empty tree) = -1
Height(one node) = 0
Height(empty tree) = -1
Height(one node) = 0
11/9/2015
15
11/9/2015
DFR/JS TREES 1
16
a
c
17
Height = 4, #nodes = 7
4 7 15
a
c
d
f
Example 2
11/9/2015
Height = 2, #nodes = 3
233
DFR/JS TREES 1
18
Breadth-first
Depth-first
pre-order
in-order
post-order
general-order
NLR
N = node
LNR
L = left
LRN
R = right
N1 go(L) N2 go(R) N3
11/9/2015
DFR/JS TREES 1
19
DFR/JS TREES 1
20
InOrder(T) {
if !is_Empty(T){ InOrder(Left(T)); process(Root(T)); InOrder(Right(T));}
}
PostOrder(T) {
if !is_Empty(T){ PostOrder(Left(T)); PostOrder(Right(T)); process(Root(T));}
}
11/9/2015
DFR/JS TREES 1
21
Arithmetic
expressions
(a+b) * (c-d)
Pre
*+ab-cd
In a+b*c-d !!
In (a+b)*(c-d)
Post ab+cd-*
Infix pre-/post-fix
11/9/2015
*
+
a
DFR/JS TREES 1
22
stack:
stack:
stack:
stack:
stack:
* ( o/p: ab+c
* (- o/p: ab+c
* (- o/p: ab+cd
* o/p:ab+cdo/p: ab+cd-*
Operator
(: Push
): Pop to (
*,+
stack / pop
but note precedence!
a+b*c abc*+
a*b+c ab*c+
Operand:
Output
DFR/JS TREES 1
23
a
a
b
a
11/9/2015
DFR/JS TREES 1
24