Académique Documents
Professionnel Documents
Culture Documents
Tree
A tree is a set of nodes that either:is empty or has a designated node, called the root, from
which hierarchically descend zero or more subtrees, which are also trees.
Fundamentals of TREES
Tree is a Data structure that matches the shape of tree
root: node with no parent, A non-empty tree has exactly one root
leaf: node with no children
Siblings/Brothers/Sisters: two nodes with the same parent.
path: a sequence of nodes n1, n2, , nk such that ni is the parent of ni+1 for 1 i < k
in other words, a sequence of hops to get from one node to another the length of a path is
the number of edges in the path, or 1 less than the number of nodes in it
M-ary: A tree with each node can maximum of M nodes, for Binary tree where M=2, all
these terminology explained in figure 1 and figure 2.
BINARY TREES
A binary tree is a finite set of elements that is either empty or is partitioned into two
disjoint subsets. The first subset contains a single element called the root of the tree.
The other two subsets are themselves binary trees, called the left and right subtrees of the
original tree. A left or right subtree can be empty. Each element of a binary tree is called
a node of the tree as illustrated in figure 4.
A complete binary tree of depth d that contains exactly 2l nodes at each level l between 0
and d. (This is equivalent to saying that it is the binary tree of depth d that contains
exactly 2d nodes at level d.)
The total number of nodes in a complete binary tree of depth d, tn equals the sum of the
number of nodes at each level between 0 and d.
tn = 20+ 21+ 22++ 2d
= j=0,d 2j
A binary tree of depth d is an almost complete binary tree if:
1.
Any node nd at level less than d-1 has
two sons.
2.
For any node nd in the tree with a right descendant at level d, nd must have a
left son and every left descendant of nd is either a leaf at level d or has two
sons.
The strictly binary tree of 12(b) satisfies condition 1, since every leaf is either at level 2
or at level 3. However, condition 2 is violated, since A has a right descendant at level 3
(J) but also has a left descendant that is a leaf at level 2 (E) as illustrated in figure 12(b).
The strictly binary tree of figure 12(c) satisfies both conditions 1 and 2.
The binary tree of figure 12(d) is also an almost complete binary tree but it is not strictly
binary, since node E has a left son but not a right son.
The nodes of an almost complete binary tree can be numbered so that the root is assigned
the number 1, a left son is assigned twice the number assigned its father, and a right son
is assigned one more than twice the number assigned its father.
An almost complete strictly binary tree with n leaves has 2n -1 nodes, as does any other
strictly binary tree with n leaves. An almost complete binary tree with n leaves that is not
strictly binary has 2n nodes.
OPERATIONS ON BINARY TREES(BT)
There are a number of primitive operations that can be applied to a binary tree. If p is a
pointer to a node nd of a binary tree, the function info(p) returns the contents of nd.
The functions returns the pointers to the
left(p)left son of nd
right(p)right son of nd
father(p)- father of nd
brother(p) - brother of nd
These functions return the null pointer if nd has no left son, right son, father, or brother.
Finally, the logical functions
isleft(p) and isright(p) return the value true if nd is a left or right son respectively, of
some other node in the tree, and false otherwise.In constructing a binary tree, the
following operations are useful. maketree(x) creates a new binary tree consisting of a
single node with information field x and returns a pointer to that node. setleft(p,x) accepts
a pointer p to a binary tree node with no left son.
In constructing a binary tree, It creates a new left son of node(p) with information field x.
setright(p,x) is analogous to setleft except that it creates a right son of node(p).
APPLICATIONS
Suppose we want to find all duplicates in a list of numbers. One way of doing this is to
compare each number with all those that precede it. However, this involves a large
number of comparisons. The number of comparisons can be reduced by using a binary
tree.
convention that we traverse left before right, only 3 traversals remain LVR, LRV, VLR
and in fact they are inorder, postorder and preorder approaches are explained in figure 13.
Tree Traversal Approaches
Example of Pre-Order
order: C F T B R K G, The trick: Walk around the outside of the tree and emit a node's
value when you touch the left side of the node and it is narrated in figure 16.
Example of In-order
order: B T R F K C G, The trick: Walk around the outside of the tree and emit a node's
value when you touch the bottom side of the node and it is explained in figure 18.
Example of Post-Order
order: B R T K F G C, The trick: Walk around the outside of the tree and emit a node's
value when you touch the right side of the node and it is portrayed in figure 20. The tree
methods further demonstrated in figure 21.
*********************
Figure 22 : BT
Such a binary tree has the property that all elements in the left subtree of a node n are less
than the contents of n, and all elements in the right subtree of n are greater than or equal
to the contents of n.
A binary tree that has this property is called a binary search tree. Another application of
binary trees: representing an expression containing operands and binary operators by a
strictly binary tree.
The root of the strictly binary tree contains an operator that is to be applied to the results
of evaluating the expressions represented by the left and right subtrees. A node
representing an operator is a nonleaf, whereas a node representing an operand is a leaf.
Traversing a tree in preorder means that the operator (the root) precedes its two operands
(the subtrees).
Thus a preorder traversal yields the prefix form of the expression. Traversing a binary
expression tree in postorder places an operator after its two operands, so that a postorder
traversal produces the postfix form of the expression, pre order and post order concpte
employed in figure 23.
Preorder: +A*BC
Postorder: ABC* +
Preorder: *+ABC
Postorder: AB + C*
Figure 23: Examples of tree traversal
}
The available list is not a binary tree but a linear list whose nodes are linked together by
the left field. Each node in a tree is taken from the available pool when needed and
returned to the available pool when no longer in use.
This representation is called the linked array representation of a binary tree.
Alternatively, a node may be defined by
struct nodetype {
int info;
struct nodetype *left;
struct nodetype *right;
struct nodetype *father; };
typedef struct nodetype *NODEPTR;
The operations info(p), left(p), right(p), and father(p) would be implemented by
references to p->info, p->left, p->right, and p->father, respectively. The routines
getnode and freenode simply allocate and free nodes using the routines malloc and free.
maketree function
This representation is called the dynamic node representation of a binary tree.
The maketree function, which allocates a node and sets it as the root of a single-node
binary tree, may be written as follows: under the dynamic node representation
NODEPTR maketree(int x) {
NODEPTR p;
p = getnode();
p->info = x;
p->left = NULL;
p->right = NULL;
return(p); } /* end maketree */
setleft( p,x)
The routine setleft( p,x) sets a node with contents x as the left son of node( p):
void setleft(NODEPTR p, int x)
{
if (p == NULL)
printf("void insertion\n");
else if (p->left !=NULL)
printf ("invalid insertion\n");
else
p->left = maketree(x);
} /* end setleft */
advance. We may implement the traversal of binary trees in C by recursive routines that
mirror the traversal definitions.
These are pretrav, intrav, and posttrav print the contents of a binary tree in preorder,
inorder, and postorder, respectively.
The parameter to each routine is a pointer to the root node of a binary tree.
Binary Tree Traversals in C
Pre Order Traversals in C
void pretrav(NODEPTR tree)
{
if (tree != NULL)
{
printf("%d\n", tree->info);
pretrav(tree->1eft);
pretrav(tree->right);
} /* end if */
} /* end pretrav */
In Order Traversals in C
void intrav(NODEPTR tree)
{
if (tree != NULL)
{
jntrav(tree->left);
printf("%d\n", tree->info);
intrav(tree->right);
} /* end if */
} I* end intrav */
Post Order Traversals in C
void posttrav(NODEPTR tree)
{
if (tree != NULL)
{
posttrav(tree->left);
posttrav(tree->right);
printf("%d\n", tree->info);
} /* end if */
} /* end posttrav */