Title: to
1Introduction to Trees
Chapter 7
2Introduction 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.
3Introduction 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.
4Basic 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.
5Basic 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
6Basic 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.
7Basic Concepts
Height of tree max. Level of leaf 1
Figure 7-2
8Basic Concepts
- A subtree is any connected structure below the
root. - A subtree can be divided into subtrees.
Figure 7-3
9Root ( 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
10Binary Trees
A binary tree is a tree in which no node can have
more than two subtrees.
Figure 7-5
11Binary Trees
Null tree
Symmetry is not a tree requirement!
Figure 7-6
12Binary 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
13Binary 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
14Binary 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
15Figure 7-25
16Binary 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
17Binary Tree Traversals
Preorder ? Inorder ? Postorder ?
Figure 7-9
18Binary Tree Traversals - Preorder
Figure 7-10
19Binary Tree Traversals - Preorder
Figure 7-11
Recursive algorithmic traversal of binary tree.
20Binary Tree Traversals - Inorder
Figure 7-12
21Binary Tree Traversals - Postorder
Figure 7-13
22Binary Tree Breadth-First Traversals
Figure 7-14
23Expression 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
24Infix Traversal Of An Expression Tree
Figure 7-16
25Infix 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
26Huffman 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
27Huffman 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..
28Huffman...
29Huffman
30Huffman...
- 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.
31Huffman
- Note that the letters that occur most
often are represented with very few bits
32General 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.
33Converting General Trees To Binary Trees
Figure 7-17
34Insertion 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
35Insertion 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
36Insertion Into General Trees
Key-sequence insertion places the new node in
key sequence among the sibling nodes.
Figure 7-20
37Excercises
- Show the tree representation of the following
parenthetical notation - a ( b c ( e (f g) ) h )?
38Excercises
- 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
39Excercises
What is the balance factor of the below tree?
Figure 7-22
40Exercises
Find the infix, prefix and postfix expressions of
the below tree.
Figure 7-23
41(No Transcript)
42HW-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.