Computer-Science

Trees

An abstract model of a hierarchical structure

1. Tree

Terminology

스크린샷 2021-12-10 오후 2 37 10

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  

Tree ADT

We use positions to abstract nodes

스크린샷 2021-12-12 오후 3 19 26

Linked Structure for Trees

Depth and Height

Algorithm depth(T, p)
  if p.isRoot()
    return 0
  else
    return 1+depth(T,p.parent())
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

2. Binary Trees

스크린샷 2021-12-12 오후 4 55 28

Properties of Binary Trees

Binary Tree ADT

Linked Structure for Binary Trees

스크린샷 2021-12-12 오후 7 58 03

스크린샷 2021-12-12 오후 7 57 40

Binary Tree Update Functions

스크린샷 2021-12-12 오후 8 02 00

Vector-Based Structure of Binary Trees

스크린샷 2021-12-12 오후 8 37 36

Performance of Binary Tree Implementations

스크린샷 2021-12-12 오후 8 42 41

3. Traversal

(1) Preorder Traversal

스크린샷 2021-12-12 오후 8 55 54

Algorithm preOrder(T,p)
  visit(p)
  for each child q of p
    preOrder(T,q)

스크린샷 2021-12-12 오후 8 54 36

Exercise

스크린샷 2021-12-12 오후 4 05 54

Result :

A B E F I J K C G H D

(2) Postorder Traversal

스크린샷 2021-12-12 오후 8 57 09

Algorithm postOrder(T,p)
  for each child q of p
    postOrder(T,q)
  visit(p)

스크린샷 2021-12-12 오후 9 01 54

Exercise: Postorder Traversal

스크린샷 2021-12-12 오후 8 59 05

Result :

E, I, J, K, F, B, G, H, C, D, A

(3) Inorder Traversal

Algorithm inOrder(T,p)
  if not p.isExternal()
    inOrder(T,p.left())
  visit(p)

  if not p.isExternal()
    inOrder(T,p.right())

스크린샷 2021-12-12 오후 8 51 32

Exercise: Inorder Traversal

스크린샷 2021-12-13 오전 10 52 50

Result :

D, B, H, E, I, A, F, C, G

(4) Euler Tour Traversal

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

스크린샷 2021-12-13 오전 11 32 22

Exercise

Result :

+ X 2 2 2 X - 5 5 5 - 1 1 1 - X + X 3 3 3 X 2 2 2 X +

4. Arithmetic Expression Tree

스크린샷 2021-12-12 오후 4 55 55

Exercise

스크린샷 2021-12-12 오후 4 56 22

Result :

(2X(a-1))+(3Xb)

1) Inorder traversal

Algorithm printExpression(T,p)
  if not p.isExternal()
    print("(")
    inOrder(T,p.left())
  print(p.element())
  if not p.isExternal()
    inOrder(p.right())
    print (")")
Exercise

스크린샷 2021-12-13 오전 11 06 11

Result :

((2X(a-1))+(3Xb))

2) Euler Tour traversal

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 (")")

Evaluate Arithmetic Expressions

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

Exercise

Result :

스크린샷 2021-12-13 오전 11 24 14 스크린샷 2021-12-13 오전 11 24 49