SlideShare una empresa de Scribd logo
1 de 14
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Red black tree
Red black treeRed black tree
Red black tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
B tree
B treeB tree
B tree
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
Tree
TreeTree
Tree
 
Queue
QueueQueue
Queue
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 

Destacado

Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.ppt
Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.pptΟ ρόλος της γυναίκας στην ιστορία 1ο μέρος.ppt
Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.pptmesagrosgym
 
Ο ρόλος της γυναίκας στην ιστορία 2ο μέρος
Ο ρόλος της γυναίκας στην ιστορία 2ο μέροςΟ ρόλος της γυναίκας στην ιστορία 2ο μέρος
Ο ρόλος της γυναίκας στην ιστορία 2ο μέροςmesagrosgym
 
Altar de Muertos
Altar de Muertos Altar de Muertos
Altar de Muertos Erika Said
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search treeMayeesha Samiha
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Treeleminhvuong
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentationHafsa.Naseem
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary treeZaid Shabbir
 

Destacado (12)

Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.ppt
Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.pptΟ ρόλος της γυναίκας στην ιστορία 1ο μέρος.ppt
Ο ρόλος της γυναίκας στην ιστορία 1ο μέρος.ppt
 
Ο ρόλος της γυναίκας στην ιστορία 2ο μέρος
Ο ρόλος της γυναίκας στην ιστορία 2ο μέροςΟ ρόλος της γυναίκας στην ιστορία 2ο μέρος
Ο ρόλος της γυναίκας στην ιστορία 2ο μέρος
 
Altar de Muertos
Altar de Muertos Altar de Muertos
Altar de Muertos
 
Cse Binary tree presentation
Cse Binary tree presentationCse Binary tree presentation
Cse Binary tree presentation
 
binary tree
binary treebinary tree
binary tree
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
6. binary tree
6. binary tree6. binary tree
6. binary tree
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 

Similar a Binary trees (20)

Binary Search Tree Traversal.ppt
Binary Search Tree Traversal.pptBinary Search Tree Traversal.ppt
Binary Search Tree Traversal.ppt
 
Binary Trees.ppt
Binary Trees.pptBinary Trees.ppt
Binary Trees.ppt
 
09 binary-trees
09 binary-trees09 binary-trees
09 binary-trees
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Binary trees
Binary treesBinary trees
Binary trees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Trees
TreesTrees
Trees
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Tree
TreeTree
Tree
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
chapter5.PPT
chapter5.PPTchapter5.PPT
chapter5.PPT
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 

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