Académique Documents
Professionnel Documents
Culture Documents
Trees
Trees
Root
store elements
hierarchically
The top node in the tree is
called the root and all
other nodes branch off
from this one.
except the root, each
element has a parent
each element has 0 or
more children
4
Trees
Terminology
siblings: two nodes that have the same parent are called
siblings
internal nodes
nodes that have children
external nodes or leaves
nodes that dont have children
Ancestors
descendants
5
Trees
6
Trees
7
Trees
8
Application of trees
Applications of trees
class hierarchy in Java
file system
storing hierarchies in organizations
9
a binary tree
Definition:
A binary tree is a tree in which each node can have
maximum two children. Thus each node can have no
child, one child or two children.
10
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
11
Examples of binary trees
:
a a
b c b c
d e f
g h i j k
12
Size and depth
The size of a binary tree is the
a number of nodes in it
This tree has size 12
b c
The depth of a node is its
distance from the root
d e f
a is at depth zero
e is at depth 2
g h i j k
13
Size and depth
a Level 0
b c Level 1
d e f Level 2
g h i j k Level 3
Level 4
l
14
Full Binary Tree
45
24 76
14 32 87
61
8 37 37 81 94
27 94
56
17
Recursive definition of a binary
tree:
Each node of a binary tree has both left and right
subtrees which can be reached with pointers:
struct tree_node{
int data;
struct tree_node *left_child;
struct tree_node *right_child;
};
18
Binary tree traversals
Linked lists are traversed sequentially from first
node to the last node. However, there is no such
natural linear order for the nodes of a tree.
Different orderings are possible for traversing a
binary tree. Every node in the tree is a root for the
subtree that it points to.
There are three common traversals for binary
trees:
pre-order: root left right
post-order: left right root
in-order: left root right
19
Binary tree traversals
In order : n2 n1 n3
Pre-order : n1 n2 n3
Post order : n2 n3 n1
22
Binary tree traversals:PreOrder
/* traverse a binary search tree in a LDR (Left-Data-Right) fashion */
/* print the data of the node whose leftchild is NULL or the path
has already been traversed */
printf ( "\t%d", sr -> data ) ;
Preorder Traversal : a b c d f g e 23
Binary tree traversals: Inorder traversal
/* traverse a binary search tree in a LDR (Left-Data-Right)
fashion */
/* print the data of the node whose leftchild is NULL or the path
has already been traversed */
25
Finding sum of values of all the nodes of a tree
To find the sum, add to the value of the current node, the sum of values
of all nodes of left subtree and the sum of values of all nodes in right
subtree.
26
Inserting Elements
/* inserts a new node in a binary search tree
*/ else /* search the node to which new node
void insert ( struct btreenode **sr, int num ) will be attached */
{ {
if ( *sr == NULL ) /* if new data is less, traverse to left */
{ if ( num < ( *sr ) -> data )
*sr = malloc ( sizeof ( struct btreenode ) ) ; insert ( &( ( *sr ) -> leftchild ), num ) ;
else
( *sr ) -> leftchild = NULL ; /* else traverse to right */
( *sr ) -> data = num ; insert ( &( ( *sr ) -> rightchild ), num )
( *sr ) -> rightchild = NULL ; ;
return ; }
}
return ;
}
27
Main Function of B Tree
void main( )
{
struct btreenode *bt ;
int req, i = 1, num ;
bt = NULL ; /* empty tree */
printf ( "Specify the number of items to be inserted: " ) ;
scanf ( "%d", &req ) ;
while ( i++ <= req )
{
printf ( "Enter the data: " ) ;
scanf ( "%d", &num ) ;
insert ( &bt, num ) ;
}