SlideShare a Scribd company logo
Trees: basic definitions and terminology
Contrary to arrays, stacks, queues and sequences all of which are one-
dimensional data structures, trees are two-dimensional data structures with
hierarchical relationship between data items.
Definition 1 A tree is a non-empty collection of vertices (nodes) and edges
that satisfy certain requirements.
Definition 2 A path in a tree is a list of distinct vertices in which successive
vertices are connected by edges in the tree.
One node in the tree is designated as the root. Each tree has exactly one
path between the root and each of the other nodes. If there is more than one
path between the root and some node, or no path at all, we have a graph.
Definition 3 A set of trees is called a forest.
Definition 4 (Recursive definition) A tree is either a single node or a root
node connected to a forest.
Example of a tree:
root
siblings
subtree
internal nodes
external nodes, or leaves
More definitions
Definition 5 An ordered tree is a tree in which the order of children is
specified.
Definition 6 A level (depth) of a node in the number of nodes on the path
from that node to the root.
Definition 7 The height (maximum distance) of a tree is the maximum
level among all of the nodes in the tree.
Definition 8 The path length of a tree is the sum of the levels of all the
nodes in the tree.
Definition 9 A tree where each node has a specific number of children
appearing in a specific order is call a multiway tree. The simplest type of
a multiway tree is the binary tree. Each node in a binary tree has exactly
two children one of which is designated as a left child, and the other is
designated as a right child.
Definition 10 (Recursive definition) A binary tree is either an external node,
or an internal node and two binary trees.
Example of a binary tree
root
left child right child
one or both children
might be external nodes
special external nodes with
no name and no data associated
with them
More binary trees examples
1. Binary tree for representing arithmetic expressions. The underlying hierarchical
relationship is that of an arithmetic operator and its two operands.
Arithmetic expression in an infix form: (A - B) + C * (E / F)
+
- *
A B C /
E F
Note that a post-order traversal of this tree (i.e. visiting the left subtree first, right
subtree next, and finally the root) returns the postfix form of the arithmetic
expression, while the pre-order traversal (root is visited first, then the left subtree,
then the right subtree) returns the prefix form of the arithmetic expression.
2. Binary tree with a heap property. The underlying hierarchical relationship
suggests that the datum in each node is greater than or equal to the data in
its left and right subtrees.
87
84 63
68 79 12
32 67 6 10
8 9
3. Binary tree with an ordering property. The underlying hierarchical
relationship suggests that the datum in each node is greater than the data in
its left subtree, and less than or equal to the data in its right subtrees.
87
84 103
68 86 90 109
32 74 88 97
70 80
4. Decision trees. The underlying hierarchical relationship depends on the nature of
the domain represented by the binary tree. For example, consider a domain that
consists of the following statements (from J.Ignizio “Intro to ES”):
 If the plane’s engine is propeller, then the plane is C130.
 If the plane’s engine is jet and the wing position is low, then the plane is B747.
 If the plane’s engine is jet and the wing position is high and no bulges are seen, then
the plane is C5A
 If the plane’s engine is jet and the wing position is high and bulges are aft of wing, then
plane is C141 .
The following decision tree can be generated from these rules:
Engine type
Jet Propeller
Wing Position C130
Low High
B747 Bulges
None Aft Wing
C5A C141
Properties of binary trees
1. The number of external nodes is 1 more than the number of internal nodes.
It is easy to see this if we start removing external nodes with their internal
parent, one pair at a time (assume that a method removeAboveExternal(n)
does this). At the end of this process, only the root with its two external
children will remain.
2. The number of external nodes is at least h + 1, where h is the height of the
tree, and at most 2
h
. The later holds for a full binary tree, which is a tree
where internal nodes completely fill every level.
3. The number of internal nodes is at least h and at most 2h
- 1.
4. The total number of nodes in a binary tree is at least 2*h + 1 and at most
2
h+1
- 1.
5. The height, h, of a binary tree with n nodes is at least log n+1 and at most
n.
6. A binary tree with n nodes has exactly n - 1 edges.
Full binary trees and complete binary trees
Here is an example of a full binary tree:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
A complete binary tree is a full binary tree where the internal nodes on the
bottom level all appear to the left of the external nodes on that level. Here is
an example of a complete binary tree:
1
2 3
4 5 6
Properties of binary trees (cont.)
The following property holds for a complete binary tree.
Let i be a number assigned to a node in a complete binary tree. Then:
1. If i = 1, then this node is the root of the tree. If i > 1, then the parent of this
node is assigned the number (i / 2).
2. If 2*i > n, then the corresponding node has no left child. Otherwise, the left
child of that node is assigned the number 2*i.
3. If 2*i + 1 > n, then the corresponding node has no right child. Otherwise, the
right child of that node is assigned the number 2*i + 1.
This property suggests a trivial array-based representation of a complete binary
tree, where i is the index of the node in the array. We will see that a slight
modification in this representation allows us to represent any binary tree in a
linear fashion.
Linear (or sequence-based) representation of a
binary tree
Linear representation of a binary tree utilizes one-dimensional array of size
2
h+1
- 1. Consider the following tree:
+ level 0 (d = 0)
- * level 1 (d = 1)
A B C / level 2 (d = 2)
E F level 3 (d = 3)
To represent this tree, we need an array of size 23+1
- 1 = 15
The tree is represented as follows:
1. The root is stored in BinaryTree[1].
2. For node BinaryTree[n], the left child is stored in BinaryTree[2*n], and the
right child is stored in BinaryTree[2*n+1]
i: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
BinaryTree[i]: + - * A B C / E F
Linear representation of a binary tree (cont.)
Advantages of linear representation:
1. Simplicity.
2. Given the location of the child (say, k), the location of the parent is easy to
determine (k / 2).
Disadvantages of linear representation:
1. Additions and deletions of nodes are inefficient, because of the data
movements in the array.
2. Space is wasted if the binary tree is not complete. That is, the linear
representation is useful if the number of missing nodes is small.
Note that linear representation of a binary tree can be implemented by means
of a linked list instead of an array. For example, we can use the Positional
Sequence ADT to implement a binary tree in a linear fashion. This way the
above mentioned disadvantages of the linear representation will be resolved.
Linked representation of a binary tree
Linked representation uses explicit links to connect the nodes. Example:
1
2 5
3 4 6 7
8 9
Nodes in this tree can be viewed as positions in a sequence (numbered 1
through 9).
+
BA
- *
C /
E F
Traversals of a binary tree
Preorder traversal
void preOrder (BTNode localRoot) {
if (localRoot != null) {
localRoot.displayBTNode();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild); } }
Example Consider a tree with an ordering property, where nodes are inserted
in the following order b i n a r y t r e e, i.e.
b
a i
e n
e r
y
t
r
The preorder traversal is: b a i e e n r y t r
Traversals of a binary tree (cont.)
Post-order traversal
void postOrder (BTNode localRoot) {
if (localRoot != null) {
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayBTNode(); } }
The nodes in the example tree are traversed in post-order as follows:
a e e r t y r n i b
In-order traversal
void inOrder (BTNode localRoot) {
if (localRoot != null) {
inOrder(localRoot.leftChild);
localRoot.displayBTNode();
inOrder(localRoot.rightChild); } }
The nodes in the example tree are traversed in in-order as follows:
a b e e i n r r t y
A Binary Search Tree is a binary
tree with the following properties:
 All items in the left subtree are less than
the root.
 All items in the right subtree are greater or
equal to the root.
 Each subtree is itself a binary search tree.
Basic Property
 In a binary search tree,
 the left subtree contains key values less
than the root
 the right subtree contains key values
greater than or equal to the root.
7-1 Basic Concepts
Binary search trees provide an excellent structure for searching a
list and at the same time for inserting and deleting data into the
list.
Insertion in binary search tree
Insert 9 in the the following tree:
3
2 15
1 11
7 13
Step 1: search for 9 3
2 15
1 11
search stops here 7 13
Step 2: insert 9 at the point where the search terminates unsuccessfully
3
2 15
1 11
7 13 That is, new nodes are always inserted at the leaf level.
9
Deletion in binary search tree
Consider the tree:
3 7
2 15 Deleting 3 2 15
1 11 1 11
7 13 13
The following cases of deletions are possible:
1. Delete a note with no children, for example 1. This only requires the appropriate link in
the parent node to be made null.
2. Delete a node which has only one child, for example 15. In this case, we must set the
corresponding child link of the parent’s parent to point to the only child of the node being
deleted.
3. Delete a node with two children, for example 3. The delete method is based on the
following consideration: in-order traversal of the resulting tree (after delete operation)
must yield an ordered list. To ensure this, the following steps are carried out:
Step 1: Replace 3 with the node with the next largest datum, i.e. 7.
Step 2: Make the left link of 11 point to the right child of 7 (which is null here).
Step 3: Copy the links from the node containing 3 to the node containing 7, and make
the parent node of 3 point to 7.
Trees in Data Structure

More Related Content

What's hot

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
ManishPrajapati78
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
Ashish Arun
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
United International University
 
Trees
TreesTrees
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
DrkhanchanaR
 
Red black tree
Red black treeRed black tree
Red black tree
Rajendran
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
 

What's hot (20)

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
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
Quick sort
Quick sortQuick sort
Quick sort
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
single linked list
single linked listsingle linked list
single linked list
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Trees
TreesTrees
Trees
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
Red black tree
Red black treeRed black tree
Red black tree
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 

Similar to Trees in Data Structure

Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
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 treeSiddhi Viradiya
 
Data Structures
Data StructuresData Structures
Data Structures
Nitesh Bichwani
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
shashankbhadouria4
 
Review session2
Review session2Review session2
Review session2
NEEDY12345
 
7.tree
7.tree7.tree
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
praveena p
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
Edhole.com
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
worldchannel
 
Trees in data structrures
Trees in data structruresTrees in data structrures
Trees in data structrures
Gaurav Sharma
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
skilljiolms
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
Prof Ansari
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 

Similar to Trees in Data Structure (20)

Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
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
 
Data Structures
Data StructuresData Structures
Data Structures
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Review session2
Review session2Review session2
Review session2
 
7.tree
7.tree7.tree
7.tree
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Trees in data structrures
Trees in data structruresTrees in data structrures
Trees in data structrures
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 

Recently uploaded

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 

Recently uploaded (20)

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 

Trees in Data Structure

  • 1.
  • 2. Trees: basic definitions and terminology Contrary to arrays, stacks, queues and sequences all of which are one- dimensional data structures, trees are two-dimensional data structures with hierarchical relationship between data items. Definition 1 A tree is a non-empty collection of vertices (nodes) and edges that satisfy certain requirements. Definition 2 A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree. One node in the tree is designated as the root. Each tree has exactly one path between the root and each of the other nodes. If there is more than one path between the root and some node, or no path at all, we have a graph. Definition 3 A set of trees is called a forest. Definition 4 (Recursive definition) A tree is either a single node or a root node connected to a forest.
  • 3. Example of a tree: root siblings subtree internal nodes external nodes, or leaves
  • 4. More definitions Definition 5 An ordered tree is a tree in which the order of children is specified. Definition 6 A level (depth) of a node in the number of nodes on the path from that node to the root. Definition 7 The height (maximum distance) of a tree is the maximum level among all of the nodes in the tree. Definition 8 The path length of a tree is the sum of the levels of all the nodes in the tree. Definition 9 A tree where each node has a specific number of children appearing in a specific order is call a multiway tree. The simplest type of a multiway tree is the binary tree. Each node in a binary tree has exactly two children one of which is designated as a left child, and the other is designated as a right child. Definition 10 (Recursive definition) A binary tree is either an external node, or an internal node and two binary trees.
  • 5. Example of a binary tree root left child right child one or both children might be external nodes special external nodes with no name and no data associated with them
  • 6. More binary trees examples 1. Binary tree for representing arithmetic expressions. The underlying hierarchical relationship is that of an arithmetic operator and its two operands. Arithmetic expression in an infix form: (A - B) + C * (E / F) + - * A B C / E F Note that a post-order traversal of this tree (i.e. visiting the left subtree first, right subtree next, and finally the root) returns the postfix form of the arithmetic expression, while the pre-order traversal (root is visited first, then the left subtree, then the right subtree) returns the prefix form of the arithmetic expression.
  • 7. 2. Binary tree with a heap property. The underlying hierarchical relationship suggests that the datum in each node is greater than or equal to the data in its left and right subtrees. 87 84 63 68 79 12 32 67 6 10 8 9
  • 8. 3. Binary tree with an ordering property. The underlying hierarchical relationship suggests that the datum in each node is greater than the data in its left subtree, and less than or equal to the data in its right subtrees. 87 84 103 68 86 90 109 32 74 88 97 70 80
  • 9. 4. Decision trees. The underlying hierarchical relationship depends on the nature of the domain represented by the binary tree. For example, consider a domain that consists of the following statements (from J.Ignizio “Intro to ES”):  If the plane’s engine is propeller, then the plane is C130.  If the plane’s engine is jet and the wing position is low, then the plane is B747.  If the plane’s engine is jet and the wing position is high and no bulges are seen, then the plane is C5A  If the plane’s engine is jet and the wing position is high and bulges are aft of wing, then plane is C141 . The following decision tree can be generated from these rules: Engine type Jet Propeller Wing Position C130 Low High B747 Bulges None Aft Wing C5A C141
  • 10. Properties of binary trees 1. The number of external nodes is 1 more than the number of internal nodes. It is easy to see this if we start removing external nodes with their internal parent, one pair at a time (assume that a method removeAboveExternal(n) does this). At the end of this process, only the root with its two external children will remain. 2. The number of external nodes is at least h + 1, where h is the height of the tree, and at most 2 h . The later holds for a full binary tree, which is a tree where internal nodes completely fill every level. 3. The number of internal nodes is at least h and at most 2h - 1. 4. The total number of nodes in a binary tree is at least 2*h + 1 and at most 2 h+1 - 1. 5. The height, h, of a binary tree with n nodes is at least log n+1 and at most n. 6. A binary tree with n nodes has exactly n - 1 edges.
  • 11. Full binary trees and complete binary trees Here is an example of a full binary tree: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 A complete binary tree is a full binary tree where the internal nodes on the bottom level all appear to the left of the external nodes on that level. Here is an example of a complete binary tree: 1 2 3 4 5 6
  • 12. Properties of binary trees (cont.) The following property holds for a complete binary tree. Let i be a number assigned to a node in a complete binary tree. Then: 1. If i = 1, then this node is the root of the tree. If i > 1, then the parent of this node is assigned the number (i / 2). 2. If 2*i > n, then the corresponding node has no left child. Otherwise, the left child of that node is assigned the number 2*i. 3. If 2*i + 1 > n, then the corresponding node has no right child. Otherwise, the right child of that node is assigned the number 2*i + 1. This property suggests a trivial array-based representation of a complete binary tree, where i is the index of the node in the array. We will see that a slight modification in this representation allows us to represent any binary tree in a linear fashion.
  • 13. Linear (or sequence-based) representation of a binary tree Linear representation of a binary tree utilizes one-dimensional array of size 2 h+1 - 1. Consider the following tree: + level 0 (d = 0) - * level 1 (d = 1) A B C / level 2 (d = 2) E F level 3 (d = 3) To represent this tree, we need an array of size 23+1 - 1 = 15 The tree is represented as follows: 1. The root is stored in BinaryTree[1]. 2. For node BinaryTree[n], the left child is stored in BinaryTree[2*n], and the right child is stored in BinaryTree[2*n+1] i: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 BinaryTree[i]: + - * A B C / E F
  • 14. Linear representation of a binary tree (cont.) Advantages of linear representation: 1. Simplicity. 2. Given the location of the child (say, k), the location of the parent is easy to determine (k / 2). Disadvantages of linear representation: 1. Additions and deletions of nodes are inefficient, because of the data movements in the array. 2. Space is wasted if the binary tree is not complete. That is, the linear representation is useful if the number of missing nodes is small. Note that linear representation of a binary tree can be implemented by means of a linked list instead of an array. For example, we can use the Positional Sequence ADT to implement a binary tree in a linear fashion. This way the above mentioned disadvantages of the linear representation will be resolved.
  • 15. Linked representation of a binary tree Linked representation uses explicit links to connect the nodes. Example: 1 2 5 3 4 6 7 8 9 Nodes in this tree can be viewed as positions in a sequence (numbered 1 through 9). + BA - * C / E F
  • 16. Traversals of a binary tree Preorder traversal void preOrder (BTNode localRoot) { if (localRoot != null) { localRoot.displayBTNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); } } Example Consider a tree with an ordering property, where nodes are inserted in the following order b i n a r y t r e e, i.e. b a i e n e r y t r The preorder traversal is: b a i e e n r y t r
  • 17. Traversals of a binary tree (cont.) Post-order traversal void postOrder (BTNode localRoot) { if (localRoot != null) { postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayBTNode(); } } The nodes in the example tree are traversed in post-order as follows: a e e r t y r n i b In-order traversal void inOrder (BTNode localRoot) { if (localRoot != null) { inOrder(localRoot.leftChild); localRoot.displayBTNode(); inOrder(localRoot.rightChild); } } The nodes in the example tree are traversed in in-order as follows: a b e e i n r r t y
  • 18. A Binary Search Tree is a binary tree with the following properties:  All items in the left subtree are less than the root.  All items in the right subtree are greater or equal to the root.  Each subtree is itself a binary search tree.
  • 19. Basic Property  In a binary search tree,  the left subtree contains key values less than the root  the right subtree contains key values greater than or equal to the root.
  • 20. 7-1 Basic Concepts Binary search trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into the list.
  • 21.
  • 22. Insertion in binary search tree Insert 9 in the the following tree: 3 2 15 1 11 7 13 Step 1: search for 9 3 2 15 1 11 search stops here 7 13 Step 2: insert 9 at the point where the search terminates unsuccessfully 3 2 15 1 11 7 13 That is, new nodes are always inserted at the leaf level. 9
  • 23. Deletion in binary search tree Consider the tree: 3 7 2 15 Deleting 3 2 15 1 11 1 11 7 13 13 The following cases of deletions are possible: 1. Delete a note with no children, for example 1. This only requires the appropriate link in the parent node to be made null. 2. Delete a node which has only one child, for example 15. In this case, we must set the corresponding child link of the parent’s parent to point to the only child of the node being deleted. 3. Delete a node with two children, for example 3. The delete method is based on the following consideration: in-order traversal of the resulting tree (after delete operation) must yield an ordered list. To ensure this, the following steps are carried out: Step 1: Replace 3 with the node with the next largest datum, i.e. 7. Step 2: Make the left link of 11 point to the right child of 7 (which is null here). Step 3: Copy the links from the node containing 3 to the node containing 7, and make the parent node of 3 point to 7.