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


    // 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

  • 相关阅读:
    Sqlserver 2008:sp_msforeachdb 坑爹的错误陷阱
    安装官方 Synaptics 驱动,终于解决 HP Pavilion G4 笔记本 讨厌的触摸板锁定问题!
    Sqlserver 2008+:变更数据捕获(CDC) 和 更改跟踪(CT)
    PHP.ini 性能优化
    PHP Notice: Undefined index: ... 问题的解决方案
    查询类方法代码分析
    页面跳转方法总结大全
    如何关闭searchIndexer.exe进程
    如何正确运用PHP ini_set函数
    PHP中的串行化变量和序列化对象
  • 原文地址:https://www.cnblogs.com/twodog/p/12140896.html
Copyright © 2020-2023  润新知