You are on page 1of 10

# Binary Search Tree

## Q1. Write functions to

a) count the number of nodes
b) number of right children
c) number of leaves
d) height
of a binary search tree.
Q2. Give the order in which the vertices of the following binary tree would be visited in post order
traversal. Also show the threads (both left and right) traversal.

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.

## Note that the root of the tree is at

index 5 and unused nodes start at
index 9.
ROOT: 5
AVAIL: 9
Draw the diagram of T in conventional form using arrows.

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:

{
Node *temp;

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

## void setLeft(node *p, int x)

{
node*temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->left=temp->right=0;
p->left=temp;
temp->father=p;
}
void setRight(node *p, int x)
{
node*temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->left=temp->right=0;
p->right=temp;
temp->father=p;
}
Q Draw the binary search tree given its inorder and preorder traversal sequence
inorder
preorder

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

## The Great Tree-List Recursion Problem

Write a recursive function treeToList(Node root) that takes an ordered binary tree and rearranges the
internal pointers to make a circular doubly linked list out of the tree nodes.
struct node {
int data;
struct node *left, *right;
};
typedef struct node *Node;
/*helper function -- given two list nodes, join them together so the second immediately follow the first.
Sets the .next of the first and the .previous of the second. */
static void join(Node a, Node b) {
a->right = b;

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 length-1 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;
treeInsert(&root, 4);
treeInsert(&root, 2);
treeInsert(&root, 1);
treeInsert(&root, 3);
treeInsert(&root, 5);

/* prints: 1 2 3 4 5 */

return(0);
}
void printList(Node root)
{
Node tmp=root;
do
{
printf(%d , tmp->data)
tmp=tmp->right;
}

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:

## G is connected and has no cycles.

G has no cycles, and a simple cycle is formed if any edge is added to G.
G is connected, but is not connected if any single edge is removed from G.
G is connected and the 3-vertex complete graph K3 is not a minor of G.
Any two vertices in G can be connected by a unique simple path.

If G has finitely many vertices, say n of them, then the above statements are also equivalent to any of the
following conditions:

## G is connected and has n 1 edges.

G has no simple cycles and has n 1 edges.

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 2-element 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

## 2.1.8 Half-edges, loose edges

o
2.2 Important graph classes

## Binary Search Tree

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

## void setLeft(node *p, int x)

{
if(p->left != 0)
{
cout<<"invalid insertion";
getch();
}
else
{
p->left=makeTree(x);
p->left->father=p;
}
}

## void bst::inTrav(node *temp)

{
if(temp!=0)
{
inTrav(temp->left);
cout<<temp->data<<" ";
inTrav(temp->right);
}
}
void bst::postTrav(node *temp)
{
if(temp!=0)
{
postTrav(temp->left);
postTrav(temp->right);
cout<<temp->data<<" ";
}
}
void bst::preTrav(node *temp)
{
if(temp!=0)
{
cout<<temp->data<<" ";
preTrav(temp->left);
preTrav(temp->right);
}
}

Change a tree so that the roles of the left and right pointers are swapped at every node.

## void mirror(struct node* node) {

if (node==NULL)
return;
else {
struct node* temp;
mirror(node->left); // do the subtrees
mirror(node->right);
// swap the pointers in this node
temp = node->left;
node->left = node->right;
node->right = temp;

4
/ \
2 5
/ \
1 3
is changed to...
4
/ \
5 2
/ \
3 1

}
}

isBST()

## if(isBST(root, INT_MIN, INT_MAX)) {

puts("This is a BST.");
} else {
puts("This is NOT a BST!");
}
int isBST(TreeNode *tmp, int min, int max) {
if(tmp == NULL)
return 1;
// true
if(tmp->data < min || tmp->data > max)
return 0;
// false
return isBST(tmp->left, min, tmp->data) && isBST(tmp->right, tmp->data, max);
}
Structure Declaration:
typedef struct node {
int info;
struct node* lchild;
struct node* rchild;
}NODE;

## Code for deletion:

NODE* delete (NODE* cnode, int elem) {
NODE* save;
if(cnode == NULL) {
printf("Element not in the tree !\n");
return;
}
if( elem < cnode->info)
cnode->lchild = delete (cnode->lchild, elem);
else if(cnode->info > elem)
cnode->rchild = delete (cnode->rchild, elem);
else
if(cnode->info == elem)
if(cnode->rchild == NULL && cnode->lchild == NULL) {
free(cnode);
return NULL;
}
else if(cnode->rchild == NULL) {
save = cnode->lchild;
free(cnode);
return save;
}
else
if(cnode->lchild == NULL) {
save = cnode->rchild;
free(cnode);
return save;
}
else {
save = findMin(cnode->rchild);
cnode->info = save->info;
cnode->lchild = delete (cnode->lchild, cnode->info);
return cnode;
}
}

// no child

// one child

// two children