Tuesday 11 March 2014

General Trees


Suitable for representing hierarchies
AbstractDataType tree{ 
  instances 
    A set of elements: (1) empty or having a distinguished root element 
    (2) each non-root element having exactly one parent element 
  operations 
    root() 
    degree() 
    child(k) 
} 
Concepts: tree, subtree, root, leaf, parent, children, level, degree of node, degree of tree, height

Properties

  • Full tree A tree with all the leaves at the same level, and all the non-leaves having the same degree.
                       o
     |---------------------------|
     o             o             o
     |             |             |
|--------|    |---------|   |---------|
o    o   o    o    o    o   o    o    o
  • Complete Tree A full tree in which the ‘last’ elements are deleted.
                  o
     |------------------|
     o           o      o
     |           |
|---------|   |----|
o    o    o   o    o
  • Level of a full tree has dh-1 nodes.
  • The first levels of a full tree have
                           h
1 + d+ d2 + ...+ dh- 1 = d---1
                       d- 1
    nodes.
  • A tree of height and degree has at most dh 1 elements
           {
N (h) =  dN (h- 1)  if h > 1
        1          if h = 1

Traversal

Level order
      1o
 |----|------|
 o    o      o
2|   3      4|
 |        |----|
 o        o    o
5         6    7
x := root() 
if( x ) queue (x) 
while( queue not empty ){ 
  x := dequeue() 
  visit() 
  i=1; while( i <= degree() ){ 
     queue( child(i) ) 
  } 
} 
Preorder
      1o
 |----|------|
 o    o      o
2|   4      5|
 |        |----|
 o        o    o
3         6    7
procedure preorder(x){ 
  visit(x) 
  i=1; while( i <= degree() ){ 
     preorder( child(i) ) 
  } 
} 
Postorder
      7o
 |----|------|
 o    o      o
2|   3      6|
 |        |----|
 o        o    o
1         4    5
procedure postorder(x){ 
  i=1; while( i <= degree() ){ 
     postorder( child(i) ) 
  } 
  visit(x) 
} 
Inorder
Meaningful just for binary trees.
procedure inorder(x){ 
  if( left_child_for(x) ) { inorder( left_child(x)  ) } 
  visit(x) 
  if( right_child_for(x) ) { inorder( right_child(x)  ) } 
} 
Usages for ‘visit’: determine the height, count the number of elements

Representations

  1. Nodes consisting of a data field and k pointers
                  |---|-||--
              |1  ||||||
-------------------------------------
|   | ||| |   |   | || |   |   ||| |||
|-2-|-|||-|   |3--|-||-|   |-4-|||-|||
|---|---|-|          |--|-------- |-----|-|-|
| 5 | | | |          |6 | | | |   | 7 | | | |
----------           ----------   ----------
  2. Nodes consisting of w data field and two pointers: a pointer to the first child, and a pointer to the next sibling.
                  |---|-|---
              |1  |||  |
-------------------|----   ----------
|   | || ------   | | ------   | ||  |
|-2-|-||--|   |3--|-|--|   |-4-|-||--|
|---||||--|          |--|-------- |---|--|--|
| 5 |  |  |          |6 |  | ------ 7 |  |  |
----------           ----------   ----------
  3. A tree of degree assumes an array for holding a complete tree of degree k, with empty cells assigned for missing elements.
    |-|-|-|-|-|-|-|-|-|-||-|
|1|2|3|4|5| | | | | 6|7|
 -----------------------

No comments:

Post a Comment

 

FACEBOOK PAGE

SKETCHES & PAINTINGS