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

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;

class node					//this is used to make class
{
	public:
		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++)
			if((vec_nod[i]).x<(vec_nod[j]).x)
				swap(vec_nod[i],vec_nod[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];
		vec_nod.push_back(tm_nod);
	}
	return vec_nod;
}
void Bi_tree(vector<node> &vec_nod)	//This is my problem function
{	

	while(vec_nod.size()>=2){
		Arng(vec_nod);
		node *node1;
		node *node2;
		node *node3 = new node;

		node1 = &vec_nod.back();
		vec_nod.pop_back();
		
		Arng(vec_nod);

		node2 = &vec_nod.back();
		vec_nod.pop_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;

		vec_nod.push_back(*node3);
//		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.
		cout<<vec_nod.back().x<<endl;
		
	}

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

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

}
*/

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


	vec_nod = Cont(vec);
	Bi_tree(vec_nod);
	cout<<(vec_nod.back()).x<<endl;
	
	cout<<endl<<endl;
	
	(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.