码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树简单实例

时间:2014-12-18 00:17:16      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:des   ar   io   sp   for   on   bs   ad   ef   

// ConsoleApplication2.cpp

//

 

#include "stdafx.h"

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

//Define the Node structure.

struct Node

{

         long item;

         unsigned int count;

         Node *pleft, *pright;

};

 

Node *create_node(long value);

Node *add_node(Node *proot, long value);

void print_node(Node *proot);

void free_node(Node *proot);

 

int _tmain(int argc, _TCHAR* argv[])

{

         Node *proot = NULL;;

         char tempc = ‘\0‘;

         long value = 0;

         while (1)

         {

                  printf("Do you want to go on? ");

                  scanf_s(" %c", &tempc, sizeof(char));

                  fflush(stdin);

                  if (tolower(tempc) == ‘n‘)

                          break;

                  printf("Enter the value: ");

                  scanf_s(" %ld", &value);

                  fflush(stdin);

                  if (proot == NULL)

                          proot = create_node(value);

                  else

                          add_node(proot, value);

         }

         print_node(proot);

         free_node(proot);

         system("pause");

         return 0;

}

//Create a new node.

Node *create_node(long value)

{

         Node *temp = (Node*)malloc(sizeof(Node));

         if (temp != NULL)

         {

                  temp->item = value;

                  temp->count = 1;

                  temp->pleft = NULL;

                  temp->pright = NULL;

         }

         return temp;

}

//Add new node to the root node.

Node *add_node(Node *proot, long value)

{

         if (proot == NULL)

                  return create_node(value);

         if (proot->item == value)

         {

                  ++proot->count;

                  return proot;

         }

         if (value < proot->item)

         {

                  if (proot->pleft == NULL)

                          return proot->pleft = create_node(value);

                  else

                          return add_node(proot->pleft, value);

         }

         else

         {

                  if (proot->pright == NULL)

                          return proot->pright = create_node(value);

                  else

                          return add_node(proot->pright, value);

         }

}

//Print the node values.

void print_node(Node *proot)

{

         for (int i = 0; i < proot->count; ++i)

                  printf("%d\t", proot->item);

         putchar(10);

         if (proot->pleft != NULL)

                  print_node(proot->pleft);

         if (proot->pright != NULL)

                  print_node(proot->pright);

}

//Free the memory storing nodes.

void free_node(Node *proot)

{

         if (proot->pleft != NULL)

                  free_node(proot->pleft);

         if (proot->pright != NULL)

                  free_node(proot->pright);

         free(proot);

}

二叉树简单实例

标签:des   ar   io   sp   for   on   bs   ad   ef   

原文地址:http://www.cnblogs.com/NewValue/p/4170665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!