zoukankan      html  css  js  c++  java
  • 简单的最小二叉树排序

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int TType;
    
    typedef struct _TreeNode
    {
    	TType elem;
    	struct _TreeNode *left, *right, *parent;
    }TreeNode;
    
    void Insert_BST(TreeNode *T, TType e)
    {
    	TreeNode *s=(TreeNode *)malloc(sizeof(TreeNode));
    	s->elem=e;
    	s->left=NULL;
    	s->right=NULL;
    	s->parent=NULL;
    
    	TreeNode *p=T, *f;
    	while(p!=NULL)
    	{
    		if(e<p->elem)
    		{
    			f=p;
    			p=p->left;
    		}
    		else
    		{
    			f=p;
    			p=p->right;
    		}
    	}
    	if(e<f->elem) f->left=s;
    	else
    		f->right=s;
    	
    }
    
    void InOrder(TreeNode *T, TType *H, int *pi)
    {
    	if(NULL==T)
    	{
    		return;
    	}
    	else
    	{
    		InOrder(T->left,H,pi);
    		H[*pi-1]=T->elem;		
    		*pi+=1;
    		InOrder(T->right,H,pi);
    	}
    }
    
    void BSTSort(TType *H, int len)
    {	
    	int i;
    	TreeNode *T=(TreeNode *)malloc(sizeof(TreeNode));
    	T->elem=H[0];
    	T->left=NULL;
    	T->right=NULL;
    	T->parent=NULL;
    	
    	for (i=2;i<=len;++i)
    	{
    		Insert_BST(T,H[i-1]);
    	}
    	i=1;
    	InOrder(T,H,&i);
    }
    
    int main()
    {
    	TType H[]={3,2,5,7,1,3,4,6,7,0};
    	int i;
    	BSTSort(H,10);
    	for (i=0;i<10;++i)
    	{
    		printf("%d ",H[i]);
    	}
    	printf("
    ");
    }
    

      

  • 相关阅读:
    利用requests, beautifulsoup包爬取股票信息网站
    Mac自带编码转换工具iconv
    Flask 快速入门
    HTML模版组件
    JavaScript正则表达式及jQuery回顾
    jQuery 教程
    Document
    Document
    Document
    Document
  • 原文地址:https://www.cnblogs.com/ledao/p/3365901.html
Copyright © 2011-2022 走看看