to - PowerPoint PPT Presentation

About This Presentation
Title:

to

Description:

Introduction to Trees Chapter 7 Introduction to Trees In the middle of nineteenth century, Gustav Kirchhoff studied on trees in mathematics. Several years later ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 43
Provided by: ValuedGate2112
Category:

less

Transcript and Presenter's Notes

Title: to


1
Introduction to Trees
Chapter 7
2
Introduction to Trees
  • In the middle of nineteenth century, Gustav
    Kirchhoff studied on trees in mathematics.
  • Several years later, Arthur Cayley used them to
    study the structure of algebraic formulas.
  • In 1951, Grace Hoppers use of them to represent
    arithmetic expressions.
  • Hoppers work bears a strong resemblance todays
    binary tree formats.

3
Introduction to Trees
  • Trees are used in computer science
  • To represent algebraic formulas,
  • As an efficient method for searching large
    dynamic lists,
  • For such diverse applications as artificial
    intelligence systems,
  • And encoding algorithms.

4
Basic Concepts
  • A tree consists of a finite set of elements,
    called nodes.
  • A tree consists of a finite set of directed
    lines, called branches.
  • These braches connect the nodes.
  • The branch is directed towards the node, it is an
    indegree branch.
  • The branch is directed away from the node, it is
    an outdegree branch.

5
Basic Concepts
  • The sum of the indegree and outdegree branches
    equals the degree
  • of the node.
  • If the tree is not empty, then the first node is
    called the root.
  • The indegree of the root is zero.
  • All of the nodes in a tree (exception of root)
    must have an indegree
  • of exactly one.

Figure 7-1
6
Basic Concepts
Parent, Outdegree gt 0
Internal Nodes, is not a root or a leaf.
Child, indegree gt 0
Leaf, Outdegree 0
  • Ancestor,
  • is any node in the path
  • from the root node.
  • Descendent is,
  • all nodes in the path from given
  • node to the leaf.

Siblings, with the same parent.
7
Basic Concepts
Height of tree max. Level of leaf 1
Figure 7-2
8
Basic Concepts
  • A subtree is any connected structure below the
    root.
  • A subtree can be divided into subtrees.

Figure 7-3
9
Root ( B ( C D ) E F ( G H I) )?
  • algorithm ConvertToParent(val root ltnode
    pointergt, ref output ltstringgt)?
  • Convert a general tree to parenthetical notation.
  • Pre root is a pointer to a tree node.
  • Post output contains parenthetical notation.
  • Place root in output
  • If (root is parent)?
  • Place an open parenthesis in the output
  • ConvertToParent(roots first child)?
  • While (more siblings)?
  • ConvertToParent(roots next child)?
  • Place close parenthesis in the output
  • Return
  • End ConvertToParent

10
Binary Trees
A binary tree is a tree in which no node can have
more than two subtrees.
Figure 7-5
11
Binary Trees
Null tree
Symmetry is not a tree requirement!
Figure 7-6
12
Binary Trees
  • Maximum height of tree for N nodes
  • Hmax N
  • The minimum height of the tree
  • Hmin log2N 1
  • If known the height of a tree
  • Nmin H
  • Nmax 2H-1

13
Binary Trees - Balance
  • A complete tree has the maximum number of entries
    for its heigh. Nmax 2H-1
  • The distance of a node from the root determines
    how efficiently it can be located.
  • The balance factor show that the balance of
    the tree.
  • B HL HR
  • If B 0, 1 or -1 the tree is balanced.

Figure 7-7
14
Binary Tree Structure
  • Each node in the structure must contain the data
    to be stored and two pointers, one to the left
    subtree and one to the right subtree.
  • Node
  • leftSubTree ltpointer to Nodegt
  • data ltdataTypegt
  • rightSubTree ltpointer to Nodegt
  • End Node

15
Figure 7-25
16
Binary Tree Traversals
  • A binary tree travelsal requires each node of
    the tree be processed once.
  • In the depth-first traversal, all of the
    descendents of a child are processed before the
  • next child.
  • In the breadth-first traversal, each level is
    completely processed before the next level
  • is started.

LRN
NLR
LNR
Three different depth-first traversal sequences.
Figure 7-8
17
Binary Tree Traversals
Preorder ? Inorder ? Postorder ?
Figure 7-9
18
Binary Tree Traversals - Preorder
Figure 7-10
19
Binary Tree Traversals - Preorder
Figure 7-11
Recursive algorithmic traversal of binary tree.
20
Binary Tree Traversals - Inorder
Figure 7-12
21
Binary Tree Traversals - Postorder
Figure 7-13
22
Binary Tree Breadth-First Traversals
Figure 7-14
23
Expression Trees
  • An expression tree is a binary tree with these
    properties
  • Each leaf is an operand.
  • The root and internal nodes are operators.
  • Subtrees are subexpressions with the root being
    an operator.

Figure 7-15
24
Infix Traversal Of An Expression Tree
Figure 7-16
25
Infix Traversal Of An Expression Tree
  • algorithm infix (val tree lttree pointergt)?
  • Print the infix expression for an expression
    tree.
  • Pre tree is a pointer to an expression tree
  • Post the infix expression has been printed
  • 1. If (tree not empty)?
  • 1. if (tree-gttoken is an operand)?
  • 1. print (tree-gttoken)?
  • 2 else
  • 1. print (open parenthesis)?
  • 2. infix(tree-gtleft)?
  • 3. print(tree-gttoken)?
  • 4. infix(tree-gtright)?
  • 5. print(close parenthesis)?
  • 2. Return
  • end infix

26
Huffman Code
  • ASCII 7 bits each character
  • Some characters occur more often than others,
    like 'E'
  • Every character uses the maximum number of bits
  • Huffman, makes it more efficient
  • Assign shorter codes to ones that occur often
  • Longer codes for the ones that occur less often
  • Typical frequencies

27
Huffman Code
  • Organize character set into a row, ordered by
    frequency.
  • Find two nodes with smallest combined weight,
    join them and form a third.
  • Repeat until ALL are combined into a tree..

28
Huffman...
29
Huffman
30
Huffman...
  • Now we assign a code to each character
  • Assign bit value for each branch
  • 0 left branch,
  • 1 right branch.
  • A character's code is found by starting at root
    and following the branches.

31
Huffman
  • Note that the letters that occur most
    often are represented with very few bits

32
General Trees
  • A general tree is a tree which each node can have
    an unlimited outdegree.
  • Binary trees are presented easier then general
    trees in programs.
  • In general tree, there are two releationships
    that we can use
  • Parent to child and,
  • Sibling to sibling.
  • Using these two relationships, we can represent
    any general tree as a binary tree.

33
Converting General Trees To Binary Trees
Figure 7-17
34
Insertion Into General Trees
FIFO insertion the nodes are inserted at the end
of the sibling list, (like insertion at the rear
of the queue).
Figure 7-18
35
Insertion Into General Trees
LIFO insertion places the new node at the
beginning of the sibling list, (like insertion
at the front of the stack).
Figure 7-19
36
Insertion Into General Trees
Key-sequence insertion places the new node in
key sequence among the sibling nodes.
Figure 7-20
37
Excercises
  • Show the tree representation of the following
    parenthetical notation
  • a ( b c ( e (f g) ) h )?

38
Excercises
  • Find
  • Root
  • Leaves
  • Internal nodes
  • Ancestors of H
  • Descendents of F
  • Indegree of F
  • Outdegree of B
  • Level of G
  • Heigh of node I

Figure 7-21
39
Excercises
What is the balance factor of the below tree?
Figure 7-22
40
Exercises
Find the infix, prefix and postfix expressions of
the below tree.
Figure 7-23
41
(No Transcript)
42
HW-7
  • Write a program to
  • Create the following binary tree and
  • Create a menu to select the printing of infix,
    prefix and postfix expressions of the tree.
  • Print the tree selected expression type.

Load your HW-6 to FTP site until 04 May. 07 at
0900 am.
Write a Comment
User Comments (0)
About PowerShow.com