Q3. For the following binary trees, determine the order in which the nodes will be visited in the mixed
order given by invoking procedure A:
void
{
A(pointer P)
if(P!=NULL)
{
visit(P);
B(P>left);
B(P>right);
}
void
B(pointer P)
{
if(P!=NULL)
{
A(P>left);
visit(P);
A(P>right);
}
}
2
3
5
7
4
6
Q4. Write a member function that will traverse a binary tree level by level. That is the root is visited first
then the immediate children of the root, then the grand children of the root and so on.
Q5. Construct a binary tree if its inorder and preorder traversals are given as follows:
Preorder
: G B Q AC K F PD E R H
Ioder
: Q B K C FAG PE D H R
Q6. Construct a binary tree if its inorder and postorder traversals are given as follows:
Postorder
Ioder
: D G B AH E I C F
:GDBHIEFCA
Q7. What is a threaded tree? Is there any advantage of a threaded binary tree over a simple binary tree.
Q8. Suppose T is stored in memory as an array of nodes each having an information part INFO and two
pointers called LEFT and RIGHT.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
INF
O
20
30
40
50
60
70
80
90
LEF
T
0
1
0
0
2
0
0
7
10
RIGH
T
0
13
0
0
6
8
0
14
0
35
12
45
55
11
95
Q9. Define a Binary Search Tree. What is the maximum no. of nodes in a complete binary tree of height
h?
Q10. Write a function that accepts a pointer to a Binary Search Tree and a pointer to a node of the tree
and returns the level of the node in the tree.
Q11. Write a function that checks whether or not a binary tree is perfectly balanced.
Q12. Write a function to test whether a binary tree is a binary search tree.
Q13. Write a function to create a mirror image of a binary search tree.
Write a function that will traverse a binary tree level by level. That is the root is visited first then
the immediate children of the root, then the grand children of root and so on.
Ans:
void breadFirst()
{
Node *temp;
temp=head;
insert(temp);
while(isEmpty()==FALSE)
{
temp=delete();
if(temp>left != 0)
insert(temp>left);
if(temp>right != 0)
insert(temp>right);
printf(\t%d,temp>data);
}
}
 EACKFHDBG
 FAEKCDHGB
Ans: the table of node and its left and right subtree is:
NODE
F
A
E
K
C
D
H
G
B
LEFT
EACK
E
C
H
B

RIGHT
HDBG
CK
BG

b>left = a;
}
/* helper function  given two circular doubly linked lists, append them and return the new list. */
static Node append(Node a, Node b) {
Node aLast, bLast;
if (a==NULL) return(b);
if (b==NULL) return(a);
aLast = a>left;
bLast = b>left;
join(aLast, b);
join(bLast, a);
return(a);
}
static Node treeToList(Node root) {
Node aList, bList;
if (root==NULL) return(NULL);
/* recursively solve subtrees  leap of faith! */
aList = treeToList(root>left);
bList = treeToList(root>right);
/* Make a length1 list out of the root */
root>left = root;
root>right = root;
/* Append everything together in sorted order */
aList = append(aList, root);
aList = append(aList, bList);
return(aList);
}
/* Create a new node */
static Node newNode(int data) {
Node node = (Node) malloc(sizeof(struct node));
node>data = data;
node>left = NULL;
node>right = NULL;
return(node);
}
/* Add a new node into a tree */
static void treeInsert(Node* rootRef, int data) {
Node root = *rootRef;
if (root == NULL) *rootRef = newNode(data);
else {
if (data <= root>data) treeInsert(&(root>small), data);
else treeInsert(&(root>large), data);
}
}
/* Demo that the code works */
int main() {
Node root = NULL;
Node head;
treeInsert(&root, 4);
treeInsert(&root, 2);
treeInsert(&root, 1);
treeInsert(&root, 3);
treeInsert(&root, 5);
head = treeToList(root);
printList(head);
/* prints: 1 2 3 4 5 */
return(0);
}
void printList(Node root)
{
Node tmp=root;
do
{
printf(%d , tmp>data)
tmp=tmp>right;
}while (tmp!=head);
}
Tree
The term "tree" was coined in 1857 by the British mathematician Arthur Cayley. In mathematics, and
more specifically in graph theory, a tree is an undirected graph in which any two vertices are connected
by exactly one simple path. In other words, any connected graph without simple cycles is a tree.
A forest is a disjoint union of trees.
The various kinds of data structures referred to as trees in computer science are equivalent as undirected
graphs to trees in graph theory, although such data structures are generally rooted trees, thus in fact being
directed graphs, and may also have additional ordering of branches.
Definitions
A tree is an undirected simple graph G that satisfies any of the following equivalent conditions:
If G has finitely many vertices, say n of them, then the above statements are also equivalent to any of the
following conditions:
Graph
A Graph is a representation of a set of objects where some pairs of objects are connected by links. The
interconnected objects are represented by mathematical abstractions called vertices, and the links that
connect some pairs of vertices are called edges.
Definition
In the most common sense of the term, a graph is an ordered pair G = (V, E) comprising
a set V of vertices or nodes together with a set E of edges or lines, which are 2element subsets of V (i.e.,
an edge is related with two vertices, and the relation is represented as an unordered pair of the vertices
with respect to the particular edge). To avoid ambiguity, this type of graph may be described precisely
as undirected and simple.
Types of graphs
o
2.1 Distinction in terms of the main definition
2.1.4 Multigraph
2.1.5 Quiver
node* makeTree(int x)
{
node *temp=new node();
temp>data=x;
temp>left=0;
temp>right=0;
if(root==0)
temp>father=0;
return temp;
}
void insert(int x)
{
node *f,*s;
if(root==0)
root=makeTree(x);
else
{
s=root;
while(s!=0)
{
f=s;
if(x<f>data)
s=f>left;
else
s=f>right;
}
if(x<f>data)
setLeft(f,x);
else
setRight(f,x);
}
}
Change a tree so that the roles of the left and right pointers are swapped at every node.
4
/ \
2 5
/ \
1 3
is changed to...
4
/ \
5 2
/ \
3 1
}
}
isBST()
// no child
// one child
// two children