DATA STRUCTURE
Chapter 8: Tree & binary tree
Prepared & Presented by
Mr. Mahmoud R. Alfarra
2011-2012
College of Science & Technology
Dep. Of Computer Science & IT
BCs of Information Technology
http://mfarra.cst.ps
Out Line
 What we mean by a tree?
 Parts of tree
 Binary tree
 Binary search tree
 Building a Binary Search Tree
2
What we mean by a tree?
3
 Trees are a very common data structure in
computer science.
 A tree is a nonlinear data structure that is used
to store data in a hierarchical manner.
What we mean by a tree?
4
 A tree is a set of nodes connected by edges.
 Each box is a node and the lines connecting
the boxes are the edges.
 The nodes, obviously,
represent the entities
(people) that make up an
organization.
 The edges represent the
relationship between the
entities.
Parts of tree
5
Parts of tree
6
 The top node of a tree is called the root node.
 If a node is connected to other nodes below it,
the top node is called the parent, and the
nodes below it are called the parent’s
children.
 A node without any child node is called a leaf.
 The series of edges you follow to get from one
node to another is called a path.
 Visiting all the nodes in a tree in some
particular order is known as a tree
transversal.
Parts of tree
7
 A tree can be broken down into levels.
 A node at any level is considered the root of a
subtree, which consists of that root node’s
children, its children’s children, and so on.
 We can define the depth of a tree as the
number of layers in the tree.
 Finally, each node in a tree has a value. This
value is sometimes referred to as the key
value.
Binary tree
8
 A binary tree is defined as a tree where each
node can have no more than two children.
 By limiting the number of children to 2, we can
write efficient programs for inserting data,
deleting data, and searching for data in a
binary tree.
 The child nodes of a parent node are referred
to as the left node and the right node.
5
10
45
2 25 30
Binary search tree
9
 The binary search tree is a binary tree where
data with lesser values are stored in left
nodes and values with greater values are
stored in right nodes.
 This property provides for very efficient
searches.
Binary search tree
10
Binary
search trees
Not a binary
search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Building a Binary Search Tree
11
 A binary search tree is made up of nodes, so
we need a Node class that is similar to the
Node class we used in the linked list
implementation.
Building a Binary Search Tree
12
 Next we’re ready to build a BinarySearchTree
(BST) class.
 The class consists of just one data member- a
Node object that represents the root node of
the BST.
Building a Binary
Search Tree
Thank You …
14
Remember that: question is the key of knowledge
Ahl Eljanna 

‫ف‬ِ ‫نا‬‫ن‬‫ن‬‫ن‬َ‫ن‬ َ ‫ف‬ِ‫ن‬‫ن‬‫ن‬‫ن‬‫ن‬ََِ
َّ‫ف‬َ
َ‫نا‬‫ن‬‫ن‬‫ن‬َ
‫ب‬َ
‫فر‬َ
‫نام‬‫ن‬‫ن‬‫ن‬َ
َ‫ف‬‫ا‬
َ‫ن‬‫ن‬‫ن‬‫ن‬َ
‫م‬ِ‫ل‬َ
‫و‬
)::(
‫ف‬
‫ن‬ََِ
َّ‫ف‬ِ
َ َِ‫ف‬‫ن‬ِ
‫َآ‬ِّ‫ن‬‫ن‬‫ن‬‫ن‬ِ‫ب‬َ‫ف‬
‫ناف‬‫ن‬‫ن‬‫ن‬َ
‫م‬ُ
‫ك‬
‫ف‬ِ
َ
‫َب‬‫ن‬
ِ
‫ذ‬َ
‫ك‬ُ‫ت‬
)::(
‫ف‬ ‫نا‬َ‫ا‬
‫ن‬‫ف‬َ‫أ‬‫ىف‬َ‫ت‬‫ا‬َ
‫و‬َ‫ذ‬
)::(
‫ف‬
َ‫ف‬
‫ف‬ُ
‫ك‬‫ن‬ََِ
َّ‫ف‬ِ
َ َِ‫ف‬‫ن‬ِ
‫َآ‬ِّ‫ن‬ِ‫ب‬
‫ف‬ِ
َ
‫َب‬‫ن‬
ِ
‫نذ‬َ
‫ك‬ُ‫ت‬‫ناف‬َ
‫م‬
)::(
‫ف‬ِ
َِ‫ن‬‫ن‬‫ا‬َ
ِ‫ف‬ِ ‫نا‬‫ن‬َ‫ا‬
‫ن‬‫ي‬َ
‫نافي‬‫ن‬َ
‫م‬ِ
‫ه‬‫ي‬ِ‫ف‬
)::(
‫ف‬
َ‫ف‬
‫ن‬‫ن‬َ
‫م‬ُ
‫ك‬‫ن‬ََِ
َّ‫ف‬ِ
َ َِ‫ف‬‫ن‬ِ
‫َآ‬ِّ‫ن‬‫ن‬ِ‫ب‬
‫ف‬ِ
َ
‫َب‬‫ن‬
ِ
‫نذ‬‫ن‬َ
‫ك‬ُ‫ت‬‫اف‬
)::(
‫ن‬‫ن‬َ ‫ا‬
‫و‬َ‫ف‬ ‫ن‬‫ن‬َ
‫ه‬ِِّ‫ا‬َ‫ف‬‫ف‬‫ن‬ِ
‫نا‬‫ن‬ُ
ِّ‫ف‬‫ا‬
َ‫ن‬‫ن‬ِ
‫نافر‬‫ن‬َ
‫م‬ِ
‫ه‬‫ي‬ِ‫ف‬
‫ف‬ِ ‫ا‬
)::(
‫ف‬
‫ن‬ََِ
َّ‫ف‬ِ
َ َِ‫ف‬‫ن‬ِ
‫َآ‬ِّ‫ن‬‫ن‬ِ‫ب‬َ‫ف‬
‫ناف‬‫ن‬َ
‫م‬ُ
‫ك‬
‫ف‬ِ
َ
‫َب‬‫ن‬
ِ
‫نذ‬‫ن‬‫ن‬َ
‫ك‬ُ‫ت‬
)::(
‫ن‬‫ن‬‫ن‬ُ
‫ن‬‫ف‬‫نىف‬‫ن‬‫ن‬ََ
‫في‬َ
‫نع‬‫ن‬‫ن‬ِ‫ئ‬ِ
‫ك‬ ُ
‫ر‬
‫ن‬‫ن‬‫ن‬‫ا‬ ِ‫ف‬‫ا‬
َ‫ن‬‫ن‬‫ن‬ِ
‫نافر‬‫ن‬‫ن‬َ
‫ه‬ُ
‫ن‬ ِ‫ائ‬َ‫ط‬ََ‫ف‬‫ش‬ُ
‫ف‬‫ق‬َ‫ا‬
‫ن‬‫ب‬َ
‫ن‬
‫ف‬ ‫ا‬َ
‫فد‬ِ‫ا‬
‫ع‬َ
‫ن‬ ‫ن‬َ‫ا‬
‫ْل‬‫فا‬ َ
‫َن‬َ َ
‫و‬
)::(
15

Chapter 8: tree data structure

  • 1.
    DATA STRUCTURE Chapter 8:Tree & binary tree Prepared & Presented by Mr. Mahmoud R. Alfarra 2011-2012 College of Science & Technology Dep. Of Computer Science & IT BCs of Information Technology http://mfarra.cst.ps
  • 2.
    Out Line  Whatwe mean by a tree?  Parts of tree  Binary tree  Binary search tree  Building a Binary Search Tree 2
  • 3.
    What we meanby a tree? 3  Trees are a very common data structure in computer science.  A tree is a nonlinear data structure that is used to store data in a hierarchical manner.
  • 4.
    What we meanby a tree? 4  A tree is a set of nodes connected by edges.  Each box is a node and the lines connecting the boxes are the edges.  The nodes, obviously, represent the entities (people) that make up an organization.  The edges represent the relationship between the entities.
  • 5.
  • 6.
    Parts of tree 6 The top node of a tree is called the root node.  If a node is connected to other nodes below it, the top node is called the parent, and the nodes below it are called the parent’s children.  A node without any child node is called a leaf.  The series of edges you follow to get from one node to another is called a path.  Visiting all the nodes in a tree in some particular order is known as a tree transversal.
  • 7.
    Parts of tree 7 A tree can be broken down into levels.  A node at any level is considered the root of a subtree, which consists of that root node’s children, its children’s children, and so on.  We can define the depth of a tree as the number of layers in the tree.  Finally, each node in a tree has a value. This value is sometimes referred to as the key value.
  • 8.
    Binary tree 8  Abinary tree is defined as a tree where each node can have no more than two children.  By limiting the number of children to 2, we can write efficient programs for inserting data, deleting data, and searching for data in a binary tree.  The child nodes of a parent node are referred to as the left node and the right node. 5 10 45 2 25 30
  • 9.
    Binary search tree 9 The binary search tree is a binary tree where data with lesser values are stored in left nodes and values with greater values are stored in right nodes.  This property provides for very efficient searches.
  • 10.
    Binary search tree 10 Binary searchtrees Not a binary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45
  • 11.
    Building a BinarySearch Tree 11  A binary search tree is made up of nodes, so we need a Node class that is similar to the Node class we used in the linked list implementation.
  • 12.
    Building a BinarySearch Tree 12  Next we’re ready to build a BinarySearchTree (BST) class.  The class consists of just one data member- a Node object that represents the root node of the BST.
  • 13.
  • 14.
    Thank You … 14 Rememberthat: question is the key of knowledge
  • 15.
    Ahl Eljanna   ‫ف‬ِ‫نا‬‫ن‬‫ن‬‫ن‬َ‫ن‬ َ ‫ف‬ِ‫ن‬‫ن‬‫ن‬‫ن‬‫ن‬ََِ َّ‫ف‬َ َ‫نا‬‫ن‬‫ن‬‫ن‬َ ‫ب‬َ ‫فر‬َ ‫نام‬‫ن‬‫ن‬‫ن‬َ َ‫ف‬‫ا‬ َ‫ن‬‫ن‬‫ن‬‫ن‬َ ‫م‬ِ‫ل‬َ ‫و‬ )::( ‫ف‬ ‫ن‬ََِ َّ‫ف‬ِ َ َِ‫ف‬‫ن‬ِ ‫َآ‬ِّ‫ن‬‫ن‬‫ن‬‫ن‬ِ‫ب‬َ‫ف‬ ‫ناف‬‫ن‬‫ن‬‫ن‬َ ‫م‬ُ ‫ك‬ ‫ف‬ِ َ ‫َب‬‫ن‬ ِ ‫ذ‬َ ‫ك‬ُ‫ت‬ )::( ‫ف‬ ‫نا‬َ‫ا‬ ‫ن‬‫ف‬َ‫أ‬‫ىف‬َ‫ت‬‫ا‬َ ‫و‬َ‫ذ‬ )::( ‫ف‬ َ‫ف‬ ‫ف‬ُ ‫ك‬‫ن‬ََِ َّ‫ف‬ِ َ َِ‫ف‬‫ن‬ِ ‫َآ‬ِّ‫ن‬ِ‫ب‬ ‫ف‬ِ َ ‫َب‬‫ن‬ ِ ‫نذ‬َ ‫ك‬ُ‫ت‬‫ناف‬َ ‫م‬ )::( ‫ف‬ِ َِ‫ن‬‫ن‬‫ا‬َ ِ‫ف‬ِ ‫نا‬‫ن‬َ‫ا‬ ‫ن‬‫ي‬َ ‫نافي‬‫ن‬َ ‫م‬ِ ‫ه‬‫ي‬ِ‫ف‬ )::( ‫ف‬ َ‫ف‬ ‫ن‬‫ن‬َ ‫م‬ُ ‫ك‬‫ن‬ََِ َّ‫ف‬ِ َ َِ‫ف‬‫ن‬ِ ‫َآ‬ِّ‫ن‬‫ن‬ِ‫ب‬ ‫ف‬ِ َ ‫َب‬‫ن‬ ِ ‫نذ‬‫ن‬َ ‫ك‬ُ‫ت‬‫اف‬ )::( ‫ن‬‫ن‬َ ‫ا‬ ‫و‬َ‫ف‬ ‫ن‬‫ن‬َ ‫ه‬ِِّ‫ا‬َ‫ف‬‫ف‬‫ن‬ِ ‫نا‬‫ن‬ُ ِّ‫ف‬‫ا‬ َ‫ن‬‫ن‬ِ ‫نافر‬‫ن‬َ ‫م‬ِ ‫ه‬‫ي‬ِ‫ف‬ ‫ف‬ِ ‫ا‬ )::( ‫ف‬ ‫ن‬ََِ َّ‫ف‬ِ َ َِ‫ف‬‫ن‬ِ ‫َآ‬ِّ‫ن‬‫ن‬ِ‫ب‬َ‫ف‬ ‫ناف‬‫ن‬َ ‫م‬ُ ‫ك‬ ‫ف‬ِ َ ‫َب‬‫ن‬ ِ ‫نذ‬‫ن‬‫ن‬َ ‫ك‬ُ‫ت‬ )::( ‫ن‬‫ن‬‫ن‬ُ ‫ن‬‫ف‬‫نىف‬‫ن‬‫ن‬ََ ‫في‬َ ‫نع‬‫ن‬‫ن‬ِ‫ئ‬ِ ‫ك‬ ُ ‫ر‬ ‫ن‬‫ن‬‫ن‬‫ا‬ ِ‫ف‬‫ا‬ َ‫ن‬‫ن‬‫ن‬ِ ‫نافر‬‫ن‬‫ن‬َ ‫ه‬ُ ‫ن‬ ِ‫ائ‬َ‫ط‬ََ‫ف‬‫ش‬ُ ‫ف‬‫ق‬َ‫ا‬ ‫ن‬‫ب‬َ ‫ن‬ ‫ف‬ ‫ا‬َ ‫فد‬ِ‫ا‬ ‫ع‬َ ‫ن‬ ‫ن‬َ‫ا‬ ‫ْل‬‫فا‬ َ ‫َن‬َ َ ‫و‬ )::( 15