SlideShare a Scribd company logo
1 of 42
Ceng-112 Data Structures I 1
Chapter 7
Introduction
to
Trees
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.
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.
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.
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
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 given
node to the leaf.
Ceng-112 Data Structures I 7
Figure 7-2
Basic Concepts
Height of tree = max. Level of leaf + 1
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
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 output
2. If (root is parent)
1. Place an open parenthesis in the output
2. ConvertToParent(root’s first child)
3. While (more siblings)
• ConvertToParent(root’s next child)
1. Place close parenthesis in the output
3. Return
End ConvertToParent
Root ( B ( C D ) E F ( G H I) )
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.
Ceng-112 Data Structures I 11
Figure 7-6
Null tree
Symmetry is
not a tree
requirement!
Binary Trees
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
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.
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
Ceng-112 Data Structures I 15
Figure 7-25
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
Ceng-112 Data Structures I 17
Figure 7-9
Binary Tree Traversals
Preorder = ?
Inorder = ?
Postorder = ?
Ceng-112 Data Structures I 18
Figure 7-10
Binary Tree Traversals - Preorder
Ceng-112 Data Structures I 19
Figure 7-11
Recursive algorithmic traversal of binary tree.
Binary Tree Traversals - Preorder
Ceng-112 Data Structures I 20
Figure 7-12
Binary Tree Traversals - Inorder
Ceng-112 Data Structures I 21
Figure 7-13
Binary Tree Traversals - Postorder
Ceng-112 Data Structures I 22
Figure 7-14
Binary Tree – Breadth-First Traversals
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.
Ceng-112 Data Structures I 24
Figure 7-16
Infix Traversal Of An Expression Tree
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 tree
Post the infix expression has been printed
1. 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. Return
end infix
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:
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..
Ceng-112 Data Structures I 28
Huffman...
Ceng-112 Data Structures I 29
Huffman
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.
Ceng-112 Data Structures I 31
Huffman
•
Note that the letters that occur most often are represented
with very few bits
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.
Ceng-112 Data Structures I 33
Figure 7-17
Converting
General Trees To Binary Trees
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).
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
Ceng-112 Data Structures I 36
Figure 7-20
Insertion Into General Trees
Key-sequence insertion; places the new node in key sequence among
the sibling nodes.
Ceng-112 Data Structures I 37
Excercises
• Show the tree representation of the following parenthetical
notation:
a ( b c ( e (f g) ) h )
Ceng-112 Data Structures I 38
Figure 7-21
Find:
1. Root
2. Leaves
3. Internal nodes
4. Ancestors of H
5. Descendents of F
6. Indegree of F
7. Outdegree of B
8. Level of G
9. Heigh of node I
Excercises
Ceng-112 Data Structures I 39
Figure 7-22
Excercises
What is the balance factor of the below tree?
Ceng-112 Data Structures I 40
Figure 7-23
Exercises
Find the infix, prefix and postfix expressions of the below tree.
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
1.if (address->left != null)
1. Push(stack, address->left)
4. end
A
B E
C D F G
address
A
B
C
D
E
Printed: A,B,C,D,E,F,G
A
E
B
D
C
G
F
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.

More Related Content

What's hot

What's hot (20)

Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Trees
TreesTrees
Trees
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Heaps
HeapsHeaps
Heaps
 
Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Heaps
HeapsHeaps
Heaps
 
2D Array
2D Array 2D Array
2D Array
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Linked list
Linked list Linked list
Linked list
 
Expression trees
Expression treesExpression trees
Expression trees
 

Viewers also liked

Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsAakash deep Singhal
 
Commas notes2 powerpoint
Commas notes2 powerpointCommas notes2 powerpoint
Commas notes2 powerpointktyndall
 
Understanding and Correcting Common Writing Errors Pt. I Grammar and Punctuation
Understanding and Correcting Common Writing Errors Pt. I Grammar and PunctuationUnderstanding and Correcting Common Writing Errors Pt. I Grammar and Punctuation
Understanding and Correcting Common Writing Errors Pt. I Grammar and Punctuationtvandamericanculture
 
Introductory Material Keynote
Introductory Material KeynoteIntroductory Material Keynote
Introductory Material Keynoteloren digges
 
Mla documentation
Mla documentationMla documentation
Mla documentationjulieee64
 
Creating a Works Cited Page and Parenthetical Citations - MLA 7
Creating a Works Cited Page and Parenthetical Citations - MLA 7Creating a Works Cited Page and Parenthetical Citations - MLA 7
Creating a Works Cited Page and Parenthetical Citations - MLA 7sraslim
 
Mla Citations for ENG 101
Mla Citations for ENG 101Mla Citations for ENG 101
Mla Citations for ENG 101Laura Cline
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
How to Write an Effective Conclusion and Parenthetical Citations
How to Write an Effective Conclusion and Parenthetical CitationsHow to Write an Effective Conclusion and Parenthetical Citations
How to Write an Effective Conclusion and Parenthetical Citationsmsalagar
 
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
PARENTHETICAL EXPRESSION@ English 10
PARENTHETICAL EXPRESSION@ English 10PARENTHETICAL EXPRESSION@ English 10
PARENTHETICAL EXPRESSION@ English 10Lea Colopano Minay
 
Relationship of Ideas in an Argument: Parallelism comparison causality
Relationship of Ideas in an Argument: Parallelism comparison causalityRelationship of Ideas in an Argument: Parallelism comparison causality
Relationship of Ideas in an Argument: Parallelism comparison causalitysumlendia
 
Time expresions
Time expresionsTime expresions
Time expresionsVitoAina
 
Basics of APA Style
Basics of APA Style Basics of APA Style
Basics of APA Style dkathryn
 
Parenthetical Citations
Parenthetical CitationsParenthetical Citations
Parenthetical Citationsleshb
 
English Grade 11 (expression love and sadness)
English Grade 11 (expression love and sadness)English Grade 11 (expression love and sadness)
English Grade 11 (expression love and sadness)Dzakira Iskandar
 

Viewers also liked (20)

Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithms
 
Commas notes2 powerpoint
Commas notes2 powerpointCommas notes2 powerpoint
Commas notes2 powerpoint
 
Understanding and Correcting Common Writing Errors Pt. I Grammar and Punctuation
Understanding and Correcting Common Writing Errors Pt. I Grammar and PunctuationUnderstanding and Correcting Common Writing Errors Pt. I Grammar and Punctuation
Understanding and Correcting Common Writing Errors Pt. I Grammar and Punctuation
 
Introductory Material Keynote
Introductory Material KeynoteIntroductory Material Keynote
Introductory Material Keynote
 
Mla documentation
Mla documentationMla documentation
Mla documentation
 
Creating a Works Cited Page and Parenthetical Citations - MLA 7
Creating a Works Cited Page and Parenthetical Citations - MLA 7Creating a Works Cited Page and Parenthetical Citations - MLA 7
Creating a Works Cited Page and Parenthetical Citations - MLA 7
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Mla Citations for ENG 101
Mla Citations for ENG 101Mla Citations for ENG 101
Mla Citations for ENG 101
 
Parallelism
ParallelismParallelism
Parallelism
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
How to Write an Effective Conclusion and Parenthetical Citations
How to Write an Effective Conclusion and Parenthetical CitationsHow to Write an Effective Conclusion and Parenthetical Citations
How to Write an Effective Conclusion and Parenthetical Citations
 
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
 
PARENTHETICAL EXPRESSION@ English 10
PARENTHETICAL EXPRESSION@ English 10PARENTHETICAL EXPRESSION@ English 10
PARENTHETICAL EXPRESSION@ English 10
 
Relationship of Ideas in an Argument: Parallelism comparison causality
Relationship of Ideas in an Argument: Parallelism comparison causalityRelationship of Ideas in an Argument: Parallelism comparison causality
Relationship of Ideas in an Argument: Parallelism comparison causality
 
Time expresions
Time expresionsTime expresions
Time expresions
 
Basics of APA Style
Basics of APA Style Basics of APA Style
Basics of APA Style
 
Parenthetical Citations
Parenthetical CitationsParenthetical Citations
Parenthetical Citations
 
Comma sense
Comma senseComma sense
Comma sense
 
English Grade 11 (expression love and sadness)
English Grade 11 (expression love and sadness)English Grade 11 (expression love and sadness)
English Grade 11 (expression love and sadness)
 
Embedded quotes
Embedded quotesEmbedded quotes
Embedded quotes
 

Similar to introduction to_trees

Similar to introduction to_trees (20)

Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Review session2
Review session2Review session2
Review session2
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
[Queue , linked list , tree]
[Queue , linked list , tree][Queue , linked list , tree]
[Queue , linked list , tree]
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Binary trees
Binary treesBinary trees
Binary trees
 
Data Structures
Data StructuresData Structures
Data Structures
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.ppt
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
7.tree
7.tree7.tree
7.tree
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 

Recently uploaded

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 

Recently uploaded (20)

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 

introduction to_trees

  • 1. Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees
  • 2. 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.
  • 3. 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.
  • 4. 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.
  • 5. 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
  • 6. 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 given node to the leaf.
  • 7. Ceng-112 Data Structures I 7 Figure 7-2 Basic Concepts Height of tree = max. Level of leaf + 1
  • 8. 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
  • 9. 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 output 2. If (root is parent) 1. Place an open parenthesis in the output 2. ConvertToParent(root’s first child) 3. While (more siblings) • ConvertToParent(root’s next child) 1. Place close parenthesis in the output 3. Return End ConvertToParent Root ( B ( C D ) E F ( G H I) )
  • 10. 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.
  • 11. Ceng-112 Data Structures I 11 Figure 7-6 Null tree Symmetry is not a tree requirement! Binary Trees
  • 12. 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
  • 13. 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.
  • 14. 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
  • 15. Ceng-112 Data Structures I 15 Figure 7-25
  • 16. 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
  • 17. Ceng-112 Data Structures I 17 Figure 7-9 Binary Tree Traversals Preorder = ? Inorder = ? Postorder = ?
  • 18. Ceng-112 Data Structures I 18 Figure 7-10 Binary Tree Traversals - Preorder
  • 19. Ceng-112 Data Structures I 19 Figure 7-11 Recursive algorithmic traversal of binary tree. Binary Tree Traversals - Preorder
  • 20. Ceng-112 Data Structures I 20 Figure 7-12 Binary Tree Traversals - Inorder
  • 21. Ceng-112 Data Structures I 21 Figure 7-13 Binary Tree Traversals - Postorder
  • 22. Ceng-112 Data Structures I 22 Figure 7-14 Binary Tree – Breadth-First Traversals
  • 23. 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.
  • 24. Ceng-112 Data Structures I 24 Figure 7-16 Infix Traversal Of An Expression Tree
  • 25. 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 tree Post the infix expression has been printed 1. 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. Return end infix
  • 26. 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:
  • 27. 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..
  • 28. Ceng-112 Data Structures I 28 Huffman...
  • 29. Ceng-112 Data Structures I 29 Huffman
  • 30. 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.
  • 31. Ceng-112 Data Structures I 31 Huffman • Note that the letters that occur most often are represented with very few bits
  • 32. 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.
  • 33. Ceng-112 Data Structures I 33 Figure 7-17 Converting General Trees To Binary Trees
  • 34. 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).
  • 35. 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
  • 36. Ceng-112 Data Structures I 36 Figure 7-20 Insertion Into General Trees Key-sequence insertion; places the new node in key sequence among the sibling nodes.
  • 37. Ceng-112 Data Structures I 37 Excercises • Show the tree representation of the following parenthetical notation: a ( b c ( e (f g) ) h )
  • 38. Ceng-112 Data Structures I 38 Figure 7-21 Find: 1. Root 2. Leaves 3. Internal nodes 4. Ancestors of H 5. Descendents of F 6. Indegree of F 7. Outdegree of B 8. Level of G 9. Heigh of node I Excercises
  • 39. Ceng-112 Data Structures I 39 Figure 7-22 Excercises What is the balance factor of the below tree?
  • 40. Ceng-112 Data Structures I 40 Figure 7-23 Exercises Find the infix, prefix and postfix expressions of the below tree.
  • 41. 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 1.if (address->left != null) 1. Push(stack, address->left) 4. end A B E C D F G address A B C D E Printed: A,B,C,D,E,F,G A E B D C G F
  • 42. 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.