SlideShare a Scribd company logo
1 of 42
Download to read offline
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Binary Trees
Binary Trees page 2
© Dr. Jonathan (Yahya) Cazalas
Outline
 Tree Stuff
 Trees
 Binary Trees
 Implementation of a Binary Tree
 Tree Traversals – Depth First
 Preorder
 Inorder
 Postorder
 Breadth First Tree Traversal
 Binary Search Trees
Binary Trees page 3
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 Trees:
 Another Abstract Data Type
 Data structure made of nodes and pointers
 Much like a linked list
 The difference between the two is how they are organized.
 A linked list represents a linear structure
 A predecessor/successor relationship between the nodes of
the list
 A tree represents a hierarchical relationship between
the nodes (ancestral relationship)
 A node in a tree can have several successors, which we refer
to as children
 A nodes predecessor would be its parent
Binary Trees page 4
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 Trees:
 General Tree Information:
 Top node in a tree is called the root
 the root node has no parent above it…cuz it’s the root!
 Every node in the tree can have “children” nodes
 Each child node can, in turn, be a parent to its children and
so on
 Nodes having no children are called leaves
 Any node that is not a root or a leaf is an interior
node
 The height of a tree is defined to be the length of the
longest path from the root to a leaf in that tree.
 A tree with only one node (the root) has a height of zero.
Binary Trees page 5
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 Trees:
 Here’s a purty picture of a tree:
 2 is the root
 2, 5, 11, and 4 are leaves
 7, 5, 6, and 9 are interior
nodes
Binary Trees page 6
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 Trees:
 Subtree:
 A subtree of a tree, T, is a tree consisting of a node, in
T, and all of its children (descendants).
Binary Trees page 7
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 Binary Trees:
 A tree in which each node can have a maximum of
two children
 Each node can have no child, one child, or two children
 And a child can only have one parent
 Pointers help us to identify if it is a right child or a left one
Examples of two
Binary Trees:
Binary Trees page 8
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 Examples of trees that are NOT Binary Trees:
Binary Trees page 9
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 More Binary Tree Goodies:
 A full binary tree:
 Every node, other than the leaves, has two children
a
b c
d e
g h
g, h, e, and c are
leaves: so they
have no children.
Binary Trees page 10
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 More Binary Tree Goodies:
 A complete binary tree:
 Every level, except possibly the last, is completely
filled, and all nodes are as far left as possible.
Binary Trees page 11
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 More Binary Tree Goodies:
 The root of the tree is at level 0
 The level of any other node in the tree is one
more than the level of its parent
 Total # of nodes (n) in a
complete binary tree:
 n = 2h+1 – 1 (maximum)
 Height (h) of the tree:
 h = log((n + 1)/2)
 If we have 15 nodes
 h = log(16/2) = log(8) = 3
Binary Trees page 12
© Dr. Jonathan (Yahya) Cazalas
Tree Stuff
 Implementation of a Binary Tree:
 A binary tree has a natural implementation using
linked storage
 Each node of a binary tree has both left and
right subtrees that can be reached with pointers:
left_child data right_child
public class intBSTnode {
protected int data;
protected intBSTnode left, right;
// Constructors go here
...
}
Binary Trees page 13
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals – Depth First
 Traversal of Binary Trees:
 We need a way of zipping through a tree for
searching, inserting, etc.
 But how can we do this?
 If you remember…
 Linked lists are traversed from the head to the last node
…sequentially
 Can’t we just “do that” for binary trees.?.
 NO! There is no such natural linear ordering for nodes of a tree.
 Turns out, there are THREE ways/orderings of
traversing a binary tree:
 Preorder, Inorder, and Postorder
Binary Trees page 14
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals – Depth First
But before we get into the
nitty gritty of those three,
let’s describe..
Binary Trees page 15
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals – Depth First
 A depth-first search (DFS)
explores a path all the way to
a leaf before backtracking and
exploring another path
 For example, after searching
A, then B, then D, the search
backtracks and tries another
path from B
 Node are explored in the order
A B D E H L M N I O P C F
G J K Q
 N will be found before J
L M N O P
G
Q
H J
I K
F
E
D
B C
A
Binary Trees page 16
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals – Depth First
 Traversal of Binary Trees:
 There are 3 ways/orderings of traversing a
binary tree (all 3 are depth first search
methods):
 Preorder, Inorder, and Postorder
 These names are chosen according to the step at
which the root node is visited:
 With preorder traversal, the root is visited before its left
and right subtrees.
 With inorder traversal, the root is visited between the
subtrees.
 With postorder traversal, the root is visited after both
subtrees.
Binary Trees page 17
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals - Preorder
 Preorder Traversal
 the root is visited before its left and right
subtrees
 For the following example, the “visiting” of a node is
represented by printing that node
 Code for Preorder Traversal:
public void preorder (intBSTnode p) {
if (p != NULL) {
System.out.print(p.data + “ ”);
preorder(p.left);
preorder(p.right);
}
}
Binary Trees page 18
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals - Preorder
a
b c
a b c
 Preorder Traversal – Example 1
 the root is visited before its left and right
subtrees
Binary Trees page 19
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals - Preorder
a
b c
d e
f
g h i j
a b d g h e i c f j
 Preorder Traversal – Example 2
Order of Visiting Nodes:
Binary Trees page 20
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals - Inorder
 Inorder Traversal
 the root is visited between the left and right
subtrees
 For the following example, the “visiting” of a node is
represented by printing that node
 Code for Inorder Traversal:
public void inorder (intBSTnode p) {
if (p != NULL) {
inorder(p.left);
System.out.print(p.data + “ ”);
inorder(p.right);
}
}
Binary Trees page 21
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals - Inorder
a
b c
b a c
 Inorder Traversal – Example 1
 the root is visited between the subtrees
Binary Trees page 22
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals - Inorder
a
b c
d e
f
g h i j
g d h b e i a f j c
 Inorder Traversal – Example 2
Order of Visiting Nodes:
Binary Trees page 23
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals – Postorder
 Postorder Traversal
 the root is visited after both the left and right
subtrees
 For the following example, the “visiting” of a node is
represented by printing that node
 Code for Postorder Traversal:
public void postorder (intBSTnode p) {
if (p != NULL) {
postorder(p.left);
postorder(p.right);
System.out.print(p.data + “ ”);
}
}
Binary Trees page 24
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals – Postorder
a
b c
b c a
 Postorder Traversal – Example 1
 the root is visited after both subtrees
Binary Trees page 25
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals – Postorder
a
b c
d e
f
g h i j
g h d i e b j f c a
 Postorder Traversal – Example 2
Order of Visiting Nodes:
Binary Trees page 26
© Dr. Jonathan (Yahya) Cazalas
Tree Traversals
 Final Traversal Example
Binary Trees page 27
© Dr. Jonathan (Yahya) Cazalas
Bike Fail
Binary Trees page 28
© Dr. Jonathan (Yahya) Cazalas
Breadth-First Traversal
 A breadth-first search (BFS)
explores nodes nearest the
root before exploring nodes
further away
 For example, after searching
A, then B, then C, the search
proceeds with D, E, F, G
 Node are explored in the
order A B C D E F G H I J K L M
N O P Q
 J will be found before N
L M N O P
G
Q
H J
I K
F
E
D
B C
A
Binary Trees page 29
© Dr. Jonathan (Yahya) Cazalas
Breadth-First Traversal
H
D
B
A C E G I K M O
N
L
J
F
O
M
K
I
G
E
C
A
N
J
F
B
L
D
H
Binary Trees page 30
© Dr. Jonathan (Yahya) Cazalas
Breadth-First Traversal
 Coding the Breadth-First Traversal
 Let’s say you want to Traverse and Print all nodes?
 Think about it, how would you make this happen?
 SOLUTION:
1) Enqueue the root node.
2) while (more nodes still in queue) {
Dequeue node at front (of queue)
Print this node (that we just dequeued)
Enqueue its children (if applicable): left then right
…continue till no more nodes in queue
}
Binary Trees page 31
© Dr. Jonathan (Yahya) Cazalas
Binary Search Tree
 Binary Search Trees
 We’ve seen how to traverse binary trees
 But it is not quite clear how this data structure
helps us
 What is the purpose of binary trees?
 What if we added a restriction…
 Consider the following
binary tree:
 What pattern can you see?
Binary Trees page 32
© Dr. Jonathan (Yahya) Cazalas
Binary Search Tree
 Binary Search Trees
 What pattern can you see?
 For each node N, all the values stored in the left subtree
of N are LESS than the value stored in N.
 Also, all the values stored in the right subtree of N are
GREATER than the value stored in N.
 Why might this property be a desireable one?
 Searching for a node is super fast!
 Normally, if we search through n nodes, it takes O(n) time
 But notice what is going on here:
 This ordering property of the tree tells us where to search
 We choose to look to the left OR look to the right of a node
 We are HALVING the search space …O(log n) time
Binary Trees page 33
© Dr. Jonathan (Yahya) Cazalas
Binary Search Tree
 Binary Search Trees
 Details:
 ALL of the data values in the left subtree of each node are
smaller than the data value in the node itself (root of said
subtree)
 Stated another way, the value of the node itself is larger than
the value of every node in its left subtree.
 ALL of the data values in the right subtree of each node are
larger than the data value in the node itself (root of the
subtree)
 Stated another way, the value of the node itself is smaller than
the value of every node in its right subtree.
 Both the left and right subtrees, of any given node, are
themselves binary search trees.
Binary Trees page 34
© Dr. Jonathan (Yahya) Cazalas
44
24
20
8 19
28
15
30 42
36
40
65
56
62
64
58
88
A Binary Search Tree
Binary Search Tree
Binary Trees page 35
© Dr. Jonathan (Yahya) Cazalas
Binary Search Tree
 Binary Search Trees
 Details:
 A binary search tree, commonly referred to as a BST, is
extremely useful for efficient searching
 Basically, a BST amounts to embedding the binary search
into the data structure itself.
 Notice how the root of every subtree in the BST on the previous
page is the root of a BST.
 This ordering of nodes in the tree means that insertions
into a BST are not placed arbitrarily
 Rather, there is a specific way to insert
 …and that is for next time
Binary Trees page 36
© Dr. Jonathan (Yahya) Cazalas
Binary Trees
WASN’T
THAT
HISTORIC!
Binary Trees page 37
© Dr. Jonathan (Yahya) Cazalas
Daily Demotivator
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Binary Trees
Binary Trees page 39
© Dr. Jonathan (Yahya) Cazalas
Binary Tree Traversals – Practice Problems
3
54 71 11 56
15 36
7
26 14
33
22
19
87
8
13
9
75
28
10
63 69
59 68
44
Practice Tree #1
Solutions on page 33
Binary Trees page 40
© Dr. Jonathan (Yahya) Cazalas
Binary Tree Traversals – Practice Problems
3
54
71
11
56
15
36
7
26
14
33
22
19
87
8
13
9
75
28
10
63
69
59
68
44
Practice Tree #2
Solutions on Page 34
Binary Trees page 41
© Dr. Jonathan (Yahya) Cazalas
 Preorder Traversal:
3, 13, 22, 19, 26, 54, 71, 33, 14, 11, 87, 8, 56, 9, 75, 28, 15, 10, 63, 36, 7, 69,
59, 68, 44
 Inorder Traversal:
54, 26, 71, 19, 22, 11, 14, 33, 8, 87, 56, 13, 9, 75, 3, 63, 10, 15, 28, 59, 69, 68,
7, 36, 44
 Postorder Traversal:
54, 71, 26, 19, 11, 14, 8, 56, 87, 33, 22, 75, 9, 13, 63, 10, 15, 59, 68, 69, 7, 44,
36, 28, 3
Practice Problem Solutions – Tree #1
Binary Trees page 42
© Dr. Jonathan (Yahya) Cazalas
 Preorder Traversal:
3, 28, 36, 44, 7, 69, 68, 59, 15, 10, 63, 13, 9, 75, 22, 33, 87, 56, 8, 14, 11, 19,
26, 71, 54
 Inorder Traversal:
44, 36, 7, 68, 69, 59, 28, 15, 10, 63, 3, 75, 9, 13, 56, 87, 8, 33, 14, 11, 22, 19,
71, 26, 54
 Postorder Traversal:
44, 68, 59, 69, 7, 36, 63, 10, 15, 28, 75, 9, 56, 8, 87, 11, 14, 33, 71, 54, 26, 19,
22, 13, 3
Practice Problem Solutions – Tree #2

More Related Content

What's hot (20)

Data Structures
Data StructuresData Structures
Data Structures
 
Trees
TreesTrees
Trees
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Trees
TreesTrees
Trees
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer Science
 
Binary tree
Binary treeBinary tree
Binary tree
 
Data struters
Data strutersData struters
Data struters
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
Data structures
Data structuresData structures
Data structures
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechnic
 
Tree in data structure
Tree in data structureTree in data structure
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
 

Similar to binary_trees1

Similar to binary_trees1 (20)

Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Binary Search Tree Traversal.ppt
Binary Search Tree Traversal.pptBinary Search Tree Traversal.ppt
Binary Search Tree Traversal.ppt
 
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
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Tree
TreeTree
Tree
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
DSA IV Unit.pptx
DSA IV Unit.pptxDSA IV Unit.pptx
DSA IV Unit.pptx
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
Binary Trees.ppt
Binary Trees.pptBinary Trees.ppt
Binary Trees.ppt
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and Algorithms
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 

More from Mohamed Elsayed (20)

binary_trees4
binary_trees4binary_trees4
binary_trees4
 
binary_trees3
binary_trees3binary_trees3
binary_trees3
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
queues
queuesqueues
queues
 
stacks2
stacks2stacks2
stacks2
 
stacks1
stacks1stacks1
stacks1
 
algorithm_analysis2
algorithm_analysis2algorithm_analysis2
algorithm_analysis2
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
recursion3
recursion3recursion3
recursion3
 
recursion2
recursion2recursion2
recursion2
 
recursion1
recursion1recursion1
recursion1
 
linked_lists3
linked_lists3linked_lists3
linked_lists3
 
sorted_listmatch
sorted_listmatchsorted_listmatch
sorted_listmatch
 
binary_search
binary_searchbinary_search
binary_search
 
arrays
arraysarrays
arrays
 
introduction
introductionintroduction
introduction
 
heaps2
heaps2heaps2
heaps2
 
heaps
heapsheaps
heaps
 
hash_tables
hash_tableshash_tables
hash_tables
 
graphs
graphsgraphs
graphs
 

Recently uploaded

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 

Recently uploaded (20)

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

binary_trees1

  • 1. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Binary Trees
  • 2. Binary Trees page 2 © Dr. Jonathan (Yahya) Cazalas Outline  Tree Stuff  Trees  Binary Trees  Implementation of a Binary Tree  Tree Traversals – Depth First  Preorder  Inorder  Postorder  Breadth First Tree Traversal  Binary Search Trees
  • 3. Binary Trees page 3 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  Trees:  Another Abstract Data Type  Data structure made of nodes and pointers  Much like a linked list  The difference between the two is how they are organized.  A linked list represents a linear structure  A predecessor/successor relationship between the nodes of the list  A tree represents a hierarchical relationship between the nodes (ancestral relationship)  A node in a tree can have several successors, which we refer to as children  A nodes predecessor would be its parent
  • 4. Binary Trees page 4 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  Trees:  General Tree Information:  Top node in a tree is called the root  the root node has no parent above it…cuz it’s the root!  Every node in the tree can have “children” nodes  Each child node can, in turn, be a parent to its children and so on  Nodes having no children are called leaves  Any node that is not a root or a leaf is an interior node  The height of a tree is defined to be the length of the longest path from the root to a leaf in that tree.  A tree with only one node (the root) has a height of zero.
  • 5. Binary Trees page 5 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  Trees:  Here’s a purty picture of a tree:  2 is the root  2, 5, 11, and 4 are leaves  7, 5, 6, and 9 are interior nodes
  • 6. Binary Trees page 6 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  Trees:  Subtree:  A subtree of a tree, T, is a tree consisting of a node, in T, and all of its children (descendants).
  • 7. Binary Trees page 7 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  Binary Trees:  A tree in which each node can have a maximum of two children  Each node can have no child, one child, or two children  And a child can only have one parent  Pointers help us to identify if it is a right child or a left one Examples of two Binary Trees:
  • 8. Binary Trees page 8 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  Examples of trees that are NOT Binary Trees:
  • 9. Binary Trees page 9 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  More Binary Tree Goodies:  A full binary tree:  Every node, other than the leaves, has two children a b c d e g h g, h, e, and c are leaves: so they have no children.
  • 10. Binary Trees page 10 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  More Binary Tree Goodies:  A complete binary tree:  Every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
  • 11. Binary Trees page 11 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  More Binary Tree Goodies:  The root of the tree is at level 0  The level of any other node in the tree is one more than the level of its parent  Total # of nodes (n) in a complete binary tree:  n = 2h+1 – 1 (maximum)  Height (h) of the tree:  h = log((n + 1)/2)  If we have 15 nodes  h = log(16/2) = log(8) = 3
  • 12. Binary Trees page 12 © Dr. Jonathan (Yahya) Cazalas Tree Stuff  Implementation of a Binary Tree:  A binary tree has a natural implementation using linked storage  Each node of a binary tree has both left and right subtrees that can be reached with pointers: left_child data right_child public class intBSTnode { protected int data; protected intBSTnode left, right; // Constructors go here ... }
  • 13. Binary Trees page 13 © Dr. Jonathan (Yahya) Cazalas Tree Traversals – Depth First  Traversal of Binary Trees:  We need a way of zipping through a tree for searching, inserting, etc.  But how can we do this?  If you remember…  Linked lists are traversed from the head to the last node …sequentially  Can’t we just “do that” for binary trees.?.  NO! There is no such natural linear ordering for nodes of a tree.  Turns out, there are THREE ways/orderings of traversing a binary tree:  Preorder, Inorder, and Postorder
  • 14. Binary Trees page 14 © Dr. Jonathan (Yahya) Cazalas Tree Traversals – Depth First But before we get into the nitty gritty of those three, let’s describe..
  • 15. Binary Trees page 15 © Dr. Jonathan (Yahya) Cazalas Tree Traversals – Depth First  A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path  For example, after searching A, then B, then D, the search backtracks and tries another path from B  Node are explored in the order A B D E H L M N I O P C F G J K Q  N will be found before J L M N O P G Q H J I K F E D B C A
  • 16. Binary Trees page 16 © Dr. Jonathan (Yahya) Cazalas Tree Traversals – Depth First  Traversal of Binary Trees:  There are 3 ways/orderings of traversing a binary tree (all 3 are depth first search methods):  Preorder, Inorder, and Postorder  These names are chosen according to the step at which the root node is visited:  With preorder traversal, the root is visited before its left and right subtrees.  With inorder traversal, the root is visited between the subtrees.  With postorder traversal, the root is visited after both subtrees.
  • 17. Binary Trees page 17 © Dr. Jonathan (Yahya) Cazalas Tree Traversals - Preorder  Preorder Traversal  the root is visited before its left and right subtrees  For the following example, the “visiting” of a node is represented by printing that node  Code for Preorder Traversal: public void preorder (intBSTnode p) { if (p != NULL) { System.out.print(p.data + “ ”); preorder(p.left); preorder(p.right); } }
  • 18. Binary Trees page 18 © Dr. Jonathan (Yahya) Cazalas Tree Traversals - Preorder a b c a b c  Preorder Traversal – Example 1  the root is visited before its left and right subtrees
  • 19. Binary Trees page 19 © Dr. Jonathan (Yahya) Cazalas Tree Traversals - Preorder a b c d e f g h i j a b d g h e i c f j  Preorder Traversal – Example 2 Order of Visiting Nodes:
  • 20. Binary Trees page 20 © Dr. Jonathan (Yahya) Cazalas Tree Traversals - Inorder  Inorder Traversal  the root is visited between the left and right subtrees  For the following example, the “visiting” of a node is represented by printing that node  Code for Inorder Traversal: public void inorder (intBSTnode p) { if (p != NULL) { inorder(p.left); System.out.print(p.data + “ ”); inorder(p.right); } }
  • 21. Binary Trees page 21 © Dr. Jonathan (Yahya) Cazalas Tree Traversals - Inorder a b c b a c  Inorder Traversal – Example 1  the root is visited between the subtrees
  • 22. Binary Trees page 22 © Dr. Jonathan (Yahya) Cazalas Tree Traversals - Inorder a b c d e f g h i j g d h b e i a f j c  Inorder Traversal – Example 2 Order of Visiting Nodes:
  • 23. Binary Trees page 23 © Dr. Jonathan (Yahya) Cazalas Tree Traversals – Postorder  Postorder Traversal  the root is visited after both the left and right subtrees  For the following example, the “visiting” of a node is represented by printing that node  Code for Postorder Traversal: public void postorder (intBSTnode p) { if (p != NULL) { postorder(p.left); postorder(p.right); System.out.print(p.data + “ ”); } }
  • 24. Binary Trees page 24 © Dr. Jonathan (Yahya) Cazalas Tree Traversals – Postorder a b c b c a  Postorder Traversal – Example 1  the root is visited after both subtrees
  • 25. Binary Trees page 25 © Dr. Jonathan (Yahya) Cazalas Tree Traversals – Postorder a b c d e f g h i j g h d i e b j f c a  Postorder Traversal – Example 2 Order of Visiting Nodes:
  • 26. Binary Trees page 26 © Dr. Jonathan (Yahya) Cazalas Tree Traversals  Final Traversal Example
  • 27. Binary Trees page 27 © Dr. Jonathan (Yahya) Cazalas Bike Fail
  • 28. Binary Trees page 28 © Dr. Jonathan (Yahya) Cazalas Breadth-First Traversal  A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away  For example, after searching A, then B, then C, the search proceeds with D, E, F, G  Node are explored in the order A B C D E F G H I J K L M N O P Q  J will be found before N L M N O P G Q H J I K F E D B C A
  • 29. Binary Trees page 29 © Dr. Jonathan (Yahya) Cazalas Breadth-First Traversal H D B A C E G I K M O N L J F O M K I G E C A N J F B L D H
  • 30. Binary Trees page 30 © Dr. Jonathan (Yahya) Cazalas Breadth-First Traversal  Coding the Breadth-First Traversal  Let’s say you want to Traverse and Print all nodes?  Think about it, how would you make this happen?  SOLUTION: 1) Enqueue the root node. 2) while (more nodes still in queue) { Dequeue node at front (of queue) Print this node (that we just dequeued) Enqueue its children (if applicable): left then right …continue till no more nodes in queue }
  • 31. Binary Trees page 31 © Dr. Jonathan (Yahya) Cazalas Binary Search Tree  Binary Search Trees  We’ve seen how to traverse binary trees  But it is not quite clear how this data structure helps us  What is the purpose of binary trees?  What if we added a restriction…  Consider the following binary tree:  What pattern can you see?
  • 32. Binary Trees page 32 © Dr. Jonathan (Yahya) Cazalas Binary Search Tree  Binary Search Trees  What pattern can you see?  For each node N, all the values stored in the left subtree of N are LESS than the value stored in N.  Also, all the values stored in the right subtree of N are GREATER than the value stored in N.  Why might this property be a desireable one?  Searching for a node is super fast!  Normally, if we search through n nodes, it takes O(n) time  But notice what is going on here:  This ordering property of the tree tells us where to search  We choose to look to the left OR look to the right of a node  We are HALVING the search space …O(log n) time
  • 33. Binary Trees page 33 © Dr. Jonathan (Yahya) Cazalas Binary Search Tree  Binary Search Trees  Details:  ALL of the data values in the left subtree of each node are smaller than the data value in the node itself (root of said subtree)  Stated another way, the value of the node itself is larger than the value of every node in its left subtree.  ALL of the data values in the right subtree of each node are larger than the data value in the node itself (root of the subtree)  Stated another way, the value of the node itself is smaller than the value of every node in its right subtree.  Both the left and right subtrees, of any given node, are themselves binary search trees.
  • 34. Binary Trees page 34 © Dr. Jonathan (Yahya) Cazalas 44 24 20 8 19 28 15 30 42 36 40 65 56 62 64 58 88 A Binary Search Tree Binary Search Tree
  • 35. Binary Trees page 35 © Dr. Jonathan (Yahya) Cazalas Binary Search Tree  Binary Search Trees  Details:  A binary search tree, commonly referred to as a BST, is extremely useful for efficient searching  Basically, a BST amounts to embedding the binary search into the data structure itself.  Notice how the root of every subtree in the BST on the previous page is the root of a BST.  This ordering of nodes in the tree means that insertions into a BST are not placed arbitrarily  Rather, there is a specific way to insert  …and that is for next time
  • 36. Binary Trees page 36 © Dr. Jonathan (Yahya) Cazalas Binary Trees WASN’T THAT HISTORIC!
  • 37. Binary Trees page 37 © Dr. Jonathan (Yahya) Cazalas Daily Demotivator
  • 38. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Binary Trees
  • 39. Binary Trees page 39 © Dr. Jonathan (Yahya) Cazalas Binary Tree Traversals – Practice Problems 3 54 71 11 56 15 36 7 26 14 33 22 19 87 8 13 9 75 28 10 63 69 59 68 44 Practice Tree #1 Solutions on page 33
  • 40. Binary Trees page 40 © Dr. Jonathan (Yahya) Cazalas Binary Tree Traversals – Practice Problems 3 54 71 11 56 15 36 7 26 14 33 22 19 87 8 13 9 75 28 10 63 69 59 68 44 Practice Tree #2 Solutions on Page 34
  • 41. Binary Trees page 41 © Dr. Jonathan (Yahya) Cazalas  Preorder Traversal: 3, 13, 22, 19, 26, 54, 71, 33, 14, 11, 87, 8, 56, 9, 75, 28, 15, 10, 63, 36, 7, 69, 59, 68, 44  Inorder Traversal: 54, 26, 71, 19, 22, 11, 14, 33, 8, 87, 56, 13, 9, 75, 3, 63, 10, 15, 28, 59, 69, 68, 7, 36, 44  Postorder Traversal: 54, 71, 26, 19, 11, 14, 8, 56, 87, 33, 22, 75, 9, 13, 63, 10, 15, 59, 68, 69, 7, 44, 36, 28, 3 Practice Problem Solutions – Tree #1
  • 42. Binary Trees page 42 © Dr. Jonathan (Yahya) Cazalas  Preorder Traversal: 3, 28, 36, 44, 7, 69, 68, 59, 15, 10, 63, 13, 9, 75, 22, 33, 87, 56, 8, 14, 11, 19, 26, 71, 54  Inorder Traversal: 44, 36, 7, 68, 69, 59, 28, 15, 10, 63, 3, 75, 9, 13, 56, 87, 8, 33, 14, 11, 22, 19, 71, 26, 54  Postorder Traversal: 44, 68, 59, 69, 7, 36, 63, 10, 15, 28, 75, 9, 56, 8, 87, 11, 14, 33, 71, 54, 26, 19, 22, 13, 3 Practice Problem Solutions – Tree #2