Vous êtes sur la page 1sur 7

Lecture Notes on Data Structures using Java Page 1

TREE ADT

Binary Tree

 finite set of elements that is either empty or is


partitioned into three disjoint subsets
 the first subset contains a single element called the
root of the tree
 the other two subsets are themselves trees called
the left and right subtree

B C

D
E F

G H

 A is the root of the tree


 A is the parent of B
 B is the left child of A
 C is the right child of A
 D, G, H and F are leaves
 the level of node G is 3
Lecture Notes on Data Structures using Java Page 2

 the depth of the tree is 3

 a node that has no children is called a leaf


 node n1 is an ancestor of node n2, if either n1 is the
parent of n2 or n1 is the parent of some ancestor of n2
 if every non-leaf has a left and right child, then the tree
is said to be a strictly binary tree
 the level of a node in a binary tree is defined as
follows
 the root of the tree has level 0
 the level of any other node in the tree is 1 more
than the level of its father
 the depth of a binary tree is the maximum level of any
leaf in the tree
 a binary tree of depth d is an almost complete binary
tree if
 each leaf in the tree is either at level d or d-1
 for any node n in the tree with a right descendant
at level d, all the left descendants of n that are
leaves should also be at level d
 the nodes of an almost complete binary tree can be
numbered so that the root is assigned number 1, a left
son is assigned twice the number assigned to its father
and a right son is assigned 1 more than twice the
number assigned to its father
 if a binary tree has m nodes at level l, then it contains
at most 2m nodes at level l+1
Lecture Notes on Data Structures using Java Page 3

Binary Search Tree

 a binary tree in which all elements in the left


subtree of a node is less than the value of the
node and all elements at the right subtree of a
node is greater than the value of the node

Example:
10

8 15

6
12 20

11 13

Operations on binary tree

1. item() 7. isright()
2. left() 8. setleft(item);
3. right() 9. setright(item)
4. parent() 10. setright(x)
5 5. sibling()
Lecture Notes on Data Structures using Java Page 4

6. isleft()

Traversal operations

Pre-order traversal
 visit root
 traverse left subtree in pre-order
 traverse right subtree in pre-order

In-order traversal
 traverse left subtree in in-order
 visit root
 traverse right subtree in in-order

Post-order traversal
 traverse left subtree in post-order
 traverse right subtree in post-order
 visit root

Example:

B C

D
E F

G H
Lecture Notes on Data Structures using Java Page 5

Remark: If a binary search tree is traversed using in-


order traversal, the result is a sorted list

Example:
10

8 15

6
12 20

11 13

Inorder Traversal: 6 8 10 11 12 13 15 20
Lecture Notes on Data Structures using Java Page 6

Tree representation of expression

 Another use of binary tree is in representing


arithmetic expressions:

a) a+b*c b) a+b*c-d/e

+ -

+ /
a *

a * d e
b c

b c

Remarks:
 If a tree representation of an expression is
traversed using

pre-order
- the result is a prefix form of the
expression
post-order - the result is a postfix form of the
expression
inorder - the result is an infix form of the
expression
Lecture Notes on Data Structures using Java Page 7

Traversal of Tree in a)

pre-order  +a*bc
post-order  abc*+
inorder  a bc * +

Traversal of Tree in b)

pre-order  -+a*bc/de
post-order  abc*+de/-
inorder  a+b*c-d/e

Vous aimerez peut-être aussi