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


    // 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 <<'	';
    		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<< '	';
    		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;
    }
    

      

    转载于:https://www.cnblogs.com/linear/p/6675820.html

  • 相关阅读:
    C#操作Word (2)-- 打开&关闭Word文档
    JS input 银行卡号格式转换
    解决H5在微信浏览器或QQ浏览器修改title的问题
    CSS
    Atom 编辑器使用和学习
    php的一个小坑,输出不了json_encode
    js 组合键监听ctrl + enter
    webpack3.0 环境搭建
    css 使表格随着内容自动适应宽度
    获取input光标的x和y轴
  • 原文地址:https://www.cnblogs.com/twodog/p/12140895.html
Copyright © 2020-2023  润新知