SlideShare a Scribd company logo
Binary Trees
2
Parts of a binary tree
 A binary tree is composed of zero or more nodes
 Each node contains:
 A value (some sort of data item)
 A reference or pointer to a left child (may be null), and
 A reference or pointer to a right child (may be null)
 A binary tree may be empty (contain no nodes)
 If not empty, a binary tree has a root node
 Every node in the binary tree is reachable from the root
node by a unique path
 A node with neither a left child nor a right child is
called a leaf
 In some binary trees, only the leaves contain a value
3
Picture of a binary tree
a
b c
d e
g h i
l
f
j k
4
Size and depth
 The size of a binary tree is the
number of nodes in it
 This tree has size 12
 The depth of a node is its
distance from the root

a is at depth zero

e is at depth 2
 The depth of a binary tree is
the depth of its deepest node
 This tree has depth 4
a
b c
d e f
g h i j k
l
5
Balance
 A binary tree is balanced if every level above the lowest is “full”
(contains 2n
nodes)
 In most applications, a reasonably balanced binary tree is
desirable
a
b c
d e f g
h i j
A balanced binary tree
a
b
c
d
e
f
g h
i j
An unbalanced binary tree
6
Binary search in an array
 Look at array location (lo + hi)/2
2 3 5 7 11 13 17
0 1 2 3 4 5 6
Searching for 5:
(0+6)/2 = 3
hi = 2;
(0 + 2)/2 = 1 lo = 2;
(2+2)/2=2
7
3 13
2 5 11 17
Using a binary
search tree
7
Tree traversals
 A binary tree is defined recursively: it consists of a root, a
left subtree, and a right subtree
 To traverse (or walk) the binary tree is to visit each node in
the binary tree exactly once
 Tree traversals are naturally recursive
 Since a binary tree has three “parts,” there are six possible
ways to traverse the binary tree:
 root, left, right
 left, root, right
 left, right, root
 root, right, left
 right, root, left
 right, left, root
8
Preorder traversal
 In preorder, the root is visited first
 Here’s a preorder traversal to print out all the
elements in the binary tree:
public void preorderPrint(BinaryTree bt) {
if (bt == null) return;
System.out.println(bt.value);
preorderPrint(bt.leftChild);
preorderPrint(bt.rightChild);
}
9
Inorder traversal
 In inorder, the root is visited in the middle
 Here’s an inorder traversal to print out all the
elements in the binary tree:
public void inorderPrint(BinaryTree bt) {
if (bt == null) return;
inorderPrint(bt.leftChild);
System.out.println(bt.value);
inorderPrint(bt.rightChild);
}
10
Postorder traversal
 In postorder, the root is visited last
 Here’s a postorder traversal to print out all the
elements in the binary tree:
public void postorderPrint(BinaryTree bt) {
if (bt == null) return;
postorderPrint(bt.leftChild);
postorderPrint(bt.rightChild);
System.out.println(bt.value);
}
11
Tree traversals using “flags”
 The order in which the nodes are visited during a tree
traversal can be easily determined by imagining there is a
“flag” attached to each node, as follows:
 To traverse the tree, collect the flags:
preorder inorder postorder
A
B C
D E F G
A
B C
D E F G
A
B C
D E F G
A B D E C F G D B E A F C G D E B F G C A
12
Copying a binary tree
 In postorder, the root is visited last
 Here’s a postorder traversal to make a complete
copy of a given binary tree:
public BinaryTree copyTree(BinaryTree bt) {
if (bt == null) return null;
BinaryTree left = copyTree(bt.leftChild);
BinaryTree right = copyTree(bt.rightChild);
return new BinaryTree(bt.value, left, right);
}
13
Other traversals
 The other traversals are the reverse of these three
standard ones
 That is, the right subtree is traversed before the left subtree
is traversed
 Reverse preorder: root, right subtree, left subtree
 Reverse inorder: right subtree, root, left subtree
 Reverse postorder: right subtree, left subtree, root
14
The End

More Related Content

PPTX
trees in data structure
PPTX
Selection sorting
PPTX
Red black trees
PPT
Data Structure and Algorithms Binary Search Tree
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Prims and kruskal algorithms
PPTX
Binary Search Tree in Data Structure
trees in data structure
Selection sorting
Red black trees
Data Structure and Algorithms Binary Search Tree
BINARY TREE REPRESENTATION.ppt
Prims and kruskal algorithms
Binary Search Tree in Data Structure

What's hot (20)

PDF
Red black tree
PPT
Data Structures- Part5 recursion
PPTX
Binary trees1
PPTX
Data structure tries
PPT
1.1 binary tree
PPT
Quick sort Algorithm Discussion And Analysis
PPTX
Binary tree and operations
PPT
17. Trees and Graphs
PPTX
Different types of Linked list.
PPTX
Merge sort algorithm
PPTX
Search tree,Tree and binary tree and heap tree
PPT
Chapter 1( intro & overview)
PPTX
linked list in data structure
PPTX
Balanced Tree (AVL Tree & Red-Black Tree)
PPTX
AVL Tree Data Structure
PDF
sparse matrix in data structure
PPTX
Binary Search Tree
PPT
Spanning trees
PPT
DATA STRUCTURES
PPTX
Quick sort
Red black tree
Data Structures- Part5 recursion
Binary trees1
Data structure tries
1.1 binary tree
Quick sort Algorithm Discussion And Analysis
Binary tree and operations
17. Trees and Graphs
Different types of Linked list.
Merge sort algorithm
Search tree,Tree and binary tree and heap tree
Chapter 1( intro & overview)
linked list in data structure
Balanced Tree (AVL Tree & Red-Black Tree)
AVL Tree Data Structure
sparse matrix in data structure
Binary Search Tree
Spanning trees
DATA STRUCTURES
Quick sort
Ad

Viewers also liked (15)

PPTX
Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.ppt
PPT
Ο ρόλος της γυναίκας στην ιστορία 2ο μέρος
PDF
Binary tree
PPTX
Altar de Muertos
PPT
binary tree
PPTX
Binary tree and Binary search tree
PDF
6. binary tree
PPT
Tree and Binary Search tree
PPT
Ch13 Binary Search Tree
PPTX
Binomial heap presentation
PPT
Trees - Data structures in C/Java
PDF
Binary tree
PPT
Binary tree
PDF
Tree and binary tree
Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.ppt
Ο ρόλος της γυναίκας στην ιστορία 2ο μέρος
Binary tree
Altar de Muertos
binary tree
Binary tree and Binary search tree
6. binary tree
Tree and Binary Search tree
Ch13 Binary Search Tree
Binomial heap presentation
Trees - Data structures in C/Java
Binary tree
Binary tree
Tree and binary tree
Ad

Similar to Binary trees (20)

PPT
Binary Search Tree Traversal.ppt
PPT
Binary Trees.ppt
PPT
09 binary-trees
PDF
Chapter 5_Trees.pdf
PPT
Binary trees
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPTX
Tree structure and its definitions with an example
PPT
BINARY SEARCH TREE
PPTX
PPTX
Trees in data structure
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
BST.pptx this is Good for data structure
PDF
Dsc++ unit 3 notes
PPT
PPT
Trees
PPTX
Unit 6 tree
PPTX
Tree
PPT
Admissions in india 2015
PPT
(Binary tree)
Binary Search Tree Traversal.ppt
Binary Trees.ppt
09 binary-trees
Chapter 5_Trees.pdf
Binary trees
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Tree structure and its definitions with an example
BINARY SEARCH TREE
Trees in data structure
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
BST.pptx this isp used for learning binary search trees
BST.pptx this is Good for data structure
Dsc++ unit 3 notes
Trees
Unit 6 tree
Tree
Admissions in india 2015
(Binary tree)

Binary trees

  • 2. 2 Parts of a binary tree  A binary tree is composed of zero or more nodes  Each node contains:  A value (some sort of data item)  A reference or pointer to a left child (may be null), and  A reference or pointer to a right child (may be null)  A binary tree may be empty (contain no nodes)  If not empty, a binary tree has a root node  Every node in the binary tree is reachable from the root node by a unique path  A node with neither a left child nor a right child is called a leaf  In some binary trees, only the leaves contain a value
  • 3. 3 Picture of a binary tree a b c d e g h i l f j k
  • 4. 4 Size and depth  The size of a binary tree is the number of nodes in it  This tree has size 12  The depth of a node is its distance from the root  a is at depth zero  e is at depth 2  The depth of a binary tree is the depth of its deepest node  This tree has depth 4 a b c d e f g h i j k l
  • 5. 5 Balance  A binary tree is balanced if every level above the lowest is “full” (contains 2n nodes)  In most applications, a reasonably balanced binary tree is desirable a b c d e f g h i j A balanced binary tree a b c d e f g h i j An unbalanced binary tree
  • 6. 6 Binary search in an array  Look at array location (lo + hi)/2 2 3 5 7 11 13 17 0 1 2 3 4 5 6 Searching for 5: (0+6)/2 = 3 hi = 2; (0 + 2)/2 = 1 lo = 2; (2+2)/2=2 7 3 13 2 5 11 17 Using a binary search tree
  • 7. 7 Tree traversals  A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree  To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once  Tree traversals are naturally recursive  Since a binary tree has three “parts,” there are six possible ways to traverse the binary tree:  root, left, right  left, root, right  left, right, root  root, right, left  right, root, left  right, left, root
  • 8. 8 Preorder traversal  In preorder, the root is visited first  Here’s a preorder traversal to print out all the elements in the binary tree: public void preorderPrint(BinaryTree bt) { if (bt == null) return; System.out.println(bt.value); preorderPrint(bt.leftChild); preorderPrint(bt.rightChild); }
  • 9. 9 Inorder traversal  In inorder, the root is visited in the middle  Here’s an inorder traversal to print out all the elements in the binary tree: public void inorderPrint(BinaryTree bt) { if (bt == null) return; inorderPrint(bt.leftChild); System.out.println(bt.value); inorderPrint(bt.rightChild); }
  • 10. 10 Postorder traversal  In postorder, the root is visited last  Here’s a postorder traversal to print out all the elements in the binary tree: public void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild); postorderPrint(bt.rightChild); System.out.println(bt.value); }
  • 11. 11 Tree traversals using “flags”  The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows:  To traverse the tree, collect the flags: preorder inorder postorder A B C D E F G A B C D E F G A B C D E F G A B D E C F G D B E A F C G D E B F G C A
  • 12. 12 Copying a binary tree  In postorder, the root is visited last  Here’s a postorder traversal to make a complete copy of a given binary tree: public BinaryTree copyTree(BinaryTree bt) { if (bt == null) return null; BinaryTree left = copyTree(bt.leftChild); BinaryTree right = copyTree(bt.rightChild); return new BinaryTree(bt.value, left, right); }
  • 13. 13 Other traversals  The other traversals are the reverse of these three standard ones  That is, the right subtree is traversed before the left subtree is traversed  Reverse preorder: root, right subtree, left subtree  Reverse inorder: right subtree, root, left subtree  Reverse postorder: right subtree, left subtree, root