Vous êtes sur la page 1sur 16

Binary Trees

Parts of a binary tree

A binary tree is composed of zero or more nodes


Each node contains:

A binary tree may be empty (contain no nodes)


If not empty, a binary tree has a root node

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)

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


2

Picture of a binary tree


a
b

d
g

e
h

f
i

l
3

Size and depth

The size of a binary tree is the


number of nodes in it

b
d
g

e
h

f
i

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 size 12

This tree has depth 4

The depth of a binary tree is the


depth of its deepest node

Height

a
b

The Height of a node is length


of the longest path from node to
leaf

d
g

e
h

f
i

a has

a height 4
e has height 2

The Height of a binary tree is


Height of its root

This tree has height 4

Balance
a

b
d

c
e

b
c

h i
j
A balanced binary tree

e
f

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
6

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)

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

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:
in-order: left node right
pre-order: node left right
post-order: left right node

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, node,


right-child
With preorder traversal the order is node, left
child , right child
With postorder traversal the order is left child, right
child, node.
10

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
11

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.
void preorder(struct tree_node * p)
{
if (p !=NULL) {
printf(%d\n, p->data);
preorder(p->left_child);
preorder(p->right_child);
}
}

12

Binary tree traversals:PreOrder


void preorder(struct tree_node * p)
{
if (p !=NULL) {
printf(%d\n, p->data);
preorder(p->left_child);
preorder(p->right_child);
}
}
Preorder Traversal : a b c d f g e
a(root)
b(left)
c d f g e (right)
13

Binary tree traversals: Inorder traversal


In the inorder traversal, we first visit its
left subtree (all the nodes) , then we
visit the root node and then its right
subtree.
void inorder(struct tree_node *p)
{
if (p !=NULL) {
inorder(p->left_child);
printf(%d\n, p->data);
inorder(p->right_child);
}
}

Inorder: b a f d g c e
b(left)
a(root)
f d g c e(right)
14

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

16

Binary search in an array

Look at array location (lo + hi)/2


Searching for 5:
(0+6)/2 = 3
hi = 2;
(0 + 2)/2 = 1

Using a binary
search tree

lo = 2;
(2+2)/2=2

7 11 13 17

13
5

11 17
17

Vous aimerez peut-être aussi