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:

  • 相关阅读:
    缺失的第一个正数
    tuple用法
    整数转罗马数字
    三种时间格式的转换
    不同包的调用
    正则表达式
    lgb模板
    线性回归
    时间序列的特征
    3D聚类
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7281550.html
Copyright © 2011-2022 走看看