zoukankan      html  css  js  c++  java
  • 把二叉树转变为左孩子右兄弟树

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

  • 相关阅读:
    【前端异常】解决前端引入Bootstrap的dropdowns 菜单时报错,Uncaught TypeError: Bootstrap‘s dropdowns require Popper.js
    @MapperScan注解
    mongoRepository mongoTemplate
    Spring注解中@component
    正则表达式
    832. Flipping an Image
    Java---静态代码块、构造代码块、构造函数以及Java类初始化顺序[未完成]
    轻松学习正则表达式「转」
    771. Jewels and Stones
    java中的栈、堆、常量池和关于String类的理解
  • 原文地址:https://www.cnblogs.com/twodog/p/12140894.html
Copyright © 2011-2022 走看看