二叉排序樹建立(遞迴)

來源:互聯網
上載者:User

標籤:lse   std   scanf   roo   return   tree   head   nod   scan   

#include<stdio.h>#include<stdlib.h>/*遞迴前中後遍曆*/typedef struct node{  int data;  struct node*left;  struct node*right;}BTnode;BTnode* CreateTree(BTnode* root,int x){if(!root)  //如果root結點為空白,建立葉子結點{root = (BTnode*)malloc(sizeof(BTnode));root->data = x;root->left=root->right=NULL;}else{if(root->data>x) root->left = CreateTree(root->left,x);  //遞迴調用左else if(root->data<x)root->right = CreateTree(root->right,x);//遞迴調用右}return root;}void Forder(BTnode*root){  if(root)  {  printf("%d",root->data);  printf("\n");  Forder(root->left);  Forder(root->right);  }}void Inorder(BTnode*root){  if(root)  {  Inorder(root->left);  printf("%3d",root->data);  printf("\n");  Inorder(root->right);  }}void Porder(BTnode*root){  if(root)  {  Porder(root->left);  Porder(root->right);  printf("%6d",root->data);  printf("\n");   }}int main(void){  BTnode * head = NULL; int x; int n; int i; printf("請輸入n="); scanf("%d",&n); printf("請輸入二叉樹的結點data\n"); for(i=0;i<n;i++) {   scanf("%d",&x);   head = CreateTree(head,x); }printf("..................\n"); Forder(head);printf("..................\n");Inorder(head);printf("..................\n");Porder(head);}


二叉排序樹建立(遞迴)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.