二叉树由一组称为节点的互连元素组成。起始节点是树的基础,称为根节点。每个节点通常包含一个数据项,以及指向两个子节点,左节点和右节点的指针。如果任一子节点不存在,则相应的指针为NULL
。
节点可以包括计数器,该计数器记录它包含的数据项的重复数。这是一个结构的示例,它定义了存储long
类型整数的节点:
typedef struct Node Node;
struct Node
{
long item; // 数据项
int count; // 项目的副本数量
Node *pLeft; // 指向左节点
Node *pRight; // 指向右节点
};
示例代码
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct Node Node;
// 定义二叉树中的节点排序整数
struct Node
{
long item; // The data item
int count; // Number of copies of item
Node *pLeft; // Pointer to left node
Node *pRight; // Pointer to right node
};
// 函数原型
Node *create_node(long value); // Create a tree node
Node *add_node(long value, Node *pNode); // Insert a new node
void list_nodes(Node *pNode); // List all nodes
void free_nodes(Node *pNode); // Release memory
int main(void){
long newvalue = 0;
Node *pRoot = NULL;
char answer = 'n';
do
{
printf_s("Enter the node value: ");
scanf_s(" %ld", &newvalue);
if (!pRoot)
pRoot = create_node(newvalue);
else
add_node(newvalue, pRoot);
printf_s("Do you want to enter another (y or n)? ");
scanf_s(" %c", &answer, sizeof(answer));
} while (tolower(answer) == 'y');
printf_s("The values in ascending sequence are:\n");
list_nodes(pRoot); // Output the contents of the tree
free_nodes(pRoot); // Release the heap memory
return 0;
}
// 创建二叉树节点
Node *create_node(long value)
{
Node *pNode = (Node*)malloc(sizeof(Node));
pNode->item = value; // Set the value
pNode->count = 1; // Set the count
pNode->pLeft = pNode->pRight = NULL; // No left or right nodes
return pNode;
}
// 向树中添加新节点
Node *add_node(long value, Node *pNode)
{
if (!pNode) // If there's no node
return create_node(value); // create one and return it
if (value == pNode->item)
{ // Value equals current node
++pNode->count; // so increment count
return pNode; // and return the same node
}
if (value < pNode->item)
{ // Less than current node
if (!pNode->pLeft) // and no left node
{
pNode->pLeft = create_node(value); // so create a left node
return pNode->pLeft; // and return it.
}
else // There is a left node
return add_node(value, pNode->pLeft); // so add value via left node
}
else
{ // Greater than current node
if (!pNode->pRight)
{ // but no right node
pNode->pRight = create_node(value); // so create one
return pNode->pRight; // and return it.
}
else // There is a right node
return add_node(value, pNode->pRight); // so add to that.
}
}
// 按升序列出节点值
void list_nodes(Node *pNode)
{
if (pNode->pLeft)
list_nodes(pNode->pLeft);
printf_s("%10d x %10ld\n", pNode->count, pNode->item);
if (pNode->pRight)
list_nodes(pNode->pRight);
}
// 释放分配给节点的内存
void free_nodes(Node *pNode)
{
if (!pNode) // If there's no node
return; // we are done and return.
if (pNode->pLeft) // If there's a left sub-tree
free_nodes(pNode->pLeft); // free memory for those nodes.
if (pNode->pRight) // If there's a right sub-tree
free_nodes(pNode->pRight); // free memory for those nodes.
free(pNode); // Free current node memory
}
执行上面示例代码,得到以下结果:
Enter the node value: 123
Do you want to enter another (y or n)? y
Enter the node value: 234
Do you want to enter another (y or n)? y
Enter the node value: 345
Do you want to enter another (y or n)? y
Enter the node value: 456
Do you want to enter another (y or n)? n
The values in ascending sequence are:
1 x 123
1 x 234
1 x 345
1 x 456