SlideShare a Scribd company logo
1 of 29
NON LINEAR DATA STRUCTURES
– TREES
B.Vijayalakshmi,AP(SG)/CSE
Ramco Institute of Technology,
Rajapalayam
Nature View of a Tree
branches
leaves
root
Computer Scientist’s View
branches
leaves
root
nodes
TREES
• Trees are one of the important non- Linear data
structure.
• A tree is a Multilevel data structure that represent a
hierarchical relationship between the Set of
individual elements called nodes.
• Each tree structure starts with a node Which is called
the root node of the Tree.
Tree- Definition
• A tree is a collection of nodes. The collection can be
empty; otherwise, a tree consists of a distinguished
node r, called the root, and zero or more non-empty
(sub) trees T1, T2, …, Tk each of whose roots are
connected by a directed edge from r.
• A tree is a collection of N nodes, one of which is the
root and N-1 edges.
Source:Mark Allen Weiss
Tree terminology
• The root of each subtree is said to be a child
of r and r is said to be the parent of each
subtree root.
• Leaves: nodes with no children (also known as
external nodes)
• Internal Nodes: nodes with children
• Siblings: nodes with the same parent
Source:Mark Allen Weiss - edited by Evan Korth
Tree terminology (continued)
• A path from node n1 to nk is defined as a sequence of
nodes n1, n2, …, nk such that ni is the parent of ni+1
for 1<= i <= k.
• The length of this path is the number of edges on
the path namely k-1.
• The length of the path from a node to itself is 0.
• There is exactly one path from the root to each node.
Source:Mark Allen Weiss
Tree terminology (continued)
• Depth of node is the length of the unique
path from the root to a node.
– Root is at depth 0.
• Depth (of tree): The depth of a tree is equal to
the depth of its deepest leaf.
• Height of node is the length of the longest
path from a node to a leaf.
• All leaves have a height of 0
• The height of a tree is equal to the height of
the root.
• The height of the root is equal to the depth of
the tree
Source:Mark Allen Weiss
Tree terminology (continued)
• Level number Every node in the tree is
assigned a level number in such a way that the
root node is at level 0, children of the root
node are at level number 1.
• Forests- A forest is a disjoint union of trees. A
set of disjoint trees (or forests) is obtained by
deleting the root and the edges connecting
the root node to nodes at level 1.
subtree
Tree Terminology
• Root: node without parent (A)
• Siblings: nodes share the same
parent
• Internal node: node with at least
one child (A, B, C, F)
• External node (leaf ): node
without children (E, I, J, K, G, H, D)
• Ancestors of a node: parent,
grandparent, grand-grandparent,
etc.
• Descendant of a node: child,
grandchild, grand-grandchild, etc.
• Degree of a node: The number of
subtrees of a node.
A
B DC
G HE F
I J K
• Degree of a tree: The
maximum degree of a node in
a tree.
• Subtree: Tree consisting of a
node and its descendants
Tree Properties
A
B C
D
G
E F
IH
Property Value
Number of nodes 9
Height 4
Root Node A
Leaves D,H,I,F,C
Interior nodes A,B,E,G
Ancestors of H G,E,B,A
Descendants of B D,E,F,G,H,I
Siblings of E D,F
Right subtree of A C
Degree of this tree 3
Implementation of Trees
• One way to implement a tree would be to have in
each node, besides its data, a pointer to each child of
the node.
• However, since the number of children per node can
vary so greatly and is not known in advance, it might
be infeasible to make the children direct links in the
data structure, because there would be too much
wasted space.
• The solution is simple: Keep the children of each
node in a linked list of tree nodes.
Implementation of Trees
struct TreeNode
{
char Element;
TreeNode* FirstChild;
TreeNode* NextSibling;
}
Implementation of Trees
A
B C GD E F
H I J
K L
A
B C GD E F
H I J
K L
Arrows that point downward are FirstChild pointers.
Arrows that go left to right are NextSibling pointers.
Node E has both a pointer to a sibling (F) and a pointer to a child (I), while some nodes
have neither.
Binary trees
• A binary tree is a tree in which no node can
have more than two children.
• Each node has an element, a reference to a
left child and a reference to a right child.
• Maximum number of nodes on level i is 2𝑖−1
for i>=1
Full Binary Tree
• A binary tree in which every node has either
two or zero number of children is called Full
Binary Tree
Complete Binary Tree
• A binary tree in which all the levels are completely filled
except possibly the last level and the last level has all keys as
left as possible is known as complete binary tree.
• In a Binary tree all internal nodes have 2 children and all
leaves are at same level then it is called perfect binary tree.
• Maximum Number of nodes = 2d+1 – 1
• Number of leaf nodes = 2d , Where, d – Depth of the tree
Left Skewed and Right Skewed Trees
• Binary tree has only left sub trees - Left
Skewed Trees
• Binary tree has only right sub trees - Right
Skewed Trees
Binary Tree Representation
1. Sequential representation using arrays
2. List representation using Linked list
Sequential representation
• To represent a binary tree of depth ‘d' using array
representation, we need one dimensional array with a
maximum size of 2d+1 - 1.
Sequential Representation
For any element in array position i, its left and
right child are in the position given by
• Left child= 2*i
• Right child= 2*i+1
• Parent= i/2
Sequential representation
• Advantages:
– Direct access to all nodes (Random access)
• Disadvantages:
– Height of tree should be known
– Memory may be wasted
– Insertion and deletion of a node is difficult
List representation
struct node
{
int data;
struct node *left;
struct node *right;
};
List Representation
• Each node is a structure consisting of the
element and pointer to the left and right child
of a node
• For every binary tree with N nodes would
require N+1 NULL pointers.
List representation
• Advantages:
– Height of tree need not be known.
– No memory wastage.
– Insertion and deletion of a node is done without
affecting other nodes.
• Disadvantages:
– Direct access to node is difficult.
– Additional memory required for storing address of
left and right node.
Applications of trees
• Trees are used to store simple as well as
complex data.
• Trees are often used for implementing other
types of data structures like hash tables, sets
and maps.
• Trees are used in database design, file system
directories.
• Trees are also widely used for information
storage and retrieval in symbol tables.
Class Poll
1. Node with out degree zero is called
A. Root
B. Sub tree
C. Leaf
D.Internal Nodes
Class Poll
2. In tree Construction which is suitable and
efficient data structure?
A.Array
B. Linked List
C. Stack
D.Queue
Class Poll
3. Using the array implementation of tree, the
right child of the key value in index i is found at
what index?
a) i + 2
b) 2i
c) 2i + 1
d) 2i + 2

More Related Content

What's hot

What's hot (20)

Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Binary tree
Binary treeBinary tree
Binary tree
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
2 3 tree
2 3 tree2 3 tree
2 3 tree
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Red black trees
Red black treesRed black trees
Red black trees
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 

Similar to Non-Linear Data Structures: Trees

Similar to Non-Linear Data Structures: Trees (20)

Data structures 3
Data structures 3Data structures 3
Data structures 3
 
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptxTERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Tree Basic concepts of Tree in Data Structure
Tree Basic concepts of Tree in Data StructureTree Basic concepts of Tree in Data Structure
Tree Basic concepts of Tree in Data Structure
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
Lecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptxLecture 21_Trees - I.pptx
Lecture 21_Trees - I.pptx
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree
TreeTree
Tree
 
tree in Data Structures
tree in Data Structurestree in Data Structures
tree in Data Structures
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
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
 
Trees krw
Trees krwTrees krw
Trees krw
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
 
Tree
TreeTree
Tree
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 

More from Viji B

Heap Memory Management.pptx
Heap Memory Management.pptxHeap Memory Management.pptx
Heap Memory Management.pptxViji B
 
ML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptxML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptxViji B
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationViji B
 
Innovative pratice crossword
Innovative pratice crosswordInnovative pratice crossword
Innovative pratice crosswordViji B
 
Innovative practice role play sorting
Innovative practice role play sortingInnovative practice role play sorting
Innovative practice role play sortingViji B
 
Innovative Practice- Learning by doing- Complete the code and trace the exec...
Innovative  Practice- Learning by doing- Complete the code and trace the exec...Innovative  Practice- Learning by doing- Complete the code and trace the exec...
Innovative Practice- Learning by doing- Complete the code and trace the exec...Viji B
 

More from Viji B (6)

Heap Memory Management.pptx
Heap Memory Management.pptxHeap Memory Management.pptx
Heap Memory Management.pptx
 
ML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptxML & AI based Cloud Compiler.pptx
ML & AI based Cloud Compiler.pptx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Innovative pratice crossword
Innovative pratice crosswordInnovative pratice crossword
Innovative pratice crossword
 
Innovative practice role play sorting
Innovative practice role play sortingInnovative practice role play sorting
Innovative practice role play sorting
 
Innovative Practice- Learning by doing- Complete the code and trace the exec...
Innovative  Practice- Learning by doing- Complete the code and trace the exec...Innovative  Practice- Learning by doing- Complete the code and trace the exec...
Innovative Practice- Learning by doing- Complete the code and trace the exec...
 

Recently uploaded

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 

Recently uploaded (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 

Non-Linear Data Structures: Trees

  • 1. NON LINEAR DATA STRUCTURES – TREES B.Vijayalakshmi,AP(SG)/CSE Ramco Institute of Technology, Rajapalayam
  • 2. Nature View of a Tree branches leaves root
  • 4. TREES • Trees are one of the important non- Linear data structure. • A tree is a Multilevel data structure that represent a hierarchical relationship between the Set of individual elements called nodes. • Each tree structure starts with a node Which is called the root node of the Tree.
  • 5. Tree- Definition • A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more non-empty (sub) trees T1, T2, …, Tk each of whose roots are connected by a directed edge from r. • A tree is a collection of N nodes, one of which is the root and N-1 edges. Source:Mark Allen Weiss
  • 6. Tree terminology • The root of each subtree is said to be a child of r and r is said to be the parent of each subtree root. • Leaves: nodes with no children (also known as external nodes) • Internal Nodes: nodes with children • Siblings: nodes with the same parent Source:Mark Allen Weiss - edited by Evan Korth
  • 7. Tree terminology (continued) • A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1<= i <= k. • The length of this path is the number of edges on the path namely k-1. • The length of the path from a node to itself is 0. • There is exactly one path from the root to each node. Source:Mark Allen Weiss
  • 8. Tree terminology (continued) • Depth of node is the length of the unique path from the root to a node. – Root is at depth 0. • Depth (of tree): The depth of a tree is equal to the depth of its deepest leaf. • Height of node is the length of the longest path from a node to a leaf. • All leaves have a height of 0 • The height of a tree is equal to the height of the root. • The height of the root is equal to the depth of the tree Source:Mark Allen Weiss
  • 9. Tree terminology (continued) • Level number Every node in the tree is assigned a level number in such a way that the root node is at level 0, children of the root node are at level number 1. • Forests- A forest is a disjoint union of trees. A set of disjoint trees (or forests) is obtained by deleting the root and the edges connecting the root node to nodes at level 1.
  • 10. subtree Tree Terminology • Root: node without parent (A) • Siblings: nodes share the same parent • Internal node: node with at least one child (A, B, C, F) • External node (leaf ): node without children (E, I, J, K, G, H, D) • Ancestors of a node: parent, grandparent, grand-grandparent, etc. • Descendant of a node: child, grandchild, grand-grandchild, etc. • Degree of a node: The number of subtrees of a node. A B DC G HE F I J K • Degree of a tree: The maximum degree of a node in a tree. • Subtree: Tree consisting of a node and its descendants
  • 11. Tree Properties A B C D G E F IH Property Value Number of nodes 9 Height 4 Root Node A Leaves D,H,I,F,C Interior nodes A,B,E,G Ancestors of H G,E,B,A Descendants of B D,E,F,G,H,I Siblings of E D,F Right subtree of A C Degree of this tree 3
  • 12. Implementation of Trees • One way to implement a tree would be to have in each node, besides its data, a pointer to each child of the node. • However, since the number of children per node can vary so greatly and is not known in advance, it might be infeasible to make the children direct links in the data structure, because there would be too much wasted space. • The solution is simple: Keep the children of each node in a linked list of tree nodes.
  • 13. Implementation of Trees struct TreeNode { char Element; TreeNode* FirstChild; TreeNode* NextSibling; }
  • 14. Implementation of Trees A B C GD E F H I J K L A B C GD E F H I J K L Arrows that point downward are FirstChild pointers. Arrows that go left to right are NextSibling pointers. Node E has both a pointer to a sibling (F) and a pointer to a child (I), while some nodes have neither.
  • 15. Binary trees • A binary tree is a tree in which no node can have more than two children. • Each node has an element, a reference to a left child and a reference to a right child. • Maximum number of nodes on level i is 2𝑖−1 for i>=1
  • 16. Full Binary Tree • A binary tree in which every node has either two or zero number of children is called Full Binary Tree
  • 17. Complete Binary Tree • A binary tree in which all the levels are completely filled except possibly the last level and the last level has all keys as left as possible is known as complete binary tree. • In a Binary tree all internal nodes have 2 children and all leaves are at same level then it is called perfect binary tree. • Maximum Number of nodes = 2d+1 – 1 • Number of leaf nodes = 2d , Where, d – Depth of the tree
  • 18. Left Skewed and Right Skewed Trees • Binary tree has only left sub trees - Left Skewed Trees • Binary tree has only right sub trees - Right Skewed Trees
  • 19. Binary Tree Representation 1. Sequential representation using arrays 2. List representation using Linked list
  • 20. Sequential representation • To represent a binary tree of depth ‘d' using array representation, we need one dimensional array with a maximum size of 2d+1 - 1.
  • 21. Sequential Representation For any element in array position i, its left and right child are in the position given by • Left child= 2*i • Right child= 2*i+1 • Parent= i/2
  • 22. Sequential representation • Advantages: – Direct access to all nodes (Random access) • Disadvantages: – Height of tree should be known – Memory may be wasted – Insertion and deletion of a node is difficult
  • 23. List representation struct node { int data; struct node *left; struct node *right; };
  • 24. List Representation • Each node is a structure consisting of the element and pointer to the left and right child of a node • For every binary tree with N nodes would require N+1 NULL pointers.
  • 25. List representation • Advantages: – Height of tree need not be known. – No memory wastage. – Insertion and deletion of a node is done without affecting other nodes. • Disadvantages: – Direct access to node is difficult. – Additional memory required for storing address of left and right node.
  • 26. Applications of trees • Trees are used to store simple as well as complex data. • Trees are often used for implementing other types of data structures like hash tables, sets and maps. • Trees are used in database design, file system directories. • Trees are also widely used for information storage and retrieval in symbol tables.
  • 27. Class Poll 1. Node with out degree zero is called A. Root B. Sub tree C. Leaf D.Internal Nodes
  • 28. Class Poll 2. In tree Construction which is suitable and efficient data structure? A.Array B. Linked List C. Stack D.Queue
  • 29. Class Poll 3. Using the array implementation of tree, the right child of the key value in index i is found at what index? a) i + 2 b) 2i c) 2i + 1 d) 2i + 2