got something magical. node3 ( a variable) seems to point itself when i have not mad it so.
Please help me understand this behavior.
It seem to me like magic.

And do provide with if any suggestion to correct the problem.

using namespace std;

class node					//this is used to make class
		int x;
		string code;
		node *Right;
		node *Left;
		node()				//constructor
			Right = NULL;
			Left = NULL;
			code = "";
//vector<node> vec_code;
//string str;
void Arng(vector<node> &vec_nod)			//function to arrange in decendin order.
	for(int i=0;i<vec_nod.size();i++)
		for(int j=i;j<vec_nod.size();j++)
 * void Disp(vector<node> vec_node)
	for(int i=0;i<vec_node.size();i++)
		cout<<vec_node[i].x<<" ";

vector<node> Cont(vector<int> vec)
	vector<node> vec_nod;
	node tm_nod;// tem_nod is temporary node in which i put values of vec and then push back to a vector of node (i.e vec_node).			
	for(int i=0;i<vec.size();i++){
		tm_nod.x = vec[i];
	return vec_nod;
void Bi_tree(vector<node> &vec_nod)	//This is my problem function

		node *node1;
		node *node2;
		node *node3 = new node;

		node1 = &vec_nod.back();

		node2 = &vec_nod.back();
		node3->Left = node1;
		cout<<node3->Left->x<<"left node\n";
		node3->Right = node2;
		cout<<node3->Right->x<<"Right node\n";
//		cout<<node3->Right->Right->x<<"Right node\n"; //this line gives segmnetation fault, which is expected and required.
		node3->x = node1->x + node2->x;

//		cout<<node3->Left->Right->x<<"left node\n";	//when this line is run in terminal it gives segmentation fault, which is proper.

		cout<<vec_nod.back().Right->Right->x<<"Right node\n";	//And the problem is here. there should not be any value.  it should
								//have given segmentation fault as this is the same (node3) which giving me 
								//segmentation fault(i.e in line number 70).
								//the node3 here seems to point to it self.

 * void Bi_read(node *Root)
	if(Root->Left != NULL){

	if(Root->Right != NULL){
	cout<<Root->x<<" ";


int main()
	vector<int> vec;
	vector<node> vec_nod;
//	string str;
	node Root;
	for(int i=0;i<2;i++)

	vec_nod = Cont(vec);
	(vec_nod.back()).code = "";
//	Bi_read(&(vec_nod.back()));
//	Disp(vec_code);
	return 0;

Don't waste a time on studies of this incorrect program behaviour. The fatal defect of your class Node manipulations approach is that std::vector is an intrusive container. It has its own copies of all elements. In the function Bi_tree you assign node addresses and manipulate with vector contents at the same time. The std::vector is a dynamic structure, all pointers to its elements are valid until you pop/push elements. So all links in your "tree" nodes are invalid.

Can you explain what for are you using std::vector at all? Build dynamic binary tree from the scratch w/o these incorrect and unnecessary vector manipulations.

i was trying to code huffman algorithm. i got stuck in this part of program.
Thank you for your correction. I have spent alot of time at this point. I was not getting whts was going on.

Please can you suggest what should i use instead of vectors.

This article has been dead for over six months. Start a new discussion instead.