Set 9 - Tree ADT

Document Sample
Set 9 - Tree ADT Powered By Docstoc
					                 Tree ADT
Definition
• A tree T is a finite set of one or more
  nodes such that there is one designated
  node r called the root of T, and the
  remaining nodes in (T – { r } ) are
  partitioned into n  0 disjoint subsets T1,
  T2, ..., Tk, each of which is a tree, and
  whose roots r1 , r2 , ..., rk , respectively,
  are children of r.
                Tree ADT
Characteristics
• A tree is an instance of a more general
  category called graph
• A tree consists of nodes connected by
  edges
• Typically there is one node in the top row
  of the tree, with lines connecting to more
  nodes on the second row, even more on
  the third, and so on.
               Tree ADT
Root
• The node at the top of the tree is called
  the root.
• There is only one root in a tree.
• For a collection of nodes and edges to be
  defined a tree, there must be one (and
  only one!) path from the root to any other
  node .
       Tree ADT
Root
                Tree ADT
Path
• If n1, n2, …,nk is a set of nodes in the tree
  such that ni, is the parent of ni+1 for 1 i 
  k, then this set is called a path from n1 to
  nk. The length of the path is k – 1.

• The number of edges that connect nodes
  is the length of the path.
               Tree ADT
Parent
• Any node (except the root) has exactly
  one edge running upward to another node.
  The node above it is called the parent of
  the node.
• If there is a path from node R to node M,
  then R is an ancestor of M.
• Parents, grandparents, etc. are ancestors.
               Tree ADT
Child
• The nodes below a given node are called
  its children.
• If there is a path from node R to node M,
  then M is a descendant of R.
• Children, grandchildren, etc. are
  descendants
                Tree ADT
Leaf and Internal node
• A node that has no children is called a leaf
  node or simply a leaf. There can be only
  one root in a tree, but there can be many
  leaves.
• A node (apart from the root) that has
  children is an internal node.
               Tree ADT
Levels
 The level of a particular node refers to how
 many generations the node is from the
 root. If the root is assumed to be on level
 0, then its children will be on level 1, its
 grandchildren will be on level 2, and so on.
                Tree ADT
Depth
• The depth of a node M in a tree is the
  length of the path from the root of the tree
  to M.

Height
• The height of a tree is one more than the
  depth of the deepest node in the tree.
                Tree ADT
Subtree
• Any node may be considered to be the
  root of the subtree, which consists of its
  children and its children children, and so
  on. If you think in terms of families, a
  node’s subtree contains all its
  descendants.
          Tree ADT
Subtree


                     Subtree
                Tree ADT
Visiting
  A node is visited when program control
  arrives at the node, usually for the purpose
  of carrying out some operation on the node,
  such as checking the value of one of its data
  fields, or displaying it.
  Merely passing over a node on the path form
  one node to another is not considered to be
  visiting the node.
                Tree ADT
Traversing
• To traverse a tree means to visit all of
  the nodes in some specified order.
  – For example, you might visit all the nodes
    in order of ascending key value. There are
    other ways to traverse a tree, as we’ll see
    later.
• If traversal visits all of the tree nodes it
  is called enumeration.
              Tree ADT
Keys
• One data item in an object is usually
  designated a key value. This value is
  used to search for the item or perform
  other operations on it.
               Tree ADT
Binary Tree
• Made up of a finite set of nodes that is
  either empty or consists of a node
  called the root together with two binary
  trees, called the left and right subtrees,
  which are disjoint from each other and
  from the root.
               Tree ADT
Full Binary Trees
• A binary tree where each node is either
  a leaf or is an internal node with exactly
  two non-empty children.

• That means, a node is allowed to have
  either none or two children (but not
  one!)
                 Tree ADT
Complete Binary Trees
• A binary tree whereby if the height is d,
  and all levels, except possibly level d,
  are completely full. If the bottom level
  is incomplete, then it has all nodes to
  the left side.

  – That is the tree has been filled in the level
    order from left to right.
                   Tree ADT




Complete Binary Tree    Not Complete Binary Tree

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:7/10/2012
language:
pages:19