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

📄 tree.cpp

📁 实现树的数据结构 用C语言 写的 编译通过
💻 CPP
字号:
#include<iostream>
#include<Node.h>
#include<Stack.h>
using namespace std;
template <class T>
class BinaryTree
{
	public:
		BinaryTree();
		BinaryTree(BinaryTreeNode<T> *lch,BinaryTreeNode<T> *rch,T item);
		int GetNumber(BinTreeNode<T> *root);
		int Exchange(BinTreeNode<T> *root);
        void  Create(istream& in,BinTreeNode<char> *&BT);
};
 


template <class T>
int BinaryTree<T>::GetNumber(BinTreeNode<T> *root)
{
	if(root!=NULL)
	{
		if (root->lch==NULL&&root->rch==NULL)
			return 1;
		else
			return GetNumber(root->lch)+GetNumber(root->rch);
	}
}
		
template <class T>
int BinaryTree<T>::Exchange(BinTreeNode<T> *root)
{
	if(root!=NULL)
	{
	 BinTreeNode<T> *p;
	 p=root->lch;
	 root->lch=root->rch;
	 root->rch=p;
	}
	Exchange(root->lch);
	Exchange(root->rch);
}



void BinaryTree<T>::create(istream& in,BinTreeNode<char> *&BT)
{stack<BinTreeNode<char> *>s;
BT=NULL;
BinTreeNode<char> *p,*t;int k;
char ch;
in>>ch;
while(ch!=0)
{switch(ch)
{
	case'(':s.Push(p);k=1;break;
	case')':s.Pop(t);break;
	case',':k=2;break;
	default:p=new BinTreeNode(ch);
		if(BT==NULL) BT=p;
		else if(k==1) {
			s.GetTop(t);t->lch=p;
		}
		else{
			s.GetTop(t);
			t->rch=p;
		}
}
in>>ch;
}
};


⌨️ 快捷键说明

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