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

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct node
    {
    int data;
    node *lchild;
    node *rchild;
    }TreeNode;

    void InsertNode(TreeNode *&Node, int n)
    {
    if (Node == NULL)
    {
    Node = (TreeNode*)malloc(sizeof(TreeNode));
    Node->data = n;
    Node->lchild = Node->rchild = NULL;
    }
    else
    {
    if (Node->data > n)
    {
    InsertNode(Node->lchild, n);
    }
    else
    {
    InsertNode(Node->rchild, n);
    }
    }
    }
    //中序遍历
    void in(TreeNode *Node)
    {
    int top = -1;
    TreeNode *Stack[100];
    TreeNode *p = Node;
    do
    {
    while(p != NULL)
    {
    Stack[++top] = p;
    p = p->lchild;
    }
    printf("%d ", Stack[top]->data);
    p = Stack[top--];
    if (p != NULL)
    {
    p = p->rchild;
    }
    } while (top >= 0 || p != NULL);
    printf("\n");
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    TreeNode *Node = NULL;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
    int data;
    scanf("%d", &data);
    InsertNode(Node, data);
    }
    in(Node);
    return 0;
    }

  • 相关阅读:
    Windbg DUMP
    NET媒体文件操作组件TagLib
    NET Framework、.NET Core、Xamarin
    面向切面编程
    微服务
    NET Core
    Yeoman generator
    Service Fabric
    Vue.JS 2.x
    CoreCLR
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3022916.html
Copyright © 2011-2022 走看看