An abstract model of a hierarchical structure
Terminology | Explain | Example |
---|---|---|
Root | node without parent | A |
Child | if node u is the parent of node v, v is a child of u | except A |
Internal node | node with at least one child | A, B, C, F |
External node (=Leaf) | node without children | E, I, J, K, G, H, D |
Ancestors of a node | parent, grandparent, great-grandparent, etc. | |
Siblings of a node | Any node which shares a parent | |
Depth of a node | number of ancestors (노드의 속성) | K의 경우, A, B, F이므로 3 |
Height of a tree | maximum depth of any node (트리의 속성) | 3 |
Descendant of a node | child, grandchild, great-grandchild, etc. | |
Subtree | tree consisting of a node and its descendants (부분집합 같은 개념) | |
Degree | number of children of a node (트리의 속성) | F의 경우, I, J, K이므로 3 |
Edge | a pair of node (u, v) such that u is a parent of v((C, H)) | |
Path | A sequence of nodes such that any two consecutive nodes form an edge | A, B, F, J |
Ordered tree | a tree with a linear ordering defined for the children of each node |
We use positions to abstract nodes
integer size()
boolean empty()
position root()
list<position> positions()
position p.parent()
list<position> p.children()
boolean p.isRoot()
boolean p.isExternal()
isRoot()
: O(1)
isExternal()
: O(1)
parent()
: O(1)
p.children()
: O(c_p)
size()
: O(1)
empty()
: O(1)
root()
: O(1)
positions()
: O(n)
O(n)
O(dp)
where dp ≤ nAlgorithm depth(T, p)
if p.isRoot()
return 0
else
return 1+depth(T,p.parent())
O(n)
Algorithm height(T, p)
if p.isExternal()
return 0
else
h=0
for each q ∈ p.children()
h = max(h,height(T,q))
return 1+h
r
,called the root of T
and storing an elementT
T
n
: number of nodesn_E
: number of external nodesn_I
: number of internal nodesh
: heightT
:
isRoot() isExternal() p.parent() p.children() size() empty() root() positions()
position p.left()
position p.right()
Proper binary tree
: Each node has either 0 or 2 children (otherwise improper)Provide the means to build and modify trees
addRoot()
: Add a root to an empty treeexpandExternal(p)
: Create two new external nodes and making them the left and right children of p
removeAboveExternal(p)
: Remove the external node p
together with its parent q
, then replace q
with the sibling of p
An example operation of removeAboveExternal(w)
T
v
is the root of T
, then f(v)=1
v
is the left child of node u
, then f(v)=2f(u)
v
is the right child of node u
, then f(v)=2f(u)+1
T
v
is associated with the element of a vector S
at f(v)
S
has size (N=f_M+1<=2^n-1
)
f_M
: the maximum value of f(v)
+1
의 이유: 0번 index도 포함되기 때문n
은 노드의 개수h
와 n
의 관계: height가 node가 됨O(n)
O(2^n)
Algorithm preOrder(T,p)
visit(p)
for each child q of p
preOrder(T,q)
A B E F I J K C G H D
Algorithm postOrder(T,p)
for each child q of p
postOrder(T,q)
visit(p)
E, I, J, K, F, B, G, H, C, D, A
x(v)
= inorder rank of v
y(v)
= depth of v
Algorithm inOrder(T,p)
if not p.isExternal()
inOrder(T,p.left())
visit(p)
if not p.isExternal()
inOrder(T,p.right())
D, B, H, E, I, A, F, C, G
preorder
)inorder
)postorder
)Algorithm eulerTour(T,p)
visit(p) on the left // preOrder
if not p.isExternal()
eulerTour(T,p.left())
visit(p) from below // inOrder
if not p.isExternal()
eulerTour(T,p.right())
visit(p) on the right // postOrder
+ X 2 2 2 X - 5 5 5 - 1 1 1 - X + X 3 3 3 X 2 2 2 X +
(2X(a-1))+(3Xb)
(
before traversing left subtree)
after traversing right subtreeAlgorithm printExpression(T,p)
if not p.isExternal()
print("(")
inOrder(T,p.left())
print(p.element())
if not p.isExternal()
inOrder(p.right())
print (")")
((2X(a-1))+(3Xb))
(
)
Algorithm printExpEuler(T,p)
if not p.isExternal()
print(p.element())
else
print("(")
printExpEuler(T,p.left())
print(p.element())
printExpEuler(T,p.right())
print (")")
Algorithm evalExpr(T,p)
if p.isExternal()
return p.element()
else
x ← evalExpr(T, p.left())
y ← evalExpr(T, p.right())
♢ ← operator stored at p
return x ♢ y
+,-,*,/