![Page 1: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/1.jpg)
Ceng-112 Data Structures I 1
Chapter 7
IntroductiontoTrees
![Page 2: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/2.jpg)
Ceng-112 Data Structures I 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 Hopper’s use of them to represent arithmetic
expressions.
• Hopper’s work bears a strong resemblance today’s binary tree
formats.
![Page 3: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/3.jpg)
Ceng-112 Data Structures I 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.
![Page 4: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/4.jpg)
Ceng-112 Data Structures I 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.
![Page 5: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/5.jpg)
Ceng-112 Data Structures I 5
Figure 7-1
• 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.
Basic Concepts
![Page 6: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/6.jpg)
Ceng-112 Data Structures I 6
Basic Concepts
Leaf,Outdegree= 0
Internal Nodes,is not a root or a leaf.
Parent,Outdegree > 0
Child,indegree > 0
Siblings, with the same parent.
• Ancestor, is any node in the path
from the root node.• Descendent is,
all nodes in the path from givennode to the leaf.
![Page 7: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/7.jpg)
Ceng-112 Data Structures I 7
Figure 7-2
Basic Concepts
Height of tree = max. Level of leaf + 1
![Page 8: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/8.jpg)
Ceng-112 Data Structures I 8
Figure 7-3
• A subtree is any connected structure below the root.• A subtree can be divided into subtrees.
Basic Concepts
![Page 9: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/9.jpg)
Ceng-112 Data Structures I 9
algorithm ConvertToParent(val root <node pointer>, ref output <string>)
Convert a general tree to parenthetical notation.Pre root is a pointer to a tree node.Post output contains parenthetical notation.1. Place root in output2. If (root is parent)
1. Place an open parenthesis in the output2. ConvertToParent(root’s first child)3. While (more siblings)
• ConvertToParent(root’s next child)4. Place close parenthesis in the output
3. ReturnEnd ConvertToParent
Root ( B ( C D ) E F ( G H I) )
![Page 10: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/10.jpg)
Ceng-112 Data Structures I 10
Figure 7-5
Binary Trees
A binary tree is a tree in which no node can have more than two subtrees.
![Page 11: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/11.jpg)
Ceng-112 Data Structures I 11
Figure 7-6
Null tree
Symmetry isnot a treerequirement!
Binary Trees
![Page 12: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/12.jpg)
Ceng-112 Data Structures I 12
• 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
Binary Trees
![Page 13: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/13.jpg)
Ceng-112 Data Structures I 13
Figure 7-7
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.
![Page 14: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/14.jpg)
Ceng-112 Data Structures I 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 <pointer to Node>
data <dataType>
rightSubTree <pointer to Node>
End Node
![Page 15: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/15.jpg)
Ceng-112 Data Structures I 15
Figure 7-25
![Page 16: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/16.jpg)
Ceng-112 Data Structures I 16
Figure 7-8
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.
Three different depth-first traversal sequences.
NLR LNR LRN
![Page 17: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/17.jpg)
Ceng-112 Data Structures I 17
Figure 7-9
Binary Tree Traversals
Preorder = ?Inorder = ?Postorder = ?
![Page 18: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/18.jpg)
Ceng-112 Data Structures I 18
Figure 7-10
Binary Tree Traversals - Preorder
![Page 19: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/19.jpg)
Ceng-112 Data Structures I 19
Figure 7-11Recursive algorithmic traversal of binary tree.
Binary Tree Traversals - Preorder
![Page 20: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/20.jpg)
Ceng-112 Data Structures I 20
Figure 7-12
Binary Tree Traversals - Inorder
![Page 21: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/21.jpg)
Ceng-112 Data Structures I 21
Figure 7-13
Binary Tree Traversals - Postorder
![Page 22: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/22.jpg)
Ceng-112 Data Structures I 22
Figure 7-14
Binary Tree – Breadth-First Traversals
![Page 23: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/23.jpg)
Ceng-112 Data Structures I 23
Figure 7-15
Expression Trees
An expression tree is a binary tree with these properties:
1. Each leaf is an operand.
2. The root and internal nodes are operators.
3. Subtrees are subexpressions with the root being an
operator.
![Page 24: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/24.jpg)
Ceng-112 Data Structures I 24
Figure 7-16
Infix Traversal Of An Expression Tree
![Page 25: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/25.jpg)
Ceng-112 Data Structures I 25
Infix Traversal Of An Expression Tree
algorithm infix (val tree <tree pointer>)Print the infix expression for an expression tree.Pre tree is a pointer to an expression treePost the infix expression has been printed1. If (tree not empty)
1. if (tree->token is an operand)1. print (tree->token)
2 else 1. print (open parenthesis)2. infix(tree->left)3. print(tree->token)4. infix(tree->right)5. print(close parenthesis)
2. Returnend infix
![Page 26: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/26.jpg)
Ceng-112 Data Structures I 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:
![Page 27: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/27.jpg)
Ceng-112 Data Structures I 27
Huffman Code
1. Organize character set into a row, ordered by frequency.
2. Find two nodes with smallest combined weight, join them and form a third.
3. Repeat until ALL are combined into a tree..
![Page 28: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/28.jpg)
Ceng-112 Data Structures I 28
Huffman...
![Page 29: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/29.jpg)
Ceng-112 Data Structures I 29
Huffman
![Page 30: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/30.jpg)
Ceng-112 Data Structures I 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.
![Page 31: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/31.jpg)
Ceng-112 Data Structures I 31
Huffman
•
Note that the letters that occur most often are represented with very few bits
![Page 32: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/32.jpg)
Ceng-112 Data Structures I 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.
![Page 33: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/33.jpg)
Ceng-112 Data Structures I 33
Figure 7-17
Converting General Trees To Binary Trees
![Page 34: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/34.jpg)
Ceng-112 Data Structures I 34
Figure 7-18
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).
![Page 35: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/35.jpg)
Ceng-112 Data Structures I 35
Figure 7-19
LIFO insertion; places the new node at the beginning of the sibling list, (like insertion at the front of the stack).
Insertion Into General Trees
![Page 36: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/36.jpg)
Ceng-112 Data Structures I 36
Figure 7-20
Insertion Into General Trees
Key-sequence insertion; places the new node in key sequence amongthe sibling nodes.
![Page 37: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/37.jpg)
Ceng-112 Data Structures I 37
Excercises
• Show the tree representation of the following parenthetical notation:
a ( b c ( e (f g) ) h )
![Page 38: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/38.jpg)
Ceng-112 Data Structures I 38
Figure 7-21
Find:1. Root2. Leaves3. Internal nodes4. Ancestors of H5. Descendents of F6. Indegree of F7. Outdegree of B8. Level of G9. Heigh of node I
Excercises
![Page 39: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/39.jpg)
Ceng-112 Data Structures I 39
Figure 7-22
Excercises
What is the balance factor of the below tree?
![Page 40: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/40.jpg)
Ceng-112 Data Structures I 40
Figure 7-23
Exercises
Find the infix, prefix and postfix expressions of the below tree.
![Page 41: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/41.jpg)
Ceng-112 Data Structures I 41
Exercise (Quiz?!)
Write the binary tree preorder traversal algorithm using a stack instead of recursion.
Algorithm preorderTraverse(ref tree <pointer>, stack <pointer of stack>)
Pre: tree variable has the address of the non-empty binary tree.
stack variable has the address of an empty stack.
Post: Binary tree is printed in preorder sequence.
1. initialize address variable with the pointer value of the binary tree
2. Push (stack, tree)
3. while (stack is not empty)
1.address=Pop(stack)
2.write(address->value)
3.if (address->right != null)
• Push(stack, address->right
4.if (address->left != null)
1. Push(stack, address->left)
4. end
A
B E
C D F G
addressABCDE
Printed: A,B,C,D,E,F,GA
E
B
D
C
G
F
![Page 42: Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees](https://reader033.vdocument.in/reader033/viewer/2022050714/56649eab5503460f94bb02b2/html5/thumbnails/42.jpg)
Ceng-112 Data Structures I 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 09:00 am.