Trees - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Trees

Description:

... is a widely-used data structure that emulates a tree structure with a set of linked nodes. ... Being the topmost node, the root node will not have parents. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 29
Provided by: phi762
Learn more at: http://www.cs.gsu.edu
Category:
Tags: emulates | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
Saurav Karmakar
2
TREE
  • A tree is a widely-used data structure that
    emulates a tree structure with a set of linked
    nodes.

3
Some Trees
  • Root nodes
  • The topmost node in a tree is called the root
    node. Being the topmost node, the root node will
    not have parents. It is the node at which
    operations on the tree commonly begin.
  • Leaf nodes
  • Nodes at the bottom most level of the tree are
    called leaf nodes. Since they are at the bottom
    most level, they will not have any children.
  • Subtrees
  • A subtree is a portion of a tree data structure
    that can be viewed as a complete tree in itself.
    Any node in a tree T, together with all the nodes
    below it, comprise a subtree of T. The subtree
    corresponding to the root node is the entire tree

4
General Trees
  • Nonrecursive definition a tree consists of a set
    of nodes and a set of directed edges that connect
    pairs of nodes.
  • Recursive definition Either a tree is empty or
    it consists of a root and zero or more nonempty
    subtrees T1, T2, Tk, each of whose roots are
    connected by an edge from the root.

Root
T2
T1
Tk

subtrees
5
Rooted Trees
  • In this class, we consider only rooted trees. A
    rooted tree has the following properties
  • One node is distinguished as the root.
  • Every node c, except the root, is connected by an
    edge from exactly one other node p.
  • Node p is cs parent, and c is one of ps
    children. acyclic property
  • A unique path traverses from the root to each
    node.

6
General Terms
  • Path length the number of edges on the path from
    a node to another.
  • Depth of a node the length of the path from the
    root to the node.
  • Height of a node the length of the path form the
    node to the deepest leaf.
  • Siblings Nodes with the same parent.
  • Size of a Node the number of descendants the
    node has (including the node itself). The size of
    root is the size of a tree. The size of a leaf is
    1.

Node Height Depth Size A 3 0
8 B 1 1 3 C 0 1 1
D 2 1 3 E 0 2 1 F 0 2
1 G 1 2 2 H 0 3 1
7
Tree example Directory
8
Trace the SIZE function
9
Trace the SIZE function
10
Representation Of a General Tree -- first
child/next sibling
  • Example for this tree

Cannot directly access D from A.
11
Tree Object
  • template ltclass Objectgt
  • class TreeNode
  • public
  • TreeNode( const Object the element Object(),
    TreeNode c NULL, TreeNode sNULL )
  • public
  • Object element
  • TreeNode child
  • TreeNode sibling

12
Binary tree (BT)
A binary tree is either empty, or it consists of
a node called the root together with TWO binary
trees called the left subtree and the right
subtree of the root.
A binary tree is a tree in which no node can have
more than two children.
13
Representation of Binary Trees
Parent Node is the one between the node and the
root of the tree.
parent
ParentPtr
Key value
Right C
Left C
leaves
left child
right child
Leaves are nodes that have no children.
Child Node is the one between the node and the
leaves of the tree.
14
Small binary trees
Empty tree
Tree of size 1
Tree of size 2
Tree of size 3
15
Binary Tree Applications
  • Expression tree
  • A central data structure in compiler design. The
    leaves of an expression tree are operands the
    other nodes contain operators.
  • Huffman coding tree
  • Implement a simple but relatively effective data
    compression algorithm.
  • Binary Search Tree (BST)
  • Will discuss in chapter 19.

16
Example Code of Recursion
  • includeltiostreamgt
  • using namespace std
  • void recur(int x)
  • if (xgt0)
  • coutltltxltltendl
  • recur(x-1)
  • int main()
  • recur(10)
  • return 0

17
Binary Tree Object
  • template ltclass Objectgt
  • class BinaryNode
  • public
  • BinaryNode( const Object the element
    Object(), BinaryNode lt NULL, BinaryNode
    rtNULL )
  • public
  • Object element
  • BinaryNode left
  • BinaryNode right

18
Recursion and Trees
Because tress can be defined recursively, many
tree routines, not surprisingly, are most easily
implemented by using recursion.
Any non-empty tree consists of the root node, its
left subtree and its right subtree. (The subtree
may be empty). Because the subtrees are also
tree, if an operation works for tree, we can also
apply it on the subtrees.
19
Tree size
int TreeSize (root TreePointer)
begin if rootnull //this is
left/right child point of a leaf then
return 0 else return 1
TreeSize(root-gtleft) TreeSize(root-gtright)
end
Size of a Node the number of descendants the
node has (including the node itself). The size of
root is the size of a tree. The size of a leaf is
1.
20
Tree height
Int height ( root ) begin if rootnull
//this is left/right child point of a leaf
return -1 else return 1
max(height(root-gtleft), height(root-gtright))
endif end
Height of a node the length of the path from the
node to the deepest leaf.
21
Traversal
  • Three standard traversal order
  • preorder - V L R
  • inorder - L V R
  • postorder - L R V

Inorder traverse all nodes in the LEFT subtree
first, then the node itself, then all nodes in
the RIGHT subtree.
Preorder traverse the node itself first, then
all nodes in the LEFT subtree , then all nodes in
the RIGHT subtree.
Postorder traverse all nodes in the LEFT subtree
first, then all nodes in the RIGHT subtree, then
the node itself,
22
Recursive Traversal Implementation
Void PrintPreorder (root) if root ! null
print(root-gtdata) PrintPreorder(root-
gtleft) PrintPreorder(root-gtright)
endif
preorder 1 2 4 5 3 6 inorder 4 2 5 1 3
6 postorder 4 5 2 6 3 1
Void PrintInorder (root) if root ! null
PrintInorder(root-gtleft)
print(root-gtdata) PrintInorder(root-gtrigh
t) endif
Void PrintPostorder (root) if root ! null
PrintPostorder(root-gtleft)
PrintPostorder(root-gtright)
print(root-gtdata) endif
The difference is the order of the three
statements in the IF.
23
Traversal
preorder 1 2 4 5 3 6 inorder 4 2 5 1 3
6 postorder 4 5 2 6 3 1
preorder 1 ... inorder 1 ... postorder
1
24
Designing a Nonrecursive Traversal
  • Consider the algorithm for an inorder traversal
  • If the current node is not null traverse the
    left subtree process the current node
    traverse the right subtree
  • End if
  • When traversing the left subtree, the stack of
    activation records remembers the postponed
    obligations of processing the current node and
    traversing the right subtree
  • A nonrecursive version of the algorithm would
    have to use an explicit stack to remember these
    obligations

25
A Nonrecursive Preorder Traversal
  • Recursion is a convenient way to postpone tasks
    that will be completed at a later time
  • For example, in a preorder traversal, the task of
    traversing the right subtree is postponed while
    the left subtree is being traversed
  • To eliminate recursion, you must use a stack to
    remember postponed obligations

26
A non-recursive preorder traversal
  • Stack S
  • push root onto S
  • repeat until S is empty
  • v pop S
  • If v is not NULL
  • visit v
  • push vs right child onto S
  • push vs left child onto S

preorder 1 2 4 5 3 6 inorder 4 2 5 1 3
6 postorder 4 5 2 6 3 1
27
A non-recursive inorder traversal
  • Stack S
  • Initialize all nodes to white
  • push root onto S
  • repeat until S is empty
  • v pop S
  • If v is black
  • visit v
  • else if v is not NULL
  • push vs right child onto S
  • change v to black
  • push (black) v onto S
  • push vs left child onto S

preorder 1 2 4 5 3 6 inorder 4 2 5 1 3
6 postorder 4 5 2 6 3 1
28
Level-Order Traversal -- Breadth First Search
(BFS)
Level order 1,2,3,4,5,6
Queue Q enqueue root onto Q repeat
until Q is empty v dequeue Q
If v is not NULL visit v
enqueue vs left child onto Q
enqueue vs right child onto Q
Write a Comment
User Comments (0)
About PowerShow.com