zoukankan      html  css  js  c++  java
  • 二叉树排序

    二叉树排序

    // 二叉树排序.cpp : 定义控制台应用程序的入口点。

    // #include "stdafx.h" #include<stdio.h> #include<stdlib.h> #include<windows.h> struct data{ int num; struct data* lbaby, *rbaby; }*root, *tree, *leaf; void find(int,struct data*); void output(struct data*); void main() { int data[10] = {75,23,98,44,57,12,29,64,38,82}; int i; printf(" <<Binary tree sort>> "); printf(" Number:"); for (i = 0; i < 10; i++) printf("%d ",data[i]); puts(""); for (i = 0; i < 60; i++) printf("-"); root = (struct data*)malloc(sizeof(struct data)); root->num = data[0]; //建立树根 root->lbaby = NULL; root->rbaby = NULL; printf(" Access:"); output(root); leaf = (struct data*)malloc(sizeof(struct data)); for (i = 1; i < 10; i++) //建立树枝 { leaf->num = data[i]; leaf->lbaby = NULL; leaf->rbaby = NULL; find(leaf->num,root); if (leaf->num>tree->num)//假设比父节点大,则放右子树 tree->rbaby = leaf; else //否则放在左子树 tree->lbaby = leaf; printf(" Access:"); output(root); leaf = (struct data*)malloc(sizeof(struct data)); } puts(""); for (i = 0; i < 60; i++) printf("-"); printf(" Sorting:"); output(root); printf(" "); system("pause"); } /*寻找新节点存放的位置*/ void find(int input, struct data* papa) { if ((input>papa->num) && (papa->rbaby != NULL)) find(input, papa->rbaby); else if ((input < papa->num) && (papa->lbaby != NULL)) find(input, papa->lbaby); else tree = papa; } /*输出数据*/ void output(struct data* node) { if (node != NULL) { output(node->lbaby); printf("%d ",node->num); output(node->rbaby); } }

    測试1:

  • 相关阅读:
    Angular中文api
    Angular各版本和组件下载
    判断一个浏览器是否支持opacity
    查找函数参数名称
    运行时代码求值
    简单的动画引擎
    利用闭包特性改写addEventListener的回调函数
    springboot指定redis库编号配置实现
    springboot获取IOC(ApplicationContext)实例
    ajax-springMVC提交表单的方式
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7281550.html
Copyright © 2011-2022 走看看