Chapter 8 General and Binary Trees - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Chapter 8 General and Binary Trees

Description:

An important class of graphs is those that are structured as trees. ... APE. DOG. BEE. COW. APE. DOG. BEE. BIS3335 2/2006. 27. Sequential Searches ... – PowerPoint PPT presentation

Number of Views:2524
Avg rating:3.0/5.0
Slides: 37
Provided by: Dew
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8 General and Binary Trees


1
Chapter 8General and Binary Trees
2
What is tree?
  • An important class of graphs is those that are
    structured as trees.
  • A tree is an acyclic simple, connected graph.
  • A tree contains no loops and no cycles there is
    no more than one edge between any pair of nodes.

3
General Trees
  • A tree is a finite set of zero or more nodes
    (v1,v2,,vn) such that
  • There is one specially designated node (say v1)
    called Root(T).
  • The remaining nodes (v2,,vn) are partitioned
    into disjoint sets named T1,T2,,Tm such that
    each Ti is itself a tree.

Tree with directed edges
4
General Trees
  • Root(T) A
  • The root has no incoming branches or in-degree
    0
  • Three subtrees are rooted at B, C and D.
  • Leaves of the tree are E, F, K, L, H, I, and J.
  • The leaves have out-degree 0

5
General Trees
  • A is the parent of B, and C.
  • B is a child of A.
  • C is a child of A.
  • B and C have the same parent, so B and C are
    called brothers (or sisters)
  • The nodes of a tree are said to be on level,
    where a nodes level is determined by the length
    of the path from the root to that node.
  • Level 0 A
  • Level 1 B, C
  • Level 2 E,F,G
  • The height of a tree is one plus the number of
    the highest level on which there are node.
  • Height 3
  • The weight of a tree is its number of leaf nodes.
  • Weight 3

6
General Trees
  • Level ?
  • Height ?
  • Weight ?
  • Red color subtree
  • Parent ?
  • Child ?
  • Brothers?
  • Leaves?

A
D
C
B
H
F
G
I
J
E
K
L
7
General Trees
  • Collection of rooted tree is called a forest.

8
Forms of Representation
  • We have used one graphical notation to represent
    trees.
  • Other graphical notations for tree structure
    include
  • Nested sets
  • Nested parentheses
  • Indentation

9
Forms of Representation
  • Nested Set

B
D
E
H
J
A
F
I
K
C
G
L
10
Forms of Representation
  • Nested Parentheses
  • (A(B(E))(C(F)(G(K)(L)))(D(H)(I)(J)))
  • Indentation
  • A-------------------
  • B-------------
  • E----------
  • C--------------
  • F-----------
  • G----------
  • K-----
  • L-----
  • D-------------
  • H---------
  • I----------
  • J---------

11
Forms of Representation
  • Nested set ?
  • Nested parentheses ?
  • Indentation ?

12
Binary Trees
  • A binary tree is a finite set of nodes which
    either is empty or contains two disjoint binary
    trees that are called its left and right
    subtrees.
  • The maximum out-degree of any node of a binary
    tree is 2. One is left node and another one is
    right node.
  • Tree a and tree b are not the same. Tree a with
    left subtree and tree b with right subtree.
  • Tree c is not a binary tree, because its subtree
    does not have leftness or rightness

(c)
(a)
(b)
13
Binary Trees
  • Two binary trees are said to be similar if they
    have the same structure.
  • Two binary trees are said to be equivalent if
    they are similar and contain the same
    information.
  • Tree 1 and Tree 2 are _____
  • Tree 1 and Tree 3 are _____

Tree 3
Tree 2
Tree 1
14
Binary Trees
  • A binary tree is said to be complete if it
    contains the maximum number of nodes possible for
    its height.
  • A complete binary tree with K levels will have 2K
    1 nodes.
  • For example, the following tree with 4 levels
    will have 16-1 15 nodes

Level 0
Level 1
Level 2
Level 3
15
Binary Trees
  • A binary tree with K level is said to be almost
    complete if level 0 through K-2 are full and
    level K-1 is being filled left to right.

(K-2)
(K-1)
16
Binary Trees
  • Is this a complete binary tree?
  • Is this an almost complete binary tree?

17
Representation of Binary Trees
  • Binary trees are most commonly represented by
    linked list.
  • Each node in linked list can be regarded as
    having three elementary fields
  • An information area
  • Pointers to the left
  • Pointers to the right

The topological placement of the boxes of the
linked lists on the page is of course
meaningless Storage for the nodes could be
scattered throughout memory.
18
Binary Tree Representation of General Trees
  • It is considerably easier to represent binary
    trees in programs than it is to represent general
    trees.
  • With a general tree, it is unpredictable how many
    edges will emanate from a node at any given time.
  • Binary trees are attractive in that each node has
    a predictable maximum number of subtree pointers
    2.

19
Binary Tree Representation of General Trees
  • The algorithm for converting a general tree to a
    binary tree form
  • Inserting edges connecting siblings and delete
    all of a parents edges to its children except to
    its leftmost offspring.

20
Binary Tree Representation of General Trees
  • Rotate the resultant diagram 45 degree to
    distinguish between left and right subtrees.

Left pointers are always from a parent node to
its first (leftmost) child in the original
general tree. Right pointers are always from a
node to one of its siblings in the original tree.
21
Binary Tree Representation of General Trees
  • Convert the following general tree to binary tree.

22
Binary Tree Representation of General Trees
  • A forest of general tree can be converted to a
    single binary tree by considering the roots to be
    siblings.

A
H
C
A
B
C
D
I
F
E
D
H
B
E
I
K
G
J
G
F
J
K
23
Binary Tree Representation of General Trees
  • Construct a binary tree from the following forest.

I
24
Example Trees
  • Trees are commonly used to structure data to
    facilitate searches for particular nodes.
  • Trees are also useful for representing
    collections of data that have branching logical
    structures. For example, trees representing
    arithmetic statements and a collection of data.

Tree 1 cde
Tree 3
Tree 2 (((ab)c/d)e?f)/g
  • Searching for a node in the tree can be
    considerably faster than searching the same
    collection sequentially.

25
Binary Search Trees
  • An important application of binary trees is their
    use in searching.
  • The property that makes a binary tree into a
    binary search tree is that for every node, X, in
    the tree, the value of all the items in its left
    subtree are smaller than the item in X, and the
    values of the items in its right subtree are
    larger than the item in X.

26
Binary Search Trees
  • Are they binary search trees?

27
Sequential Searches
  • The process of going through a tree in such a way
    that each node is visited once and only once is
    called tree traversal.
  • When a tree is traversed, its entire collection
    of nodes is looked at. There are several
    well-known methods of binary tree traversal. Each
    imposes a sequential, linear ordering upon the
    nodes of a tree.
  • A node is said to be visited when it is
    encountered in the traversal whatever processing
    is desirable on its contents is done at that
    time.
  • There are three kinds of basic activities in each
    of the binary tree traversal algorithms
  • Visit the root
  • Traverse the left subtree
  • Traverse the right subtree
  • The order in which these type of activities are
    performed leads to three traversal method.

28
Sequential Searches
  • Pre-order traversal
  • Visit the root.
  • Traverse the left subtree
  • Traverse the right subtree
  • In-order traversal
  • Traverse the left subtree
  • Visit the root.
  • Traverse the right subtree
  • Post-order traversal
  • Traverse the left subtree
  • Traverse the right subtree
  • Visit the root.

1
cde
2
4
3
4
3
cde
2
1
4
3
cde
2
1
29
Sequential Searches
  • Pre-order traversal results in a string
    representation in prefix form an operator
    precedes its operands.
  • In-order traversal results in a string
    representation in infix form an operator appears
    between its operands.
  • Post-order traversal results in a string
    representation in postfix form an operator is
    preceded by its two operands.

30
Sequential Searches
  • Find the results of pre-order traversal, in-order
    traversal, and post-order traversal of the
    following two trees.
  • Pre-order ?
  • In-order ?
  • Post-order ?
  • Pre-order ?
  • In-order ?
  • Post-order ?

31
Direct Searches
  • The properties of a binary search tree imply that
    there is a procedure for determining whether or
    not a node with a given key resides in the tree
    and for finding that node when it exists.
  • To find the node with key k in the binary search
    tree rooted at Ri, the following steps are taken.
  • If the tree is empty, the search terminates
    unsuccessfully.
  • If k Ki, the search terminates successfully
    the sought node is Ri.
  • If k lt Ki, the left subtree of Ri is searched.
  • If k gt Ki, the right subtree of Ri is searched.
  • Finding a particular node in a tree that is not a
    binary search tree is usually done with a
    sequential search rather than a direct search,
    unless the tree is specially structured and the
    nodes have key fields.
  • The effort required to find a particular record
    in a binary search tree depends upon the position
    of the record in the tree. The farther the sought
    record is from the trees root, the greater is
    the effort required to find the record.

32
Direct Searches
  • The search effort to find particular node in a
    binary search tree is measured by the number of
    comparisons made before the search terminates,
    either successfully or unsuccessfully.

(d)
(c)
(b)
(a)
  • For example, to find the record with name DOG in
    the above binary search trees require
  • Tree (a) 1 comparison
  • Tree (b) 4 comparisons
  • Tree (c) 3 comparisons
  • Tree (d) 2 comparisons
  • If it were necessary often to find DOG, then tree
    (a) would probably be considered to be a better
    binary search tree than would tree (b).

33
Direct Searches
  • A binary search tree cannot be evaluated well on
    the sole basis of the search path that it
    provides to just one record. Rather, the search
    paths through the entire tree should enter into
    the evaluation thus the expected (i.e., weighted
    average) length of a search path in the tree is
    useful.
  • If the access probabilities for the keys
    structured in the binary search trees were
  • APE 0.2
  • BEE 0.4
  • COW 0.3
  • DOG 0.1
  • The expected search lengths for binary search
    tree (a), (b), (c), and (d) would be
  • APE BEE COW DOG
  • (a) 0.24 0.43 0.32 0.11 2.7
  • (b) 0.21 0.42 0.33 0.14 2.3
  • (c) 0.22 0.41 0.32 0.13 1.7
  • (d) 0.22 0.41 0.33 0.12 1.9
  • Note that here in order to reduce the expected
    search length, the tree is structured so that the
    most frequently accessed names are placed as
    close as possible to the root.

comparison
34
Balancing Binary Search Trees
  • If the access probabilities were known and no
    insertions were to be made in the tree, the
    binary search tree that yields optimal
    performance could be constructed.
  • However, if the access probabilities are
    unavailable, balancing tree give nearly optimal
    expected search lengths.
  • Inserting ZEBRA would be easy it would be the
    right subtree of GIRAFFE and the tree would still
    be in balance.
  • Instead, however, try to insert APE. The position
    of APE will be as the left subtree of BEE, which
    pulls the tree out of balance. To make tree
    balance, all of the nodes of the original tree
    had to be moved.

35
Height-Balanced (AVL) Trees
  • One type of almost completely balanced trees is
    known as the height-balanced tree, or the AVL
    tree.
  • A tree is height-balanced if the height of the
    left subtree of Ri, and the height of right
    subtree of Ri differ by at most 1.

(c)
(b)
(a)
Height-balanced trees
  • All the above trees are height-balanced. Note
    that although it is height-balanced, tree (c) is
    not completely balanced.

36
Height-Balanced (AVL) Trees
(a)
(b)
(c)
Binary trees that are not height-balanced
  • The height constraint is violated at node APE in
    tree (a), at node COW in tree (b), and at node
    FOX in tree (c).
  • AVL trees provide good direct search performance.
    Although they tend to look somewhat more sparse
    than do completely balanced trees, the search
    they require are only moderately longer.
Write a Comment
User Comments (0)
About PowerShow.com