Vous êtes sur la page 1sur 28

Trees

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

The depth of a binary tree is


l the depth of its deepest node
This tree has depth 4

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

How many nodes? Total number of nodes


Level 0 : 1 node ( height 1) n = 2h 1 ( maximum)
Level 1: 2 nodes ( height 2) h = log ( n+1)
Level 3 : 4 nodes (height 3)
Level 3: 8 nodes (height 4)
15
Balance
a a
b c b
c e
d e f g
d f
h i j
A balanced binary tree g h
i j
An unbalanced binary tree

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
16
Recursive definition of a binary
tree:
A binary tree is either
- Empty, or
- A node (called root) together with two binary
trees (called left subtree and the right subtree of
the root)

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

With inorder traversal the order is left-child, root node,


right-child

With preorder traversal the order is root node, left


child , right child.

With postorder traversal the order is left child, right


child, root node.
20
Binary tree traversals
A tree will typically have
more than 3 nodes.
Instead of
nodes n2 and n3 there
would be subtrees as
shown :

With inorder traversal the order is left subtree, then the


root and finally the right subtree. Thus the root is visited
in-between visiting the left and right subtrees.
With preorder traversal the root node is visited first,
then the nodes in the left subtree are visited followed by
the nodes in the right subtrees
With postorder traversal the root is visited after both the
subtrees have been visited.(left subtree followed by right
subtree. 21
Binary tree traversals:PreOrder
In a preorder traversal, we first visit the root node.

If there is a left child we visit the left subtree (all the


nodes) in pre-order fashion starting with that left child .

If there is a right child then we visit the right subtree in


pre-order fashion starting with that right child.

22
Binary tree traversals:PreOrder
/* traverse a binary search tree in a LDR (Left-Data-Right) fashion */

void inorder ( struct btreenode *sr )


{
if ( sr != NULL )
{
inorder ( sr -> leftchild ) ;

/* print the data of the node whose leftchild is NULL or the path
has already been traversed */
printf ( "\t%d", sr -> data ) ;

inorder ( sr -> rightchild ) ;


}
else
return ;

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 */

void inorder ( struct btreenode *sr )


{
if ( sr != NULL )
{
inorder ( sr -> leftchild ) ;

/* print the data of the node whose leftchild is NULL or the path
has already been traversed */

printf ( "\t%d", sr -> data ) ;


Inorder:
inorder ( sr -> rightchild ) ; bafdgce
}
else
return ;
}
24
Binary tree traversals: Postorder traversal
/* traverse a binary search tree in LRD (Left-
Right-Data) fashion */
void postorder ( struct btreenode *sr )
{
if ( sr != NULL )
{
postorder ( sr -> leftchild ) ;
postorder ( sr -> rightchild ) ;
printf ( "\t%d", sr -> data ) ;
}
else
return ; Inorder:
} bafdgce

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.

int sum(struct tree_node *p)


{
if ( p!= NULL)
return(p->data + sum(p->left_child)+ sum(p->right_child));
else
return 0;
}

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 ) ;
}

printf ( "\nIn-order Traversal: " ) ;


inorder ( bt ) ;

printf ( "\nPre-order Traversal: " ) ;


preorder ( bt ) ;

printf ( "\nPost-order Traversal: " ) ;


postorder ( bt ) ;
}
28

Vous aimerez peut-être aussi