赞
踩
1)二叉树就是度不超过2的树,其每个结点最多有两个子结点
2)二叉树的结点分为左结点和右结点
- #include <stdio.h>
- #include <stdlib.h>
-
- struct Node {
- int data;
- struct Node* pLeft;
- struct Node* pRight;
- };
-
- //初始化树节点的函数
- struct Node* createNode(int data) {
- struct Node* newNode = malloc(sizeof(struct Node));
- if (newNode == NULL) return newNode;
- newNode->pLeft = NULL, newNode->pRight = NULL;
- newNode->data = data;
- return newNode;
- }
-
- //插入树节点函数
-
-
- void Insert(int data ,struct Node** root) {
- if (NULL == root) return;
- //如果是空树,直接变根节点
- if ( NULL== *root) {
- *root = createNode(data);
- return;
- }
- //
- if (data < (*root)->data) {
- Insert(data, &((*root)->pLeft));
- }
- else {
- Insert(data, &((*root)->pRight));
- }
-
-
- }
- //遍历树
- //先序遍历
- void preTravel(struct Node* pRoot) {
- if (NULL == pRoot) return;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。