Introduction to Trees - PowerPoint PPT Presentation

1 / 75
About This Presentation
Title:

Introduction to Trees

Description:

Every node of a tree is the root of a subtree. ... Binary Trees ... As long as we are willing to convert general trees to binary ones. ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 76
Provided by: jimal
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Trees


1
Introduction to Trees
2
Tree example
  • Consider this program structure diagram as itself
    a data structure.

main
readin
print
process
sort
lookup
3
Terminology
  • Node - an element of a tree
  • Root node - the single node at level 0
  • Child node - nodes at level 1 or lower
    (descending from a parent node). Note a node
    can have only one parent.

4
Terminology (continued)
  • Parent node - a node at one level associated with
    nodes at a lower level (children).
  • Note nodes without children are called leaves.
  • Every node of a tree is the root of a subtree.

5
  • As you can see by the diagram of a tree, its
    structure is highly recursive. Therefore, as you
    would expect, the procedures and functions that
    manipulate trees are also recursive in nature.

6
The tree as an ADT
  • Fundamental operationsCreate(Root)Empty(Root)Fi
    nd(Root,Target,TargetLoc)Add(Root,Node)Delete(Ro
    ot,Node)Traverse(Root)

7
Binary Trees
  • This (next slide) is an example of a binary tree
    (ie. one in which each node has no more than two
    children)

8
Binary tree example
9
(see previous tree)
  • This tree also has the property that each node is
    greater than either of its children. Another
    name for it is a heap (not to be confused with
    the system heap).

10
Question
  • What is the hierarchical ordering principal for
    this tree?
  • (see next slide)

11
What is the ordering property?
12
Answer
  • Each node is greater than all of the nodes in its
    left subtree and less than all of the nodes in
    its right subtree.
  • This is called its ordering property.

13
Trees representing arithmetic expressions.
  • For example (A-B)(C(E/F))

14
Example
15
  • compare this tree to a tree for the expression
    (A-B)(CE/F)

16
  • Notice how small changes in the order of
    operations (like removing a set of parentheses)
    make major changes in the structure of the tree.

17
Reverse Polish Again!?!
  • Recall that expressions are easily evaluated
    using a stack if they are available in postfix
    form AB-CEF/
  • Trees can be traversed in such a way as to
    produce the postfix form. eg. a postorder
    traversal yields the postfix form of the
    expression.
  • More on this later.

18
Implementing Binary Trees
  • Two methods
  • 1. Linear representation - array
  • 2. Linked representation - pointers

19
Linear representation (Depth3)
  • 1. Allocate an array of size 2(d1)-1
  • 2. Store root in location 1
  • 3. For node in location n, store left child in
    location 2n, right child in location 2n1

20
(No Transcript)
21
Tradeoffs
  • 1. Fast access (given a node, its children and
    parent can be found very quickly)
  • 2. Slow updates (inserts and deletions require
    physical reordering)
  • 3. Wasted space (partially filled trees)

22
Linked Representation
  • Use records with pointer fields(A-B)(C(E/F))

23
(No Transcript)
24
Using an array of records
  • The array implementation of this ADT looks like
    this (next slide)
  • Insertions and deletions involve only
    manipulation of pointers

25
Array of structures
26
Insertion (A-B)(P-C(E/F))
27
Deletion (A-B)(P-E/F)
  • The changes to the array of records look like
    this after both of the operations (insertion of
    -P and deletion of C) have been performed.

28
(No Transcript)
29
(No Transcript)
30
Tradeoffs
  • 1. Faster updates
  • 2. Extra memory for pointers
  • 3. Wasted space for nil pointers (can be
    corrected by threading)
  • 4. Difficult to determine a node's parent
    (corrected by adding a parent pointer)

31
Traversing Binary Trees
  • A tree traversal visits each node of a tree
    exactly once

32
Modes of traversal
  • When visiting a node we have three choices
  • 1. Process the data
  • 2. Remember location of the current node and
    process nodes in the left subtree
  • 3. Remember location of the current node and
    process nodes in the right subtreeThe choice
    made dictates the order of node processing for
    the entire tree

33
Example tree (T)
34
Steps for a Preorder traversal of a binary tree
  • 1. Process root
  • 2. Do a preorder traversal of the nodes in the
    root's left subtree(recursively)
  • 3. Do a preorder traversal of the nodes in the
    root's right subtree (recursively)

35
  • Assume the we have a procedure called Process
    that simply writes out the data contained in any
    given node.
  • Then, using process, a preorder traversal of tree
    T produces ABC/EF (the prefix form of
    (A-B)C(E/F))

36
Steps for an Inorder traversal of a binary tree
  • 1. Recursively visit left subtree
  • 2. Process root
  • 3. Recursively visit right sub-tree

37
Steps for a Postorder traversal of a binary tree
  • 1. Recursively visit left subtree
  • 2. Recursively visit right subtree
  • 3. Process Root

38
  • A postorder traversal of T produces AB- CEF/
    (the postfix form)
  • These traversals can be done on any binary tree,
    not just expression trees.

39
Implementing lists using binary trees
40
Implementing the List ADT
  • Vehicle Advantage Disadvantage
  • Array Fast search Slow inserts
    O(log2N) and deletions
    O(N)
  • Linked Fast inserts Slow search List and
    deletions O(N) O(1)
  • Binary Fast inserts Tree must be
    Tree and deletions balanced O(log2N)
    The hierarchical relationship we employed
    is the ordering property.

41
Example tree T1
42
  • Inorder traversal of this tree gives
    ADEFGHKMNPSTUVZSo An inorder traversal of a
    binary tree with the ordering property visits the
    nodes in ascending order.

43
  • Adding a node to a binary tree with the ordering
    property
  • Follow the insertion rule "If less than, go
    left, otherwise go right."

44
Example Add the letter R to the previous tree.
  • 1. Compare R to M (the root). Since R M,
    add R to the right subtree
  • 2. Compare R to T (the root of the subtree).
    Since R
  • 3. Compare R to P. Since R P go right
  • 4. Compare R to S. Since R
  • 5. Since the left subtree is NULL add R at
    this pointClearly, the procedure is
    recursive.

45
Adding R to the tree
46
Efficiency of adding to a binary tree
  • Question How many comparisons did we have to
    make?Answer 4 (ie. depth 1)

47
Differing tree configurations
  • Consider another tree T2 representing the same
    list

48
(No Transcript)
49
Question
  • How many comparisons do we have to make to add
    the letter R?Answer 9 (ie. depth 1)

50
Question
  • How do you minimize the depth of a binary
    tree?Answer Make sure it is full, or balanced.

51
Definition
  • A balanced binary tree is one in which every node
    above level depth-1 has exactly two children.

52
Depth to size comparisons
  • When a binary tree of N nodes is balanced, no
    branch will be longer than log(base 2)N1. Thus,
    the efficiency of the adding algorithm is
    O(log(base 2)N)
  • Depth MaxN
  • 0 1 1 3 2 7 3 15 . . .
    9 1023 10 1 million

53
Suppose we add Q to T1
54
Suppose we add Q to T1
  • Now T1 is not balanced.
  • There are methods for balancing trees.

55
Recall the binary search of an array
56
(No Transcript)
57
Binary similarities
  • Each probe into the array is analogous to a
    recursive descent in the search of a binary tree.
    (both are O(log2N))

58
Deleting from a tree
59
For example, remove T from tree T1.
  • The result becomes

60
Deletion strategies
  • Requirement The resulting tree must have the
    ordering property.
  • One method is to find the largest item in T1's
    left sub-tree and replace T with it.

61
Three special cases which might occur
  • 1. Node to be deleted has a left child
  • 2. Node to be deleted has a right child but
    no left child
  • 3. Node to be deleted has no children

62
Case 1
  • Suppose P points to the node to be deleted. There
    are two important subcases
  • 1.a P-Left has no right child
  • 1.b P-Left has a right child

63
Case 1a
64
Case 1b
65
Case 2
66
Case 3
67
List comparison
  • Tradeoffs Linked List Binary Tree
  • adding O(1) O(1) deleting searching O(N) O(
    log2N) balanced O(N) worst case
  • user 1 ptr/node 2 ptrs/node memory (many
    NULL)
  • system stack minimal recursion (remedy -
    threading)

68
General Trees
69
General Trees
  • So far we have been studying binary trees,
    general trees however can have more than two
    children for each node.Question Does the
    former restriction to binary trees mean that we
    cannot apply what we have learned to general
    trees?Answer No! As long as we are willing
    to convert general trees to binary ones.

70
(No Transcript)
71
Binary version of general tree
72
Preorder Traversal
  • (Root, Left, Right)
  • J B D N E P Q K L M C X R T

73
Processing order
  • 1. Process root2. Recursively traverse the
    left-most subtree3. Recursively traverse the
    next subtree to the right4. Repeat step 3
    until there are no more subtrees

74
Postorder Traversal
  • (Left, Right, Root)
  • Q P E N D L X R C T M K B J

75
  • Note that this order on the general tree can be
    described by1. Start at the leaves and work
    up2. Process a node after all nodes below it
    have been processed
Write a Comment
User Comments (0)
About PowerShow.com