SlideShare a Scribd company logo
10
Most read
D A T A S T R U C T U R E S
Tree:
So far, we have been studying mainly linear types of data structures: arrays,
lists, stacks and queues. Now we defines a nonlinear data structure called Tree. This
structure is mainly used to represent data containing a hierarchical relationship
between nodes/elements e.g. family trees and tables of contents.
There are two main types of tree:
 General Tree 
 Binary Tree 
General Tree: 
A tree where a node can has any number of children / descendants is called
General Tree. For example:

Following figure is an example of general tree where root is “Desktop”.
D A T A S T R U C T U R E S
Binary Tree:
A tree in which each element may has 0-child , 1-child or maximum of 2-children.
A Binary Tree T is defined as finite set of elements, called nodes, such that:
a) T is empty (called the null tree or empty tree.)
b) T contains a distinguished node R, called the root of T, and the remaining
nodes of T form an ordered pair of disjoint binary trees T1 and T2.
If T does contain a root R, then the two trees T1 and T2 are called, respectively, the
left sub tree and right sub tree of R.
D A T A S T R U C T U R E S
If T1 is non empty, then its node is called the left successor of R; similarly, if T 2 is
non empty, then its node is called the right successor of R. The nodes with no
successors are called the terminal nodes. If N is a node in T with left successor S1
and right successor S2, then N is called the parent(or father) of S1 and S2.
Analogously, S1 is called the left child (or son) of N, and S 2 is called the right child
(or son) of N. Furthermore, S1 and S2 are said to siblings (or brothers). Every
node in the binary tree T, except the root, has a unique parent, called the
predecessor of N. The line drawn from a node N of T to a successor is called an
edge, and a sequence of consecutive edges is called a path. A terminal node is
called a leaves, and a path ending in a leaves is called a branch.
The depth (or height) of a tree T is the maximum number of nodes in a
branch of T. This turns out to be 1 more than the largest level number of T.
Level of node & its generation:
Each node in binary tree T is assigned a level number, as follows. The root R
of the tree T is assigned the level number 0, and every other node is assigned a
level number which is 1 more than the level number of its parent. Furthermore,
those nodes with the same level number are said to belong to the same generation.
D A T A S T R U C T U R E S
Complete Binary Tree:
Consider a binary tree T. each node of T can have at most two children.
Accordingly, one can show that level n-2 of T can have at most two nodes.
A tree is said to be complete if all its levels, except possibly the last have the
maximum number of possible nodes, and if all the nodes at the last level appear as
far left as possible.
A
B E
F G L M
N O P
Extended Binary Tree: 2-Tree:
A binary tree T is said to be a 2-tree or an extended binary tree if each node
N has either 0 or 2 children.
In such a case, the nodes, with 2 children are called internal nodes, and the
node with 0 children are called external node.
A
A
B
B
Binary Tree Extended 2-tree
D A T A S T R U C T U R E S
Traversing of Binary Tree:
A traversal of a tree T is a systematic way of accessing or visiting all the node of T.
There are three standard ways of traversing a binary tree T with root R. these are :
 Preorder (N L R): 
a) Process the node/root. (A B D F I C G H J L K)
b) Traverse the Left sub tree.
c) Traverse the Right sub tree.
 Inorder (L N R): ( D B I F A G C L J H K )
a) Traverse the Left sub tree.
b) Process the node/root.
c) Traverse the Right sub tree.
 Postorder (L R N): ( D I F B G L J K H C A )
a) Traverse the Left sub tree.
b) Traverse the Right sub tree.
c) Process the node/root.
 Descending order (R N L): ( K H J L C G A F I B D )
(Used in Binary Search Tree, will be discussed later)
a) Traverse the Right sub tree.
b) Process the node/root.
c) Traverse the Left sub tree.
D A T A S T R U C T U R E S
Preorder Traversal:
Algorithm: PREORDER (pRoot)
First time this function is called by passing original root into pRoot.
Here pRoot is pointers pointing to current root. This algorithm does a
preorder traversal of T, applying by rcurrsively calling same function
and updating proot to traverse in a way i.e. (NLR) Node, Left, Right.
1. If (pRoot NOT = NULL) then: [ does child exist ?]
i- Apply PROCESS to pRoot-> info. e.g. Write: pRoot -> info
[ recursive call by passing address of left child to update pRoot]
ii- PREORDER (pRoot -> Left)
[ recursive call by passing address of right child to update pRoot]
iii- PREORDER( pRoot -> Right)
[End of If structure.]
2. Exit.
Inorder Traversal:
Algorithm: INORDER (pRoot)
First time this function is called by passing original root into pRoot.
Here pRoot is pointers pointing to current root. This algorithm does a
Inorder traversal of T, applying by rcurrsively calling same function
and updating proot to traverse in a way i.e. (LNR) Left, Node, Right.
1. If (pRoot NOT = NULL) then: [ does child exist ?]
[ recursive call by passing address of left child to update pRoot]
i- PREORDER (pRoot -> Left)
ii- Apply PROCESS to pRoot-> info. e.g. Write: pRoot -> info
[ recursive call by passing address of right child to update pRoot]
iii- PREORDER( pRoot ->
Right) [End of If structure.]
2. Exit.
Postorder Traversal:
Algorithm: POSTORDER (pRoot)
First time this function is called by passing original root into pRoot.
Here pRoot is pointers pointing to current root. This algorithm does a
Postorder traversal of T, applying by rcurrsively calling same function
and updating proot to traverse in a way i.e. (LRN) Left, Right, Node.
1. If (pRoot NOT = NULL) then: [ does child exist ?]
[ recursive call by passing address of left child to update pRoot]
i- PREORDER (pRoot -> Left)
[ recursive call by passing address of right child to update pRoot]
ii- PREORDER( pRoot -> Right)
iii- Apply PROCESS to pRoot-> info. e.g. Write: pRoot -> info
[End of If structure.]
2. Exit.
D A T A S T R U C T U R E S
Preparing a Tree from an infix arithmetic expression
Recursion:
For implementing tree traversal logics as stated, two approaches are used, i.e. use
stacks or recursive functions.
Recursion is an important concept in computer science. Many algorithms can be best
described in terms of recursion. A recursive function / procedure containing a Call
statement to itself. To make a function recursive one must consider the following
properties:
(1) There must be certain (using arguments), called base criteria, for which the
procedure / function does not call itself.
(2) Each time the procedure / function does call itself, control must be closer to
the base criteria.
D A T A S T R U C T U R E S
Binary Search Tree:
Suppose T is a binary tree, the T is called a binary search tree or binary
sorted tree if each node N of T has the following property:
The values of at N (node) is greater than every value in the left sub tree of
N and is less than every value in the right sub tree of N.
Binary Search Tree using these values: (50, 30, 55, 25, 10, 35, 37, 31, 20, 53, 60,
62)
 50 
 30

55

 25 35 53 60
 

10 62
31 37
20
Following figure shows a binary search tree. Notice that this tree is obtained by inserting
the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an
empty tree.
D A T A S T R U C T U R E S
 Sorting: Note that inorder traversal of a binary search tree always gives a sorted
sequence of the values. This is a direct consequence of the BST property. This
provides a way of sorting a given sequence of keys: first, create a BST with these
keys and then do an inorder traversal of the BST so created. 
Inorder Travers (LNR) : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18 

 Search: is straightforward in a BST. Start with the root and keep moving left or
right using the BST property. If the key we are seeking is present, this search
procedure will lead us to the key. If the key is not present, we end up in a null
link. 
 Insertion in a BST is also a straightforward operation. If we need to insert an
element n, we traverse tree starting from root by considering the above stated
rules. Our traverse procedure ends in a null link. It is at this position of this null
link that n will be included. . 
 Deletion in BST: Let x be a value to be deleted from the BST and let N denote
the node containing the value x. Deletion of an element in a BST again uses the
BST property in a critical way. When we delete the node N containing x, it would
create a "gap" that should be filled by a suitable existing node of the BST. There
are two possible candidate nodes that can fill this gap, in a way that the BST
property is not violated: (1). Node containing highest valued element among all
descendants of left child of N. (2). Node containing the lowest valued element
among all the descendants of the right child of N. There are three possible cases to
consider: 
Deleting a leaf (node with no children): Deleting a leaf is easy, as we can
simply remove it from the tree. 
Deleting a node with one child: Delete it and replace it with its child. 
Deleting a node with two children: Call the node to be deleted "N". Do not
delete N. Instead, choose its in-order successor node "S". Replace the value of 
“N” with the value of “S”. (Note: S itself has up to one child.) 
As with all binary trees, a node's in-order successor is the left-most child of its
right subtree. This node will have zero or one child. Delete it according to one of
the two simpler cases above. 

Figure on next page illustrates several scenarios for deletion in BSTs. 
D A T A S T R U C T U R E S
Ad

Recommended

Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Daniyal Khan
 
Tree in data structure
Tree in data structure
ghhgj jhgh
 
Linked lists
Linked lists
SARITHA REDDY
 
B and B+ tree
B and B+ tree
Ashish Arun
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
b+ tree
b+ tree
bitistu
 
Different types of Linked list.
Different types of Linked list.
JAYANTA OJHA
 
Binary tree
Binary tree
Vanitha Chandru
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Queue as data_structure
Queue as data_structure
eShikshak
 
Chap 5 Tree.ppt
Chap 5 Tree.ppt
shashankbhadouria4
 
Binary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Binary tree
Binary tree
Rajendran
 
single linked list
single linked list
Sathasivam Rangasamy
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Tree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Linked list
Linked list
eShikshak
 
Tree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
 
Searching in c language
Searching in c language
CHANDAN KUMAR
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Searching and sorting
Searching and sorting
PoojithaBollikonda
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
linear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Data structure - Graph
Data structure - Graph
Madhu Bala
 
Red black tree
Red black tree
Rajendran
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Consultores para la Reactivación Económica y el Autoempleo
Consultores para la Reactivación Económica y el Autoempleo
David Solves
 
Electrical power Engineer
Electrical power Engineer
Ibrahim Mahmoud
 

More Related Content

What's hot (20)

Threaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Queue as data_structure
Queue as data_structure
eShikshak
 
Chap 5 Tree.ppt
Chap 5 Tree.ppt
shashankbhadouria4
 
Binary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Binary tree
Binary tree
Rajendran
 
single linked list
single linked list
Sathasivam Rangasamy
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Tree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Linked list
Linked list
eShikshak
 
Tree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
 
Searching in c language
Searching in c language
CHANDAN KUMAR
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Searching and sorting
Searching and sorting
PoojithaBollikonda
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
linear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Data structure - Graph
Data structure - Graph
Madhu Bala
 
Red black tree
Red black tree
Rajendran
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 

Viewers also liked (15)

Consultores para la Reactivación Económica y el Autoempleo
Consultores para la Reactivación Económica y el Autoempleo
David Solves
 
Electrical power Engineer
Electrical power Engineer
Ibrahim Mahmoud
 
AggelikiLamprou1053460, StellaAnagnostopoulou1053396
AggelikiLamprou1053460, StellaAnagnostopoulou1053396
aggeliki lamp
 
Los mandatos
Los mandatos
nickclift
 
DFSO p1
DFSO p1
manuel alejandro gonzalez robles
 
Term paper
Term paper
orderyouressays
 
Definiciones de conceptos (tic's)
Definiciones de conceptos (tic's)
Eduardo Vea Royo
 
Ganadores direct latinoamericanos cannes lions 2015
Ganadores direct latinoamericanos cannes lions 2015
letskalk
 
Ppt akhlaq
Ppt akhlaq
anniemasni123
 
Ppt akhlaq
Ppt akhlaq
anniemasni123
 
Fine Homes Summer 2013 Article
Fine Homes Summer 2013 Article
Nathan Stobbe
 
El cambio-educativo-desde-la-investigacion-accion. elliot
El cambio-educativo-desde-la-investigacion-accion. elliot
Hermila A
 
Resume
Resume
sundararajulu durairaj
 
2016 - Femi Osofisan's Vagabond Minstrels
2016 - Femi Osofisan's Vagabond Minstrels
KemiIlori
 
Pp fundamentos
Pp fundamentos
CarmenSlideSH
 
Consultores para la Reactivación Económica y el Autoempleo
Consultores para la Reactivación Económica y el Autoempleo
David Solves
 
Electrical power Engineer
Electrical power Engineer
Ibrahim Mahmoud
 
AggelikiLamprou1053460, StellaAnagnostopoulou1053396
AggelikiLamprou1053460, StellaAnagnostopoulou1053396
aggeliki lamp
 
Los mandatos
Los mandatos
nickclift
 
Definiciones de conceptos (tic's)
Definiciones de conceptos (tic's)
Eduardo Vea Royo
 
Ganadores direct latinoamericanos cannes lions 2015
Ganadores direct latinoamericanos cannes lions 2015
letskalk
 
Fine Homes Summer 2013 Article
Fine Homes Summer 2013 Article
Nathan Stobbe
 
El cambio-educativo-desde-la-investigacion-accion. elliot
El cambio-educativo-desde-la-investigacion-accion. elliot
Hermila A
 
2016 - Femi Osofisan's Vagabond Minstrels
2016 - Femi Osofisan's Vagabond Minstrels
KemiIlori
 
Ad

Similar to Lecture notes data structures tree (20)

481803111-Trees-Unit-Review-pdf - Copy.pdf
481803111-Trees-Unit-Review-pdf - Copy.pdf
barsubiasarah
 
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
7.tree
7.tree
Chandan Singh
 
Dsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
Tree.pptx
Tree.pptx
worldchannel
 
Tree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Unit 4 tree
Unit 4 tree
kalyanineve
 
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
ticonah393
 
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
timspizer25
 
non linear data structure -introduction of tree
non linear data structure -introduction of tree
Siddhi Viradiya
 
Tree
Tree
bhumish
 
Trees in Data Structure
Trees in Data Structure
Om Prakash
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 
Unit 3.ppt
Unit 3.ppt
JITTAYASHWANTHREDDY
 
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
ANANDSHOE
 
Data Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Trees
Trees
Speaking Technology
 
9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
481803111-Trees-Unit-Review-pdf - Copy.pdf
481803111-Trees-Unit-Review-pdf - Copy.pdf
barsubiasarah
 
Lecture 5 tree.pptx
Lecture 5 tree.pptx
Abirami A
 
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
ticonah393
 
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
timspizer25
 
non linear data structure -introduction of tree
non linear data structure -introduction of tree
Siddhi Viradiya
 
Trees in Data Structure
Trees in Data Structure
Om Prakash
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
ANANDSHOE
 
Data Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
Ad

More from maamir farooq (20)

Ooad lab1
Ooad lab1
maamir farooq
 
Lesson 03
Lesson 03
maamir farooq
 
Lesson 02
Lesson 02
maamir farooq
 
Php client libray
Php client libray
maamir farooq
 
Swiftmailer
Swiftmailer
maamir farooq
 
Lect15
Lect15
maamir farooq
 
Lec 7
Lec 7
maamir farooq
 
Lec 6
Lec 6
maamir farooq
 
Lec 5
Lec 5
maamir farooq
 
J query 1.7 cheat sheet
J query 1.7 cheat sheet
maamir farooq
 
Assignment
Assignment
maamir farooq
 
Java script summary
Java script summary
maamir farooq
 
Lec 3
Lec 3
maamir farooq
 
Lec 2
Lec 2
maamir farooq
 
Lec 1
Lec 1
maamir farooq
 
Css summary
Css summary
maamir farooq
 
Manual of image processing lab
Manual of image processing lab
maamir farooq
 
Session management
Session management
maamir farooq
 
Data management
Data management
maamir farooq
 
Content provider
Content provider
maamir farooq
 

Recently uploaded (20)

BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
“THE BEST CLASS IN SCHOOL”. _
“THE BEST CLASS IN SCHOOL”. _
Colégio Santa Teresinha
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 

Lecture notes data structures tree

  • 1. D A T A S T R U C T U R E S Tree: So far, we have been studying mainly linear types of data structures: arrays, lists, stacks and queues. Now we defines a nonlinear data structure called Tree. This structure is mainly used to represent data containing a hierarchical relationship between nodes/elements e.g. family trees and tables of contents. There are two main types of tree:  General Tree   Binary Tree  General Tree:  A tree where a node can has any number of children / descendants is called General Tree. For example:  Following figure is an example of general tree where root is “Desktop”.
  • 2. D A T A S T R U C T U R E S Binary Tree: A tree in which each element may has 0-child , 1-child or maximum of 2-children. A Binary Tree T is defined as finite set of elements, called nodes, such that: a) T is empty (called the null tree or empty tree.) b) T contains a distinguished node R, called the root of T, and the remaining nodes of T form an ordered pair of disjoint binary trees T1 and T2. If T does contain a root R, then the two trees T1 and T2 are called, respectively, the left sub tree and right sub tree of R.
  • 3. D A T A S T R U C T U R E S If T1 is non empty, then its node is called the left successor of R; similarly, if T 2 is non empty, then its node is called the right successor of R. The nodes with no successors are called the terminal nodes. If N is a node in T with left successor S1 and right successor S2, then N is called the parent(or father) of S1 and S2. Analogously, S1 is called the left child (or son) of N, and S 2 is called the right child (or son) of N. Furthermore, S1 and S2 are said to siblings (or brothers). Every node in the binary tree T, except the root, has a unique parent, called the predecessor of N. The line drawn from a node N of T to a successor is called an edge, and a sequence of consecutive edges is called a path. A terminal node is called a leaves, and a path ending in a leaves is called a branch. The depth (or height) of a tree T is the maximum number of nodes in a branch of T. This turns out to be 1 more than the largest level number of T. Level of node & its generation: Each node in binary tree T is assigned a level number, as follows. The root R of the tree T is assigned the level number 0, and every other node is assigned a level number which is 1 more than the level number of its parent. Furthermore, those nodes with the same level number are said to belong to the same generation.
  • 4. D A T A S T R U C T U R E S Complete Binary Tree: Consider a binary tree T. each node of T can have at most two children. Accordingly, one can show that level n-2 of T can have at most two nodes. A tree is said to be complete if all its levels, except possibly the last have the maximum number of possible nodes, and if all the nodes at the last level appear as far left as possible. A B E F G L M N O P Extended Binary Tree: 2-Tree: A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children. In such a case, the nodes, with 2 children are called internal nodes, and the node with 0 children are called external node. A A B B Binary Tree Extended 2-tree
  • 5. D A T A S T R U C T U R E S Traversing of Binary Tree: A traversal of a tree T is a systematic way of accessing or visiting all the node of T. There are three standard ways of traversing a binary tree T with root R. these are :  Preorder (N L R):  a) Process the node/root. (A B D F I C G H J L K) b) Traverse the Left sub tree. c) Traverse the Right sub tree.  Inorder (L N R): ( D B I F A G C L J H K ) a) Traverse the Left sub tree. b) Process the node/root. c) Traverse the Right sub tree.  Postorder (L R N): ( D I F B G L J K H C A ) a) Traverse the Left sub tree. b) Traverse the Right sub tree. c) Process the node/root.  Descending order (R N L): ( K H J L C G A F I B D ) (Used in Binary Search Tree, will be discussed later) a) Traverse the Right sub tree. b) Process the node/root. c) Traverse the Left sub tree.
  • 6. D A T A S T R U C T U R E S Preorder Traversal: Algorithm: PREORDER (pRoot) First time this function is called by passing original root into pRoot. Here pRoot is pointers pointing to current root. This algorithm does a preorder traversal of T, applying by rcurrsively calling same function and updating proot to traverse in a way i.e. (NLR) Node, Left, Right. 1. If (pRoot NOT = NULL) then: [ does child exist ?] i- Apply PROCESS to pRoot-> info. e.g. Write: pRoot -> info [ recursive call by passing address of left child to update pRoot] ii- PREORDER (pRoot -> Left) [ recursive call by passing address of right child to update pRoot] iii- PREORDER( pRoot -> Right) [End of If structure.] 2. Exit. Inorder Traversal: Algorithm: INORDER (pRoot) First time this function is called by passing original root into pRoot. Here pRoot is pointers pointing to current root. This algorithm does a Inorder traversal of T, applying by rcurrsively calling same function and updating proot to traverse in a way i.e. (LNR) Left, Node, Right. 1. If (pRoot NOT = NULL) then: [ does child exist ?] [ recursive call by passing address of left child to update pRoot] i- PREORDER (pRoot -> Left) ii- Apply PROCESS to pRoot-> info. e.g. Write: pRoot -> info [ recursive call by passing address of right child to update pRoot] iii- PREORDER( pRoot -> Right) [End of If structure.] 2. Exit. Postorder Traversal: Algorithm: POSTORDER (pRoot) First time this function is called by passing original root into pRoot. Here pRoot is pointers pointing to current root. This algorithm does a Postorder traversal of T, applying by rcurrsively calling same function and updating proot to traverse in a way i.e. (LRN) Left, Right, Node. 1. If (pRoot NOT = NULL) then: [ does child exist ?] [ recursive call by passing address of left child to update pRoot] i- PREORDER (pRoot -> Left) [ recursive call by passing address of right child to update pRoot] ii- PREORDER( pRoot -> Right) iii- Apply PROCESS to pRoot-> info. e.g. Write: pRoot -> info [End of If structure.] 2. Exit.
  • 7. D A T A S T R U C T U R E S Preparing a Tree from an infix arithmetic expression Recursion: For implementing tree traversal logics as stated, two approaches are used, i.e. use stacks or recursive functions. Recursion is an important concept in computer science. Many algorithms can be best described in terms of recursion. A recursive function / procedure containing a Call statement to itself. To make a function recursive one must consider the following properties: (1) There must be certain (using arguments), called base criteria, for which the procedure / function does not call itself. (2) Each time the procedure / function does call itself, control must be closer to the base criteria.
  • 8. D A T A S T R U C T U R E S Binary Search Tree: Suppose T is a binary tree, the T is called a binary search tree or binary sorted tree if each node N of T has the following property: The values of at N (node) is greater than every value in the left sub tree of N and is less than every value in the right sub tree of N. Binary Search Tree using these values: (50, 30, 55, 25, 10, 35, 37, 31, 20, 53, 60, 62)  50   30  55   25 35 53 60    10 62 31 37 20 Following figure shows a binary search tree. Notice that this tree is obtained by inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree.
  • 9. D A T A S T R U C T U R E S  Sorting: Note that inorder traversal of a binary search tree always gives a sorted sequence of the values. This is a direct consequence of the BST property. This provides a way of sorting a given sequence of keys: first, create a BST with these keys and then do an inorder traversal of the BST so created.  Inorder Travers (LNR) : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18    Search: is straightforward in a BST. Start with the root and keep moving left or right using the BST property. If the key we are seeking is present, this search procedure will lead us to the key. If the key is not present, we end up in a null link.   Insertion in a BST is also a straightforward operation. If we need to insert an element n, we traverse tree starting from root by considering the above stated rules. Our traverse procedure ends in a null link. It is at this position of this null link that n will be included. .   Deletion in BST: Let x be a value to be deleted from the BST and let N denote the node containing the value x. Deletion of an element in a BST again uses the BST property in a critical way. When we delete the node N containing x, it would create a "gap" that should be filled by a suitable existing node of the BST. There are two possible candidate nodes that can fill this gap, in a way that the BST property is not violated: (1). Node containing highest valued element among all descendants of left child of N. (2). Node containing the lowest valued element among all the descendants of the right child of N. There are three possible cases to consider:  Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree.  Deleting a node with one child: Delete it and replace it with its child.  Deleting a node with two children: Call the node to be deleted "N". Do not delete N. Instead, choose its in-order successor node "S". Replace the value of  “N” with the value of “S”. (Note: S itself has up to one child.)  As with all binary trees, a node's in-order successor is the left-most child of its right subtree. This node will have zero or one child. Delete it according to one of the two simpler cases above.   Figure on next page illustrates several scenarios for deletion in BSTs. 
  • 10. D A T A S T R U C T U R E S