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

📄 threaded.cpp

📁 All are the assignments of mine that I have developped in Data Structure Laboratory Hours
💻 CPP
字号:
/* Ass No. 7
   Program: Threaded Binary Tree : 1. Creation 2. Display(InOrder)
   Programmed By: M. R. Sanghavi*/

/* Header File Declaration*/
#include "iostream.h"
#include "conio.h"
/* Class Definition For Node of Tree */
class Node
{
	public:
		/* Data Members */
		int nData,nLeft,nRight;
		Node *pLeft,*pRight;
		/* Memeber Function */
		Node * CreateBTree(Node *);
		void Insert(Node *,Node *);
		void Display(Node *);
};
/* Header Node for Threaded Binary Tree */
Node *Header = NULL;
/* Function which Create & Attach the Node to Tree */
Node * Node::CreateBTree(Node *pRoot)
{
	Node *pTemp;
	char cAns;
	do
	{
		pTemp = new Node;
		cout<<"\n Enter Data :";
		cin>>pTemp->nData;
		if(pRoot==NULL)
		{
			pRoot = pTemp;
			pRoot->pRight = pRoot->pLeft = Header;
			pRoot->nRight = pRoot->nLeft = 1;
		}
		else
			Insert(pRoot,pTemp);
		cout<<"\n Do U want to add more nodes :";
		cin>>cAns;
	}while(cAns == 'Y' || cAns == 'y');
	return(pRoot);
}
/* Function to Insert the Node in to Tree */
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->nRight == 1)
		{
			pTemp->pRight = pRoot->pRight;
			pRoot->pRight = pTemp;
			pTemp->pLeft = pRoot;
			pRoot->nRight = 0;
			pTemp->nRight = pTemp->nLeft = 1;
		}
		else
			Insert(pRoot->pRight,pTemp);
	}
	if(cChoice == 'L'|| cChoice == 'l')
	{
		if(pRoot->nLeft == 1)
		{
			pTemp->pLeft = pRoot->pLeft;
			pRoot->pLeft = pTemp;
			pTemp->pRight = pRoot;
			pRoot->nLeft = 0;
			pTemp->nRight = pTemp->nLeft = 1;
		}
		else
			Insert(pRoot->pLeft,pTemp);
	}
}
/* Function Deisplay a Tree in InOrder Traversal */
void Node::Display(Node *pRoot)
{
	Node *p = pRoot;
	int nFlag = 0;
	while(p!=Header)
	{
		while(nFlag==0 && p->nLeft==0)
			p = p->pLeft;
		cout<<"\t"<<p->nData;
		if(p->nRight == 0)
			nFlag = 0;
		else
			nFlag = 1;
		p = p->pRight;
	}
}
/* Main Function Starts Here */
void main()
{
	Node p1,*pRoot = NULL;
	pRoot = p1.CreateBTree(pRoot);
	cout <<"\n Created Threded Binary Tree ... \n";
	p1.Display(pRoot);
	getch();
}
/* E n d   o f   P r o g r a m */

⌨️ 快捷键说明

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