SlideShare a Scribd company logo
Introduction to Algorithms and
Data Structures
Lecture 12 - “I think that I shall never
see.. a data structure lovely as a”
Binary Tree
What is a Binary Tree
• A binary tree is a a collection of nodes that
consists of the root and other subsets to the root
points, which are called the left and right subtrees.
• Every parent node on a binary tree can have up to
two child nodes (roots of the two subtrees); any
more children and it becomes a general tree.
• A node that has no children is called a leaf node.
A Few Terms Regarding Binary Trees
A
E F
C
B
G
D
A is the root
B and C are A’s children
A is B’s parent
B & C are the root of two subtrees
D, E, and G are leaves
This is NOT A Binary Tree
A
E F
C
B
G
D H
This is a general tree because C has three child nodes
This is NOT A Binary Tree
A
E F
C
B
G
D
This is a graph
because A, B, E, H, F and C
form a circuit
H
Tree Traversal
• There are three common ways to traverse a tree:
– Preorder: Visit the root, traverse the left subtree
(preorder) and then traverse the right subtree (preorder)
– Postorder: Traverse the left subtree (postorder),
traverse the right subtree (postorder) and then visit the
root.
– Inorder: Traverse the left subtree (in order), visit the
root and the traverse the right subtree (in order).
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: ABDECFG
Postorder: DEBGFCA
In Order: DBEAFGC
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: A
(visit each node as your reach it)
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: AB
(visit each node as your reach it)
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: ABD
(visit each node as your reach it)
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: ABDE
(visit each node as your reach it)
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: ABDEC
(visit each node as your reach it)
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: ABDECF
(visit each node as your reach it)
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: ABDECFG
(visit each node as your reach it)
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder:
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder:
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: D
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DE
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DEB
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DEB
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DEB
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DEBG
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DEBGF
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DEBGFC
Tree Traversals: An Example
A
E F
C
B
G
D
Postorder: DEBGFCA
Tree Traversals: An Example
A
E F
C
B
G
D
In Order:
Tree Traversals: An Example
A
E F
C
B
G
D
In Order:
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: D
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: DB
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: DBE
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: DBEA
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: DBEA
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: DBEAF
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: DBEAFG
Tree Traversals: An Example
A
E F
C
B
G
D
In Order: DBEAFGC
Tree Traversals: An Example
A
E F
C
B
G
D
Preorder: ABDECFG
Postorder: DEBGFCA
In Order: DBEAFGC
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
In Order: DHFIBEACG
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: A
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: AB
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABD
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDF
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDFH
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDFHI
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDFHIE
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDFHIEC
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDFHIECG
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder:
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder:
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder:
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder:
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: H
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HI
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIF
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIFD
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIFDE
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIFDEB
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIFDEB
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIFDEBG
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIFDEBGC
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Postorder: HIFDEBGCA
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order:
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order:
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: D
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: D
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DH
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DHF
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DHFI
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DHFIB
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DHFIBE
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DHFIBEA
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DHFIBEAC
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
In Order: DHFIBEACG
G
H
Tree Traversals: Another Example
A
E
F
C
B
I
D
Preorder: ABDFHIECG
Postorder: HIFDEBGCA
In Order: DHFIBEACG
G
H
Basic Implementation of a Binary
Tree
• We can implement a binary in essentially
the same way as a linked list, except that
there are two nodes that comes next:
public class Node {
private int data;
private Node left;
private Node right;
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setData(int x) {
data = x;
}
public void setLeft(Node p) {
left = p;
}
public void setRight(Node p) {
right = p;
}
}
The tree Class
public class Tree {
private Node root;
// tree() - The default constructor – Starts
// the tree as empty
public Tree() {
root = null;
}
// Tree() - An initializing constructor that
// creates a node and places in it
// the initial value
public Tree(int x) {
root = new Node();
root.setData(x);
root.setLeft(null);
root.setRight(null);
}
public Node getRoot() {
return root;
}
// newNode() - Creates a new node with a
// zero as data by default
public Node newNode() {
Node p = new Node();
p.setData(0);
p.setLeft(null);
p.setRight(null);
return(p);
}
// newNode() - Creates a new node with the
// parameter x as its value
public Node newNode(int x) {
Node p = new Node();
p.setData(x);
p.setLeft(null);
p.setRight(null);
return(p);
}
//travTree() – initializes recursive
// traversal of tree
public void travTree() {
if (root != null)
travSubtree(root);
System.out.println();
}
//travSubtree() – recursive method used to
// traverse a binary tree (inorder)
public void travSubtree(Node p) {
if (p != null) {
travSubtree(p.getLeft());
System.out.print(p.getData()
+ "t");
travSubtree(p.getRight());
}
}
// addLeft() - Inserts a new node containing
// x as the left child of p
public void addLeft(Node p, int x) {
Node q = newNode(x);
p.setLeft(q);
}
// addRight() - Inserts a new node containing
// x as the right child of p
public void addRight(Node p, int x) {
Node q = newNode(x);
p.setRight(q);
}
}
A Basic Search Tree
• We can construct a simple search tree if we
add new nodes with value x on the tree
using this strategy:
– Every time x is less than the value in the node
we move down to the left.
– Every time x is greater than the value in the
node we move down to the right.
A Sample Search Tree
15
12 18
22
8
20
5 23
16
10
7
// insert() - Insert value x in a new node to
// be inserted after p
public void insert(int x) {
Node p, q;
boolean found = false;
p = root;
q = null;
while (p != null && !found) {
q = p;
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
if (found)
error("Duplicate entry");
if (q.getData() > x)
addLeft(q, x);
else
addRight(q, x);
//q = newNode(x);
}
// isXThere() - Is there a node in the
// tree containing x?
public boolean isXThere(int x) {
Node p;
boolean found = false;
p = root;
while (p != null && !found) {
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
return(found);
}
public void error(String message) {
System.out.println(message);
System.exit(0);
}
// getNode() - Get the pointer for the
// node containing x
public Node getNode(int x) {
Node p, q;
boolean found = false;
p = root;
q = null;
while (p != null && !found) {
q = p;
if (p.getData() == x)
found = true;
else if (p.getData() > x)
p = p.getLeft();
else
p = p.getRight();
}
if (found)
return(q);
else
return(null);
}
public class TestTree {
public static void main(String[] args) {
Tree mytree = new Tree(8);
mytree.addLeft(mytree.getRoot(), 6);
mytree.addRight(mytree.getRoot(), 9);
mytree.insert(4);
mytree.insert(1);
mytree.insert(12);
if (mytree.isXThere(13))
System.out.println("great");
else
System.out.println("not great, Bob");
mytree.travTree();
}
}
Tracing TestTree
8
Tracing TestTree
8
6
Tracing TestTree
8
9
6
Tracing TestTree
8
9
6
4
Tracing TestTree
8
9
6
4
1
Tracing TestTree
8
9
6
4 12
1

More Related Content

Similar to binary tree power point presentation for iT (20)

PPT
DATA STRUCTURES
bca2010
 
PPT
bca data structure
shini
 
PDF
Make money fast! department of computer science-copypasteads.com
jackpot201
 
PDF
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
PPT
computer notes - Data Structures - 13
ecomputernotes
 
DOCX
Trees in data structrures
Gaurav Sharma
 
DOCX
#include iostream using namespace std; const int nil = 0; cl.docx
ajoy21
 
PPTX
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
PPT
Tree and Binary Search tree
Muhazzab Chouhadry
 
PPT
Trees
Avila Rebello
 
PDF
A perfect left-sided binary tree is a binary tree where every intern.pdf
michardsonkhaicarr37
 
PPTX
Trees
Syed Zaid Irshad
 
PPTX
Data Structures
Nitesh Bichwani
 
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
asimshahzad8611
 
PDF
Extend your work on the OCC logo to add shapes that curve1 the blue.pdf
arihantcommunication
 
PPT
Lec21
Nikhil Chilwant
 
PPT
Introduction To Binary Search Trees .ppt
mixedbangladesh
 
PDF
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
PPTX
Lecture 6 tree traversal
Abirami A
 
DATA STRUCTURES
bca2010
 
bca data structure
shini
 
Make money fast! department of computer science-copypasteads.com
jackpot201
 
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
computer notes - Data Structures - 13
ecomputernotes
 
Trees in data structrures
Gaurav Sharma
 
#include iostream using namespace std; const int nil = 0; cl.docx
ajoy21
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Tree and Binary Search tree
Muhazzab Chouhadry
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
michardsonkhaicarr37
 
Data Structures
Nitesh Bichwani
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
asimshahzad8611
 
Extend your work on the OCC logo to add shapes that curve1 the blue.pdf
arihantcommunication
 
Introduction To Binary Search Trees .ppt
mixedbangladesh
 
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
Lecture 6 tree traversal
Abirami A
 

More from DanilynSukkie (20)

PPTX
Behaviorism AND OTHER THEORIES OF LEARNING.pptx
DanilynSukkie
 
PPT
INTRODUCTION TO DATA STRUCTURES LECTURE!
DanilynSukkie
 
PPTX
Demo_Business English 2020 for english tutor.pptx
DanilynSukkie
 
PPTX
Learner-Centered Psychological Principles-ted03.pptx
DanilynSukkie
 
PPT
DATA STRUCTURES IN INFORMATION TECHNOLOGY
DanilynSukkie
 
PPTX
Blue and Gray Illustrative Back to School Class Orientation Education Present...
DanilynSukkie
 
PDF
LESSON EXEMPLAR FILIPINO 7 - WEEK 1 - MATATAG CURRICULUM
DanilynSukkie
 
PPTX
KATUTUBONG PANITIKAN NA PATULA - MATATAG CURRICULUM
DanilynSukkie
 
PPTX
panitikan sa panahon ng katutubo - matatag curriculum
DanilynSukkie
 
PPTX
MGA SALOOBING NANGIBABAW SA AKDANG IBONG ADARNA
DanilynSukkie
 
PPTX
Teachers' Commitment and Motivation in Relation to Learners' Performance
DanilynSukkie
 
PPTX
AWITING BAYAN SA BISAYAS NG PILIPINAS AT
DanilynSukkie
 
PPTX
Operational-planning
DanilynSukkie
 
PPTX
WELCOME BACK TO SCHOOL GRADE 8 – FLAMINGO.pptx
DanilynSukkie
 
PPTX
isang punongkahoy.pptx
DanilynSukkie
 
PPTX
MASS MEDIA, KATOTOHANAN,OPINYON,HINUHA.pptx
DanilynSukkie
 
PPTX
PARTIII-ORGANIZATIONAL-ORGANIZING-AND-STAFFING-LESSON-3.4-3.6.pptx
DanilynSukkie
 
PPT
SubjectVerbAgreement-1 (1).ppt
DanilynSukkie
 
PPTX
3-mans-origin.pptx
DanilynSukkie
 
PPTX
1-his-and-her-story-B.pptx
DanilynSukkie
 
Behaviorism AND OTHER THEORIES OF LEARNING.pptx
DanilynSukkie
 
INTRODUCTION TO DATA STRUCTURES LECTURE!
DanilynSukkie
 
Demo_Business English 2020 for english tutor.pptx
DanilynSukkie
 
Learner-Centered Psychological Principles-ted03.pptx
DanilynSukkie
 
DATA STRUCTURES IN INFORMATION TECHNOLOGY
DanilynSukkie
 
Blue and Gray Illustrative Back to School Class Orientation Education Present...
DanilynSukkie
 
LESSON EXEMPLAR FILIPINO 7 - WEEK 1 - MATATAG CURRICULUM
DanilynSukkie
 
KATUTUBONG PANITIKAN NA PATULA - MATATAG CURRICULUM
DanilynSukkie
 
panitikan sa panahon ng katutubo - matatag curriculum
DanilynSukkie
 
MGA SALOOBING NANGIBABAW SA AKDANG IBONG ADARNA
DanilynSukkie
 
Teachers' Commitment and Motivation in Relation to Learners' Performance
DanilynSukkie
 
AWITING BAYAN SA BISAYAS NG PILIPINAS AT
DanilynSukkie
 
Operational-planning
DanilynSukkie
 
WELCOME BACK TO SCHOOL GRADE 8 – FLAMINGO.pptx
DanilynSukkie
 
isang punongkahoy.pptx
DanilynSukkie
 
MASS MEDIA, KATOTOHANAN,OPINYON,HINUHA.pptx
DanilynSukkie
 
PARTIII-ORGANIZATIONAL-ORGANIZING-AND-STAFFING-LESSON-3.4-3.6.pptx
DanilynSukkie
 
SubjectVerbAgreement-1 (1).ppt
DanilynSukkie
 
3-mans-origin.pptx
DanilynSukkie
 
1-his-and-her-story-B.pptx
DanilynSukkie
 
Ad

Recently uploaded (20)

PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Introduction to Probability(basic) .pptx
purohitanuj034
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Introduction to Probability(basic) .pptx
purohitanuj034
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Ad

binary tree power point presentation for iT