You are on page 1of 13

BST Implementation Memory Management

/* Binary Search Tree implementation */


#include<stdio.h>
#include<stdlib.h>
struct BstNode {
struct BstNode *left;
int data;
struct BstNode *right;
};
typedef struct BstNode BstNode;
BstNode *GetNewNode(int data)
{
BstNode *newNode = malloc(sizeof(struct BstNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
BstNode *Insert(BstNode *root,int data)
{
if(root == NULL)
{
root = GetNewNode(data);
return root;
}
else if(data<=root->data)
{
root->left = Insert(root->left,data);
}
else
{
root->right = Insert(root->right,data);
}
return root;
}
int search(BstNode *root,int data)
{
if(root == NULL)
return -1;
else if (root->data == data)
return 1;
else if(data<=root->data)
return search(root->left,data);
else
return search(root->right,data);
}
int main()
{
struct BstNode *root = NULL;
int data;

//
//

root =
root =
root =
root =
root =
root =
root =
root
root

Insert(root,55);
Insert(root,65);
Insert(root,45);
Insert(root,25);
Insert(root,35);
Insert(root,53);
Insert(root,48);
= Insert(root,34);
= Insert(root,38);

printf("Enter Number to search: ");


scanf("%d",&data);
if(search(root,data)==1)
printf("item found\n");
else
printf("Item Not Found\n");
}

The Memory that is allocated to program for its execution can be


typically divided into four segments, namely Code/Text, Static/Global,
Stack, Heap.

HEAP

STACK

STATIC/GLOBAL

CODE/TEXT

Text segment will store all the instructions of the program. The
next segment is used to store all the global variables, all the variables
that are declared outside all the functions. The next one is called stack,

it is a scratch space for function call execution. All the local variables
live in stack. The last one is called Heap. Heap can grow or shrink
dynamically as per requirement. The size of other segments is decided
at compile time whereas Heap can dynamically expand or shrink at run
time, we can control dynamic allocation and de-allocation in heap. We
cannot control dynamic allocation and de-allocation in other segments.
Let us see what happens in stack and heap during program
execution.
When the program will start execution first the main function will
be called, whenever a function is called some amount of memory is
allocated for function execution. The allocated memory is called stack
frame. All the local variables and state of execution of the function call
would be stored in the stack frame of the function call.

MEMORY MANAGEMENT OF A PROGRAM

MAIN

STACK FRAME

STACK

HEAP

In main function we have the local variable root a pointer to


BstNode, it is initialized with NULL,

MEMORY MANAGEMENT OF A PROGRAM

ROOT

0
STACK
FRAME

MAIN

STACK

HEAP

Now after Initialization, we are making a function call to insert,


main function will pause at this stage and a new stack frame will be
allocated for the execution of insert, main will wait for the function
insert to finish and return. Once this insert call finishes main will
resume at line 2. We have two variables root and data in insert function
in which we are collecting the arguments.
BstNode *Insert(BstNode *root,int data)
{
if(root == NULL)
{
root = GetNewNode(data);
return root;
}
else if(data<=root->data)
{
root->left = Insert(root->left,data);
}
else

{
root->right = Insert(root->right,data);
}
return root;
}

MEMORY MANAGEMENT OF A PROGRAM

INSERT

ROOT

DATA

55

ROOT

0
STACK
FRAME

MAIN

STACK

HEAP

In this call to insert function we go towards root = GetNewNode(data);


of the function, i.e., we will make call to GetNewNode function once
again this function call will pass and a new stack frame will be
allocated for execution of GetNewNode function. We have two local
variables in get new node, NewNode and data. In this function we are
using malloc for memory allocation in heap.
Let,s say address of this new node is 200, malloc will return the
address 200, This pointer newnode we are setting values of this three
fields data as 55, left and right as NULL.

Now GetNewNode will return the address of NewNode and


finishes its execution.

MEMORY MANAGEMENT OF A PROGRAM

GetNewNode

NewNode

200

DATA

55

NULL

INSERT

ROOT

DATA

55

ROOT

55

NULL

STACK
FRAME

MAIN

STACK

HEAP

Once the function finishes execution stack frame allocated to it is


reclaimed call to insert function will resume at root =
GetNewNode(data); and the return of GetNewNode, address of
NewNode will be set in the root which is local variable in insert call.

200

MEMORY MANAGEMENT OF A PROGRAM

55

NULL

INSERT

ROOT

200

DATA

55

ROOT

200

NULL

STACK
FRAME

MAIN

HEAP

STACK

And now, this particular insert function return the address of the
root and finish. Now main will resume at line root = insert (root,55);
and root is set to 200.
MEMORY MANAGEMENT OF A PROGRAM

NULL

ROOT

55

200
STACK
FRAME

MAIN

STACK

HEAP

NULL

200

Now main control will go to the next line and it will call the insert
function with a value 65, once again execution of main will be paused
and a stack frame will be allocated for execution of insert. Now, this
time for insert call root is not NULL, so program will not enter the first
if condition and it will access the data field of the node at address 200
using a local variable pointer named root.
MEMORY MANAGEMENT OF A PROGRAM

NULL

INSERT

ROOT

200

DATA

65

ROOT

200

55

NULL

200

STACK
FRAME

STACK
FRAME

MAIN

STACK

HEAP

Then the value get compared with the value 65, 65 is greater than
55 so we will go else condition, i.e. line root->right = Insert(root>right,data); now, we are making a recursive call here. Recursion is a
function calling itself. So, Execution of this insert call will be paused
and a new stack frame will be allocated for execution of another insert
call to which the arguments passed are NULL (address of left child) to
local variable root and data i.e. 65.

MEMORY MANAGEMENT OF A PROGRAM

INSERT

INSERT

ROOT

DATA

65

ROOT

200

DATA

65

ROOT

200

MAIN

STACK

STACK
FRAME
3

NULL

55

NULL

200

STACK
FRAME
2

STACK
FRAME
1

HEAP

So, for this particular insert call root is NULL, so control will go
inside first if condition and we will make a call to GetNewNode
function, now insert will pause and a stack frame will be created to
GetNewNode function, and inside this function there is a local variable
NewNode. In this we are using malloc for memory allocation in heap,
the return address of malloc is passed to NewNode, so let us assume
address be 300.

MEMORY MANAGEMENT OF A PROGRAM

NewNode
GetNew
Node

INSERT

INSERT

300

DATA

65

ROOT

DATA

65

ROOT

200

DATA

65

ROOT

200

MAIN

STACK

STACK
FRAME
4

STACK
FRAME
3

STACK
FRAME
2

NULL

65

NULL

300

NULL

55

NULL

200

STACK
FRAME
1

HEAP

Now GetNewNode will return 300 and finish. Execution of this


call to insert will resume in first if condition (root = GetNewNode(data); ) it
will set root of insert function to 300.

MEMORY MANAGEMENT OF A PROGRAM

INSERT

INSERT

ROOT

300

DATA

65

ROOT

200

DATA

65

ROOT

200

MAIN

STACK

STACK
FRAME
3

STACK
FRAME
2

NULL

65

NULL

300

NULL

55

NULL

200

STACK
FRAME
1

HEAP

Now this call to insert will return address 300 and finish. Insert
below will resume at the line (root->right = Insert(root->right,data);) and
now in this insert call right child of this address at node 200 wil be set
as return of the previous insert call which is 300.

MEMORY MANAGEMENT OF A PROGRAM

INSERT

ROOT

200

DATA

65

ROOT

200

MAIN

STACK
FRAME
2

NULL

65

NULL

300

NULL

55

300

200

STACK
FRAME
1

STACK

HEAP

So now two nodes are linked and finally insert call will finish.
Control will return back to main to the line (root = Insert (root, 65)) root
will be rewritten as 200.
MEMORY MANAGEMENT OF A PROGRAM

ROOT

200

MAIN

STACK

NULL

65

NULL

300

NULL

55

300

200

STACK
FRAME
1

HEAP

This will continue for all the other function calls.

You might also like