淘先锋技术网

首页 1 2 3 4 5 6 7

1.建立一棵二叉查找树
2.按中序遍历的要求显示树中每一个结点的值
3.按要求去查找某一个结点

using namespace std;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#define Maxsize 10

/*
括号表示法
'('表示一颗左子树的开始
')'表示一颗子树的结束
','表示一颗右子树的开始
单个字符:节点的值


构造方法:
先构造根节点,再构造左节点,继续构造右节点
构造的过程中需要使用栈来存储根节点


处理方法:
使用一个变量ch去扫描括号表示法中的数据	:
若ch = '(',则将前面的创建的节点当做父节点压入栈中,并且将k = 1,并且开始处理左节点;
若ch = ')',则表示左右孩子都处理完毕,退栈
若ch = ',',则表示开始处理右孩子,将k = 2;

若遇到k = 1,则将创建的节点作为左节点
若遇到k = 2, 则将创建的节点作为右节点



*/


struct Sqtree
{
	char data;
	struct Sqtree *lchild, *rchild;   //指向左树和右树的指针
};


// 创建一颗二叉树
void CreateTree(Sqtree *& b, char * str)
{
	Sqtree * s[Maxsize], *p = NULL;
	int top = -1, k = 0, j = 0;
	char ch;
	b = NULL;
	ch = str[j];
	while (ch != '\0')
	{
		switch (ch)
		{
		case'(':top++; s[top] = p; k = 1; break;
		case')':top--; break;
		case',':k = 2; break;
		default:
			p = new Sqtree;
			p->data = ch;
			p->lchild = p->rchild = NULL;
			if (b == NULL)
			{
				b = p;
			}
			else
			{
				switch (k)
				{
				case 1:s[top]->lchild = p; break;
				case 2:s[top]->rchild = p; break;
				}
			}
			break;
		}
		j++;
		ch = str[j];
	}
}


// 查找节点的值
Sqtree * FindSqtree(Sqtree *& s, char data)
{
	Sqtree * p;
	if (s == NULL) {
		return NULL;
	}
	else if (s->data == data)
	{
		return s;
	}
	else
	{
		p = FindSqtree(s->lchild, data);
		if (p == NULL)
		{
			return p;
		}
		else
		{
			return FindSqtree(s->rchild, data);
		}
	}
}



//求解二叉树的高度
int DepthSqtree(Sqtree *& s)
{
	int Depth_left_child, Depth_right_child;
	if (s == NULL)
	{
		return 0;
	}
	else
	{
		Depth_left_child = DepthSqtree(s->lchild);
		Depth_right_child = DepthSqtree(s->rchild);
		return (Depth_left_child > Depth_right_child) ? (Depth_left_child + 1) : (Depth_right_child + 1);
	}
}



// 先序打印二叉树
void PrePrintSqtree(Sqtree *&s)
{
	if (s != NULL)   //先输出根节点
	{
		cout << s->data;
		if (s->lchild != NULL || s->rchild != NULL)
		{
			cout << '(';
			PrePrintSqtree(s->lchild);
			if (s->rchild != NULL)
			{
				cout << ',';
			}
			PrePrintSqtree(s->rchild);
			cout << ")";
		}
	}
}


//中序遍历
void MidPrintSqtree(Sqtree *&s)
{
	if (s != NULL)
	{
		if (s->lchild != NULL || s->rchild != NULL)
		{
			cout << "左节点";
			MidPrintSqtree(s->lchild);   // 先输出左节点
			cout << "   ";
			cout << "根节点";
			cout << s->data;             //再输出根节点
			cout << "   ";
			if (s->rchild != NULL)
			{
				cout << "右节点";
				MidPrintSqtree(s->rchild);  //最后输出右节点
				cout << "   ";
			}

		}
		else
		{
			cout << s->data;
		}
	}
}


void BackPrintSqtree(Sqtree *&s)
{
	if (s != NULL)
	{
		if (s->lchild != NULL || s->rchild != NULL)
		{
			cout << "左节点";
			BackPrintSqtree(s->lchild);   // 先输出左节点
			cout << "   ";
			if (s->rchild != NULL)
			{
				cout << "右节点";
				BackPrintSqtree(s->rchild);  // 最后输出右节点
				cout << "   ";
			}
			cout << "根节点";
			cout << s->data;             // 再输出根节点
			cout << "   ";
		}
		else
		{
			cout << s->data;
		}
	}
}

int main()
{
	Sqtree * s;
	char a[1000];
	char * data = a;
	char find_data;
	cout << "请输入需要存储的数据" << endl;
	cin >> data;
	CreateTree(s, data);   //生成二叉树
	PrePrintSqtree(s);        //先序打印二叉树
	cout << endl;
	cout << "请输入需要查找的数据的左节点" << endl;
	cin >> find_data;
	cout << find_data << "的左节点为:" << endl;
	cout << FindSqtree(s, find_data)->lchild->data<< endl;
	cout << "二叉树的高度为" << DepthSqtree(s) << endl;
	system("pause");
	return 0;
}