二叉树的各种创建方法

7 篇文章 0 订阅
5 篇文章 1 订阅

1.前序创建

#include<stdlib.h>
#include<malloc.h>
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef  char ElemType;
typedef struct  BtNode
{
	BtNode *leftchild;
	BtNode *rightchild;
	ElemType data;
}BtNode,*BinaryTree;
//购买结点
BtNode *Buynode()
{
	BtNode *p=(BtNode *)malloc(sizeof(BtNode));
	if(NULL==p)  exit(1);
	memset(p,0,sizeof(BtNode));
	return p;
}
void *Freenode(BtNode *ptr)
{
	free(ptr);
}
第一种建树方法(通过结点进行创建二叉树)
//方法一
//前序创建二叉树
void *PreCreateTree(BinaryTree  *ptr)
{
	char ch;
	cin>>ch;
	if(ch!='#'||ptr!=NULL)
        {
		*ptr=Buynode();
		(*ptr)->data=ch;
		PreCreateTree(&(*ptr)->leftchild);
		PreCreateTree(&(*ptr)->rightchild);
	}
}
int main()
{
	BinaryTree root=NULL;
	PreCreateTree(&root);
}
第二种创建方法:
//利用指针  
void Createtree4(BtNode ** const p,char *&str)  
{  
    if(  str != NULL && *str != '#')  
    {  
        (*p) = Buynode();  
        (*p)->data = *str;  
        Createtree4(&(*p)->leftchild,++str);  
        Createtree4(&(*p)->rightchild,++str);  
    }  
  
}
第三种用前序方法创建二叉树(通过传入空,进行创建)
BtNode *CreateTree()
{
	ElemType x;
	BtNode *s=NULL;
	cin>>x;
	if(x!=END)
	{
		s=Buynode();
		s->data=x;
		s->leftchild=CreateTree();
		s->rightchild=CreateTree();
	}
	return s;
}
第四种用前序创建二叉树,通过传入引用(如果不是传入的是引用,会出现问题)
//传引用建树  
BtNode *Createtree1(char *&str)  
{  
     BtNode *p = NULL;  
     if( str != NULL && *str != '#')  
     {  
         p = Buynode();  
         p->data = *str;  
         p->leftchild = Createtree1(++str);  
         p->rightchild = Createtree1(++str);  
     }  
     return p;  
       
}
第五种创建方法:
//引用转换成二级指针建树  
BtNode *Createtree2(char ** const str)  
{  
    BtNode *p = NULL;  
    if( str != NULL  && *str != NULL && **str != '#')  
    {  
        p = Buynode();  
        p->data = **str;  
        p->leftchild = Createtree1(++*str);  
        p->rightchild = Createtree1(++*str);  
    }  
    return p;  
  
}  
第六种创建方法:

//给一个结点建立左右孩子  
void Createtree3(BtNode *&p,char *&str)  
{  
    if(  str != NULL && *str != '#')  
    {  
        p = Buynode();  
        p->data = *str;  
        Createtree3(p->leftchild,++str);  
        Createtree3(p->rightchild,++str);  
    }  
}  

2.用前序和中序创建二叉树

方法一:

//用前序和中序创建二叉树
*BtNode* CreateTreePI(char ps[],char is[],int n )
{
	if(n<=0)
	{
		return NULL;
	}
	BtNode* p=(BtNode*)malloc(sizeof(BtNode));
	int i=0;
	int m;
	while(i<n)
	{
		if(ps[0]==is[i])
		{
			m=i;
			break;
		}
		++i;
	}
	if(i>=n)
	{
		return NULL;
	}
	p->data=ps[0];
	p->leftchild=CreateTreePI(ps+1,is,m);
	p->rightchild=CreateTreePI(ps+m+1,is+m+1,n-m-1);
	return p;
}
int main()
{
	char ps[]="ABDGHCEIF";
	char is[]="GDHBAEICF";
	int len=strlen(ps);
	CreateTreePI(ps,is,len);
}

方法二:

int Findvalue(char *is,ElemType p,int n)  
{  
     for(int i = 0;i < n;++i)  
     {  
         if(is[i] == p)  
             return i;  
     }  
     return -1;  
}  
//根据前序和中序建立一个二叉树  
BtNode *CreatePM(char *pr,char *is,int n)  
{  
     ElemType p = pr[0];  
     BtNode *tmp = NULL;  
     if(n > 0)  
     {  
         int m = Findvalue(is,p,n);  
         if(-1 == m) exit(1);  
         tmp  = Buynode();  
         tmp->data = p;  
         tmp->leftchild = CreatePM(pr+1,is,m);  
         tmp->rightchild = CreatePM(pr+m+1,is+m+1,n-m-1);  
      
     }  
     return tmp;  
}  
BtNode *CreatetreePM(char *pr,char *is,int n)  
{  
      if(pr == NULL || is == NULL || n < 1) return NULL;  
      return  CreatePM(pr,is,n);  
        
} 

3.用中序和后序创建二叉树

方法一:

//用中序和后序创建二叉树
BtNode* CreateTreeIL(char is[],char ls[],int n)
{
	if(n<=0)
	{
		return NULL;
	}
	BtNode *p=(BtNode*)malloc(sizeof(BtNode));
	int i=0;
	int m;
	while(i<n)
	{
		if(ls[n-1]==is[i])
		{
			m=i;
			break;
		}
		++i;
	}
	if(i>=n)
	{
		return NULL;
	}
	p->data=ls[n-1];
	p->leftchild= CreateTreeIL(is,ls,m);
	p->rightchild= CreateTreeIL(is+m+1,ls+m,n-m-1);
	return p;
}
int main()
{
	char is[]="GDHBAEICF";
	char ls[]="GHDBIEFCA";
	int len=strlen(is);
	CreateTreeIL(is,ls,len);
} 

方法二:

// 根据中序和后序建立一个二叉树  
BtNode *CreateML(char *mi,char *la,int n)  
{  
    ElemType p = la[n-1];  
    BtNode *tmp = NULL;  
    if(n > 0)  
    {  
        int m = Findvalue(mi,p,n);  
        if(-1 == m) exit(1);  
        tmp  = Buynode();  
        tmp->data = p;  
        tmp->leftchild = CreateML(mi,la,m);  
        tmp->rightchild = CreateML(mi+m+1,la+m,n-m-1);  
    }  
    return tmp;  
}  
BtNode *CreatetreeML(char *mi,char *la,int n)  
{  
    if(mi == NULL || la == NULL || n < 1) return NULL;  
    return  CreateML(mi,la,n);  
  
}

  • 76
    点赞
  • 475
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
二叉树是一种基本的数据结构,它由节点组成,每个节点最多有两个子节点。创建二叉树有两种常见的方法:递归法和非递归法。 1. 递归法创建二叉树 递归法是一种比较简单的创建二叉树方法,它基于如下思路: - 如果当前节点为空,则创建新节点并返回; - 如果当前节点不为空,则根据节点值的大小关系将新节点插入左子树或右子树。 下面是递归法创建二叉树的Python代码: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def insert(root, val): if not root: return TreeNode(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root ``` 2. 非递归法创建二叉树 非递归法也是一种创建二叉树的常见方法,它利用栈来实现。 - 初始时将根节点入栈; - 取出栈顶元素,如果其左子树为空,则将新节点插入其左子树;如果左子树不为空,则将其左子树入栈; - 取出栈顶元素,如果其右子树为空,则将新节点插入其右子树;如果右子树不为空,则将其右子树入栈; - 重复上述步骤,直到栈为空。 下面是非递归法创建二叉树的Python代码: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def insert(root, val): if not root: return TreeNode(val) stack = [root] while stack: node = stack.pop() if not node.left: node.left = TreeNode(val) return root elif not node.right: node.right = TreeNode(val) return root else: stack.append(node.left) stack.append(node.right) return root ``` 以上是二叉树创建方法的设计以及Python代码。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值