⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btree.cpp

📁 All are the assignments of mine that I have developped in Data Structure Laboratory Hours
💻 CPP
字号:
#include "iostream.h"
#include "conio.h"

class Node
{
	public:
		int nData;
		Node *pLeft,*pRight;
		Node * CreateBTree(Node *);
		void Insert(Node *,Node *);
		void Display(Node *);
		void LeafNode(Node *);
};
Node * Node::CreateBTree(Node *pRoot)
{
	Node *pTemp;
	char cAns;
	do
	{
		pTemp = new Node;
		pTemp->pLeft = pTemp->pRight = NULL;
		cout<<"\n Enter Data :";
		cin>>pTemp->nData;
		if(pRoot==NULL)
			pRoot = pTemp;
		else
			Insert(pRoot,pTemp);
		cout<<"\n Do U want to add more nodes :";
		cin>>cAns;
	}while(cAns == 'Y' || cAns == 'y');
	return(pRoot);
}
void Node::Insert(Node *pRoot,Node *pTemp)
{
	char cChoice;
	cout<<"\n Enter 'R' for adding Elements to Right\n Enter 'L' for adding element to Left of Node:"<<pRoot->nData;
	cin>>cChoice;
	if(cChoice == 'R'|| cChoice == 'r')
	{
		if(pRoot->pRight == NULL)
			pRoot->pRight = pTemp;
		else
			Insert(pRoot->pRight,pTemp);
	}
	if(cChoice == 'L'|| cChoice == 'l')
	{
		if(pRoot->pLeft == NULL)
			pRoot->pLeft = pTemp;
		else
			Insert(pRoot->pLeft,pTemp);
	}
}
void Node::Display(Node *pRoot)
{
	if(pRoot)
	{
		Display(pRoot->pLeft);
		cout<<"\t"<<pRoot->nData;
		Display(pRoot->pRight);
	}
}

void Node::LeafNode(Node *pRoot)
{
	if(pRoot)
	{
		if((pRoot->pLeft == NULL) && (pRoot->pRight == NULL))
			cout<<"\t"<<pRoot->nData;
		else
		{
			LeafNode(pRoot->pLeft);
			LeafNode(pRoot->pRight);
		}
	}
}

void main()
{
	Node p1,*pRoot = NULL;
	pRoot = p1.CreateBTree(pRoot);
	cout<<"\n Created Graph ";
	p1.Display(pRoot);
	cout<<"\n Total Leaf Nodes...";
	p1.LeafNode(pRoot);
	getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -