把二叉树转变为左孩子右兄弟树

// erchatoshu.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<queue>
using namespace std;
//二叉树节点结构////////////////////////
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
struct TreeNode{
    int Element;
    SearchTree left;
    SearchTree right;
};

///左孩子右兄弟树节点结构//////////////////
typedef struct TreeNodeforTree *Tree;
struct TreeNodeforTree {
	int Element;
	Tree LeftChild;
	Tree Brother;
};
///二叉树到普通树转变函数//////////////////
Tree change(SearchTree T)
{
	///生成普通树树根///////////////////////
	Tree TT = (Tree)malloc(sizeof(TreeNodeforTree));
	TT->Brother = NULL;
	TT->LeftChild = NULL;
	TT->Element = T->Element;

	//二叉树和普通树的树根分别人队/////////////////
	queue<SearchTree> que;
	queue<Tree> quee;
	que.push(T);
	quee.push(TT);


	while (!que.empty())
	{
		//先检查有没有左孩子,有左孩子就先处理左孩子////////////////////////
		if (que.front()->left) {
			que.push(que.front()->left);
			Tree leftchild= (Tree)malloc(sizeof(TreeNodeforTree));
			leftchild->Brother = NULL;
			leftchild->LeftChild = NULL;
			leftchild->Element = que.front()->left->Element;
			quee.front()->LeftChild = leftchild;

		//再检查有没有右孩子,有右孩子则处理右孩子//////////////////////////////
			if (que.front()->right) {
				que.push(que.front()->right);
				Tree brother = (Tree)malloc(sizeof(TreeNodeforTree));
				brother->Element = que.front()->right->Element;
				brother->Brother = NULL;
				brother->LeftChild = NULL;
				leftchild->Brother = brother;
				quee.push(leftchild);
				quee.push(brother);
			}
		}
		//没有左孩子有右孩子的话把父亲的LeftChild指针赋值为右孩子的地址///////////////////////
		else
		{
			if (que.front()->right) {
				que.push(que.front()->right);
				Tree brother = (Tree)malloc(sizeof(TreeNodeforTree));
				brother->Element = que.front()->right->Element;
				brother->Brother = NULL;
				brother->LeftChild = NULL;
				quee.front()->LeftChild = brother;
				quee.push(brother);
			}
		}
		//循环结束弹出对应的树节点/////////////////////////////////
		que.pop();
		quee.pop();
	}
	return TT;
}

///递归的二叉树插入函数
SearchTree Insert(int x, SearchTree T)
{
	if (T == NULL) {
		T = (SearchTree)malloc(sizeof(struct TreeNode));
		if (T == NULL)
			EXIT_FAILURE;
		else
		{
			T->Element = x;
			T->left = T->right = NULL;
		}
	}
	else
	{
		if (x < T->Element)
			T->left = Insert(x, T->left);
		else if (x > T->Element)
			T->right = Insert(x, T->right);
		//否则在已在树上什么都不做
		return T;
	}
}
///递归打印二叉树/////////////////////////////////////
void printTree(SearchTree T)
{
	if (T != NULL)
	{
		cout << T->Element <<'\t';
		printTree(T->left);
		printTree(T->right);
	}
}

///打印左孩子右兄弟树结构的函数//////////////////
void print(Tree T)               
{
	Tree left, right;
	queue<Tree> que;
	que.push(T);
	while (!que.empty())
	{
		cout << que.front()->Element<< '\t';
		left = que.front()->LeftChild;
		que.pop();
		if (left != NULL)
		{
			right = left->Brother;
			que.push(left);
			if (right != NULL)
			{
				que.push(right);
			}

		}
	}
	cout << endl;
}

int main()
{
	SearchTree T = NULL;
	for (int i = 1; i <= 14; i++)
		T=Insert(i, T);
	printTree(T);
	cout << endl;
	cout << "-------------------------------------------------------------------------------"<<endl;
	Tree TT = change(T);
	print(TT);
	while (1);
    return 0;
}

  

posted @ 2017-04-06 22:40  lineaar  阅读(2606)  评论(0编辑  收藏  举报