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

  • 相关阅读:
    【GO】文件二进制读取
    【JAVA】IO FileInputStream 读取二进制文件
    【JAVA】Maven 常用命令
    【JAVA】java8 function apply() compose() andThen()
    【Java】Maven resoucese 目录文件获取
    【数据结构】线索二叉树
    【Java】Java中的null作为方法参数是被当做值传递
    【Python】《Effective Python》 读书笔记 (一)
    【Python】一个try/except/else/finally 组合使用的例子
    【数据结构】二叉树的创建与遍历
  • 原文地址:https://www.cnblogs.com/twodog/p/12140895.html
Copyright © 2011-2022 走看看