Sunteți pe pagina 1din 55

Trees

Binary Trees
A Binary Tree is a finite set of elements that is
either empty or is partitioned into three
disjoint subsets.
Root.
Left subtree.
Right subtree.
Each element of a binary tree is called a node
of the tree.
Father: If A is the root of a binary tree and B is
the root of its left or right sub tree, then A is said
to be the father of B. B is said to be the son of A.
Leaf: A node that has no sons.
Ancestor/Descendant: Node n1 is an ancestor of
node n2 if n1 is either the father of n2 or father
of some ancestor of n2.
Left descendant: node n2 is a left descendant of
n1 if n1 is either the left son of n1 or a
descendant of left son of n1.
Brothers: If two nodes have the same father.
Binary trees and non binary trees.
Strictly Binary Tree: If every node in a binary
tree has nonempty left and right sub-trees.
A strictly binary tree with n leaves always
contains 2n-1 nodes.
Level: The root of a tree has level 0, and the
level of any other node in the tree is one more
than the level of its father.
Depth: The depth of a binary tree is the
maximum level of any leaf in the tree.
Complete Binary Tree
A complete binary tree of depth d is the
strictly binary tree all of whose leaves are at
level d.
If a binary tree contains m nodes at level l, it
contains at most 2m nodes at level l+1.
Since a binary tree can contain at most one
node at level 0, it can contain at most 2
l
nodes
at level l.
The total number of nodes in a complete binary
tree of depth d, equals the sum of the number of
nodes at each level between 0 and d.
i.e, tn =2
0
+2
1
+2
2
+.+2
d.
Sum = 2
d+1
-1.

Similarly, if the number of nodes, tn, in a
complete binary tree is known, we can compute
its depth d, from the 2
d+1
-1.
So d = log
2
(tn+1)-1
Almost complete binary tree
A binary tree with 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.

An almost complete binary tree is a tree in
which each node that has a right child also has
a left child. Having a left child does not require
a node to have a right child. Stated alternately,
an almost complete binary tree is a tree
where for a right child, there is always a left
child, but for a left child there may not be a
right child.

A Binary Tree of depth d is Almost Complete
iff:
1. The tree is Complete Binary Tree(All nodes)
till level(d-1)
2. At level d,(i.e, the last level), if a Node is
present, then all the Nodes to the left of that
node should also be present.
Numbering the nodes.
Root =1.
Left son is assigned the twice the number of
father.
Right son is assigned one more than the twice
the number assigned to father.
Strictly binary tree has odd number of nodes.
Almost complete binary tree has even
number of nodes.
Operations on Binary Trees
If p is a pointer to a node nd of a binary tree,
The functions:
info(p): returns contents of nd.
left(p): returns left son of nd.
right(p): returns right son of nd.
father(p): returns father of nd.
brother(p): returns brother of nd.
isleft(p)/isright(p): returns true if nd is left/right
son p else returns false.
Implementation of isleft
q=father(p);
if(q==null)
return(false)
if(left(q)==p)
return(true);
return(false);
OR
father(p) && p==left(father(p))

Brother(p)can be implemented as:
if(father(p) == null)
return(null)
if(isleft(p))
return(right(father(p)));
return(left(father(p)));

Applications of Binary Trees
A binary tree is a useful data structure when two-way
decisions must be made at each point in a process.
Ex: find all duplicates in a list of numbers.
Difference between list representation and tree.
The first number in the list is placed in a node that is
established as the root of a binary tree with empty left
and right binary tree.
Each successive number is compared with number in a
root, if it matches, we have a duplicate. If its smaller,
examine the left sub tree else examine the right
subtree.
Ex: 14,15,4,9,7,18,3,5,16,4,20,17,9,14,5.
Algorithm
scanf("%d",&number);
tree=maketree(number);
while(there are numbers left in the input)
{
scanf("%d",&number);
p = q =tree;
while(number != info(p) && q !=NULL)
{
p=q;
if(number < info(p))
q = left(p));
else
q = right(p);
}
if(number == info(p))
printf("%d %s\n",number, "is a duplicate");

elseif(number < info(p))
setleft(p,number);
else
setright(p,number);
}

Traversing
To pass through the tree, enumerating each of
its nodes once. i.e, visiting each node as it is
enumerated.
The order in which the nodes of a linear list
are visited in a traversal is clearly from first to
last.
The different orderings are used for traversal
in different cases. We have three different
methods.
Binary tree traversals
Pre-order.
In-order.
Post-order.
Pre-order Traversal
1. Visit the root.
2. Traverse the left sub-tree in preorder.
3. Traverse the right sub-tree in preorder.
In-order Traversal
1. Traverse the left sub-tree in inorder.
2. Visit the root.
3. Traverse the right sub-tree in inorder.

Post-order Traversal
1. Traverse the left sub-tree in preorder.
2. Traverse the right sub-tree in preorder.
3. Visit the root.

Examples.

Binary Search Tree
The binary tree in which smaller elements are
at the left of the root and all greater than or
equal are at right of the root.
If a binary search tree is traversed in inorder
we get the sorted elements.
Binary Tree for Sorting
As we read the numbers, they can be inserted
into a binary tree.
When a number is compared with the
contents of a node in the tree:
Left branch is taken if the number is smaller than
the content of the node.
Right branch is taken if the number is greater than
or equal to the content of the node.
14,15,4,9,7,18,3,5,16,4,20,17,9,14,5.
Representation of Expressions.
The expression containing operands and binary
operators by a strictly binary tree.
A node representing an operator is a non-leaf
node.
Node representing an operand is a leaf.
Ex:
A+B*C.
(A+B)*C.
A+(B-C)*D$(E*F)
(A+B*C)$((A+B)*C)
Traverse the above expressions to get prefix
and postfix expressions.
Binary Tree Representations
Array Representation.
Linked Representation.

Array Representation
#define NUMNODE 500
struct nodetype{
int info;
int left;
int right;
int father;
};
struct nodetype node[NUMNODES];
Accessing??
Available list
int avail,i;
{
avail =1;
for (i=0;i<NUMNODES;i++)
node[i].left=i+1;
node[NUMNODES-1].left=0;
}

Alternative for:
isleft() and isright().
Brother().


Linked Representation
struct nodetype
{
int info;
struct nodetype *left;
struct nodetype *right;
struct nodetypr *father;
};
typedef struct nodetype *NODEPTR;
Program..
Internal and External nodes
Internal nodes:- The non-leaf nodes are called
internal nodes.
External nodes:- The leaf nodes are called
external nodes.
Binary Tree Traversal in C
Pretrav.
Intrav.
Posttrav.
Pretrav
void pretrav(NODEPTR tree)
{
if(tree != NULL)
{
printf("%d",tree->info);
pretrav(tree->left);
pretrav(tree->right);
}
}

void intrav(NODEPTR tree)
{
if(tree != NULL)
{
intrav(tree->left);
printf("%d",tree->info);
intrav(tree->right);
}
}

void posttrav(NODEPTR tree)
{
if(tree != NULL)
{
posttrav(tree->left);
posttrav(tree->right);
printf("%d",tree->info);
}
}
Threaded Binary Tree
Binary trees have a lot of wasted space: the
leaf nodes each have 2 null pointers
We can use these pointers to help us in
inorder traversals
We have the pointers reference the next node
in an inorder traversal; called threads
We need to know if a pointer is an actual link
or a thread, so we keep a boolean for each
pointer.
A binary tree is threaded by making all right
child pointers that would normally be null
point to the inorder successor of the node,
and all left child pointers that would normally
be null point to the inorder predecessor of the
node
Threaded Tree Traversal
We start at the leftmost node in the tree, print
it, and follow its right thread
If we follow a thread to the right, we output
the node and continue to its right
If we follow a link to the right, we go to the
leftmost node, print it, and continue
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Start at leftmost node, print it
Output
1
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow link to right, go to leftmost
node and print
Output
1
3
5
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
5
6
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
5
6
7
8
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
8
9
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
5
6
7
8
9
11
Threaded Tree Traversal
8
7 5
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
8
9
11
13
struct nodetype{
int info;
struct nodetype *left;
struct nodetype *right;
int rthread;
}
typedef struct nodetype *NODEPTR;

Constructing Threaded Binary Tree
NODEPTR maketree(int x)
{
NODEPTR p;
P = getnode();
p->info = x;
p->left = NULL;
p->right = NULL;
p->rthreaded = TRUE;
return(p);
}

Constructing left sub-tree
Set left
Constructing right sub-tree
Right sub-tree
In order traversal
In-trav

S-ar putea să vă placă și