当前位置: 首页 > news >正文

网站制作_做网站_耐思智慧nba最新交易新闻

网站制作_做网站_耐思智慧,nba最新交易新闻,淘宝客网站哪里可以做,wordpress模板导航栏二叉搜索树: 非线性的,树是层级结构。基本单位是节点,每个节点最多2个子节点。有序。每个节点,其左子节点都比它小,其右子节点都比它大。每个子树都是一个二叉搜索树。每个节点及其所有子节点形成子树。可以是空树。 …

二叉搜索树:

  • 非线性的,树是层级结构。
  • 基本单位是节点,每个节点最多2个子节点。
  • 有序。每个节点,其左子节点都比它小,其右子节点都比它大。
  • 每个子树都是一个二叉搜索树。每个节点及其所有子节点形成子树。
  • 可以是空树。

C语言实现:(使用链表实现,不使用递归)

 创建结构体数据类型(记录二叉搜索树的根节点和数据个数):

typedef struct Link
{LinkNode *root;            // 根节点int length;                // 统计有多少数据
} LinkBST;                     // 别名

创建二叉搜索树,并初始化:

LinkBST bst;
bst.root = NULL;    // 根节点,初始化为NULL
bst.length = 0;     // 数据个数,初始化为0


创建节点(结构体数据类型),并创建具体节点实例的函数:

// 节点(结构体数据类型)
typedef struct Node
{int value;                // 数据类型为整型struct Node *left;        // 左子节点struct Node *right;       // 右子节点
} LinkNode;                   // 别名
// 函数:创建节点
LinkNode *createNode(int data)
{LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));    // 分配节点内存空间if(node == NULL){perror("Memory allocation failed");exit(-1);}node->value = data;    // 数据node->left = NULL;     // 左子节点,初始化为NULLnode->right = NULL;    // 右子节点,初始化为NULLreturn node;
}


添加元素:

从根节点开始,比对数值。若比它小,往左子树比对;若比它大,往右子树比对;直到找到为空,则为新元素的位置。

void add(LinkBST *bst, int data)	// add a element to the tree
{LinkNode *newNode = createNode(data);// 若是空树,根节点就是新节点if(bst->root == NULL){bst->root = newNode;bst->length++;return ;}// 非空树,比根节点数值小,往左边比对,比根节点数值大,往右边比对LinkNode *cur = bst->root;while(1){if(data == cur->value) return ;if(data < cur->value){if(cur->left == NULL){cur->left = newNode;bst->length++;return ;}cur = cur->left;}else if(data > cur->value){if(cur->right == NULL){cur->right = newNode;bst->length++;return ;}cur = cur->right;}}
}

删除元素:

  • 若删除的节点为叶子节点(即无子节点),则直接删除。
  • 若删除的节点只有左子节点,则左子节点替代删除节点。
  • 若删除的节点只有右子节点,则右子节点替代删除节点。
  • 若删除的节点既有左子节点又有右子节点,则找到直接前驱(即删除节点的左子树中的最大值,即删除节点的左子节点的最右节点),直接前驱的值替代删除节点的值,删除直接前驱节点。
void delete(LinkBST *bst,int data)	// delete a element from the tree
{// 函数:删除节点的具体操作LinkNode *del(LinkNode *node){// 只有右子节点,右子节点替代删除节点if(node->left == NULL){bst->length--;return node->right;}// 只有左子节点,左子节点替代删除节点if(node->right == NULL){bst->length--;return node->left;}// 左右子节点都有,直接前驱(左子节点的最右节点,即左子树中最大值)替代删除节点,删除直接前驱if(node->left && node->right){LinkNode *tmp = node, *cur = node->left;while(cur->right){tmp = cur;cur = cur->right;}node->value = cur->value;bst->length--;if(tmp != node) tmp->right = cur->left;else tmp->left = cur->left;return node;}}// 函数:找到删除节点void delNode(int data){LinkNode *parent, *cur = bst->root;while(1){if(cur == NULL) return ;if(data == cur->value){// 删除节点若是根节点,根节点接收删除后的节点if(cur == bst->root) bst->root = del(cur);// 删除节点若是左子节点,父节点的左子节点接收删除后的节点else if(data < parent->value) parent->left = del(cur);// 删除节点若是右子节点,父节点的右子节点接收删除后的节点else if(data > parent->value) parent->right = del(cur);return ;}if(data < cur->value){parent = cur;cur = cur->left;}else if(data > cur->value){parent = cur;cur = cur->right;}}}// 空树,直接退出程序if(bst->root == NULL) return ;delNode(data);
}

遍历元素:

前序遍历:(顺序:根节点、左子节点、右子节点)

使用数组实现栈(后进先出),数量:一个栈。

1、起始栈中元素为根节点。2、栈中元素依次出栈(并打印),找到元素的右节点和左节点依次入栈(注意:先右后左)。3、重复2,直到栈为空。

void pretraverse(LinkBST *bst)	// show element one by one,(root,left,right)
{LinkNode *cur = NULL;// 指针数组(数组元素是指针),实现栈(后进先出)LinkNode *arr[bst->length];int n = 1;arr[n-1] = bst->root;printf("pretravel: ");while(n != 0){cur = arr[n-1];printf("%d  ", cur->value);n--;if(cur->right){arr[n] = cur->right;n++;}if(cur->left){arr[n] = cur->left;n++;}}printf("\n");
}

中序遍历:(顺序:左子节点、根节点、右子节点)

使用数组实现栈(后进先出),数量:一个栈。

1、从根节点开始遍历,根节点入栈。2、找左节点依次入栈,找到最左节点后,栈中元素依次出栈(并打印),找右节点入栈。3、重复2,直到节点不存在或者栈为空。

void midtraverse(LinkBST *bst)	// show element one by one,(left,root,right)
{printf("midtravel: ");LinkNode *cur = bst->root;// 指针数组(数组元素是指针),实现栈(后进先出)LinkNode *arr[bst->length];int n = 0;while(cur || n != 0){if(cur){arr[n] = cur;n++;cur = cur->left;}else{cur = arr[n-1];printf("%d  ", cur->value);n--;cur = cur->right;}}printf("\n");
}

后序遍历:(顺序:左子节点、右子节点、根节点)

使用数组实现栈(后进先出),数量:两个栈(辅助栈,目标栈)。

1、辅助栈中起始元素为根节点。2、辅助栈中元素依次出栈(并入栈目标栈),找到元素的左节点和右节点依次入栈辅助栈(注意:先左后右)。3、重复2,直到辅助栈为空。4、遍历目标栈,并打印。

void posttraverse(LinkBST *bst)	// show element one by one,(left,right,root)
{LinkNode *cur = NULL;// 指针数组(数组元素是指针),实现栈(后进先出)LinkNode *arr[bst->length];    // 辅助栈LinkNode *brr[bst->length];    // 目标栈int n = 1, m = 0;arr[n-1] = bst->root;while(n != 0){	cur = brr[m] = arr[n-1];    // 辅助栈出栈,目标栈入栈n--;m++;if(cur->left){arr[n] = cur->left;    // 辅助栈入栈n++;}if(cur->right){arr[n] = cur->right;    // 辅助栈入栈n++;}}// 遍历目标栈printf("posttravel: ");for(int i = m - 1; i >= 0; i--){printf("%d  ", brr[i]->value);}printf("\n");
}

广度遍历(层级遍历):

使用链表实现队列(先进先出),数量:一个队列。

1、队列中起始元素为根节点。2、队列中元素依次从队头出队(并打印),找到元素的左节点和右节点依次从队尾入队(注意:先左后右)。3、重复2,直到队列为空。

void breadthtraverse(LinkBST *bst)	// show element one by one,(levels)
{printf("threadtravel: ");// 链表:实现队列(先进先出),注:链表的函数在bstqueue.c(完整代码中展示)Queue queue;queue.header = createQnode(bst->root);    // 头指针,指向第一个元素queue.tail = NULL;                        // 尾指针,指向最后一个元素LinkNode *cur = NULL;while(queue.header){cur = queue.header->bstnode;printf("%d  ", cur->value);popQnode(&queue);                    // 从队头出队(函数在bstqueue.c)if(cur->left){addQnode(&queue, cur->left);    // 从队尾入队(函数在bstqueue.c)}if(cur->right){addQnode(&queue, cur->right);    // 从队尾入队(函数在bstqueue.c)}}printf("\n");
}

查找元素:

从根节点开始,比对数值。若比它小,往左子树查找;若比它大,往右子树查找;直到找到该元素,则返回1(true),若没有,则返回0(false)。

int find(LinkNode *node, int data)	// if find data,return 1(true),or return 0(false)
{LinkNode *cur = node;while(cur){if(data == cur->value) return 1;if(data < cur->value) cur = cur->left;else if(data > cur->value) cur = cur->right;}return 0;
}

完整代码:(bstree.c,bstqueue.c(链表实现的队列,用于广度遍历))

// bstree.c
#include <stdio.h>
#include <stdlib.h>
#include "bstqueue.c"    // 引入链表实现的队列,用于广度遍历/* structure */
typedef struct Node		// node of the binary search tree(bst)
{int value;		    // data type is integerstruct Node *left;	// left child nodestruct Node *right;	// right child node
} LinkNode;typedef struct Link		//bst(Linkedlist)
{LinkNode *root;		// root nodeint length;		    // the number of the tree
} LinkBST;/* function prototype */
void add(LinkBST *, int);	    // add a element
void delete(LinkBST *,int);	    // delete a element
void pretraverse(LinkBST *);	// show element one by one,(root,left,right)
void midtraverse(LinkBST *);	// show element one by one,(left,root,right)
void posttraverse(LinkBST *);	// show element one by one,(left,right,root)
void breadthtraverse(LinkBST *);	// show element one by one,(levels)
int find(LinkNode *, int);	    // if find data,return 1(true),or return 0(false)/* main function */
int main(void)
{// create binary search tree and initializationLinkBST bst;bst.root = NULL;bst.length = 0;printf("isempty(1:true, 0:false): %d, length is %d\n", bst.root==NULL, bst.length);add(&bst, 15);add(&bst, 8);add(&bst, 23);add(&bst, 19);add(&bst, 10);add(&bst, 6);add(&bst, 9);add(&bst, 12);printf("isempty(1:true, 0:false): %d, length is %d\n", bst.root==NULL, bst.length);pretraverse(&bst);midtraverse(&bst);posttraverse(&bst);breadthtraverse(&bst);printf("find 10(1:true, 0:false): %d\n", find(bst.root, 10));printf("find 11(1:true, 0:false): %d\n", find(bst.root, 11));delete(&bst, 23);delete(&bst, 15);delete(&bst, 6);printf("isempty(1:true, 0:false): %d, length is %d\n", bst.root==NULL, bst.length);pretraverse(&bst);midtraverse(&bst);posttraverse(&bst);breadthtraverse(&bst);return 0;
}/* subfunction */
LinkNode *createNode(int data)		// create a node of the binary search tree
{LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));if(node == NULL){perror("Memory allocation failed");exit(-1);}node->value = data;node->left = NULL;node->right = NULL;return node;
}void add(LinkBST *bst, int data)	// add a element to the tree
{LinkNode *newNode = createNode(data);// if empty, root is newNodeif(bst->root == NULL){bst->root = newNode;bst->length++;return ;}// if not empty, smaller,to left, biger,to rightLinkNode *cur = bst->root;while(1){if(data == cur->value) return ;if(data < cur->value){if(cur->left == NULL){cur->left = newNode;bst->length++;return ;}cur = cur->left;}else if(data > cur->value){if(cur->right == NULL){cur->right = newNode;bst->length++;return ;}cur = cur->right;}}
}void delete(LinkBST *bst,int data)	// delete a element from the tree
{// subfunction: delete the nodeLinkNode *del(LinkNode *node){// if only right child, return right child nodeif(node->left == NULL){bst->length--;return node->right;}// if only left child, return left child nodeif(node->right == NULL){bst->length--;return node->left;}// both left and right, the max on the left replace the delete node and delete itif(node->left && node->right){LinkNode *tmp = node, *cur = node->left;while(cur->right){tmp = cur;cur = cur->right;}node->value = cur->value;bst->length--;if(tmp != node) tmp->right = cur->left;else tmp->left = cur->left;return node;}}// subfunction: find the delete nodevoid delNode(int data){LinkNode *parent, *cur = bst->root;while(1){if(cur == NULL) return ;if(data == cur->value){// delete node is root,root receive the node after deleteif(cur == bst->root) bst->root = del(cur);// delete node is left,left child of parent receive the node after deleteelse if(data < parent->value) parent->left = del(cur);//delete node is right,right child of parent receive the node after deleteelse if(data > parent->value) parent->right = del(cur);return ;}if(data < cur->value){parent = cur;cur = cur->left;}else if(data > cur->value){parent = cur;cur = cur->right;}}}if(bst->root == NULL) return ;delNode(data);
}void pretraverse(LinkBST *bst)	// show element one by one,(root,left,right)
{LinkNode *cur = NULL;// pointer array(stack:LIFO): array, each element is a pointer(point to node)LinkNode *arr[bst->length];int n = 1;arr[n-1] = bst->root;printf("pretravel: ");while(n != 0){cur = arr[n-1];printf("%d  ", cur->value);n--;if(cur->right){arr[n] = cur->right;n++;}if(cur->left){arr[n] = cur->left;n++;}}printf("\n");
}void midtraverse(LinkBST *bst)	// show element one by one,(left,root,right)
{printf("midtravel: ");LinkNode *cur = bst->root;// pointer array(stack:LIFO): array, each element is a pointer(point to node)LinkNode *arr[bst->length];int n = 0;while(cur || n != 0){if(cur){arr[n] = cur;n++;cur = cur->left;}else{cur = arr[n-1];printf("%d  ", cur->value);n--;cur = cur->right;}}printf("\n");
}void posttraverse(LinkBST *bst)	// show element one by one,(left,right,root)
{LinkNode *cur = NULL;// pointer array(stack:LIFO): array, each element is a pointer(point to node)LinkNode *arr[bst->length];LinkNode *brr[bst->length];int n = 1, m = 0;arr[n-1] = bst->root;while(n != 0){	cur = brr[m] = arr[n-1];n--;m++;if(cur->left){arr[n] = cur->left;n++;}if(cur->right){arr[n] = cur->right;n++;}}printf("posttravel: ");for(int i = m - 1; i >= 0; i--){printf("%d  ", brr[i]->value);}printf("\n");
}void breadthtraverse(LinkBST *bst)	// show element one by one,(levels)
{printf("threadtravel: ");// queue(FIFO): use Linkedlist implementQueue queue;queue.header = createQnode(bst->root);queue.tail = NULL;LinkNode *cur = NULL;while(queue.header){cur = queue.header->bstnode;printf("%d  ", cur->value);popQnode(&queue);if(cur->left){addQnode(&queue, cur->left);}if(cur->right){addQnode(&queue, cur->right);}}printf("\n");
}int find(LinkNode *node, int data)	// if find data,return 1(true),or return 0(false)
{LinkNode *cur = node;while(cur){if(data == cur->value) return 1;if(data < cur->value) cur = cur->left;else if(data > cur->value) cur = cur->right;}return 0;
}
// bstqueue.c
#include <stdlib.h>/* structure */
typedef struct queueNode	// node of the queue
{void *bstnode;			// data type is bst nodestruct queueNode *next;	// point to next node
} Qnode;typedef struct queue		// queue(Linkedlist)
{Qnode *header;		    // point to the top nodeQnode *tail;		    // point to the last node
} Queue;/* subfunction */
Qnode *createQnode(void *bstnode)	// create a node of the queue
{Qnode *node = (Qnode *)malloc(sizeof(Qnode));if(node == NULL){perror("Memory allocation failed");exit(-1);}node->bstnode = bstnode;node->next = NULL;return node;
}void addQnode(Queue *queue, void *node)	// add a element to the end of the queue
{Qnode *qnode = createQnode(node);if(queue->tail == NULL) queue->tail = queue->header = qnode;else{queue->tail->next = qnode;queue->tail = qnode;}
}void popQnode(Queue *queue)		// delete a element from the top of the queue
{queue->header = queue->header->next;if(queue->header == NULL) queue->tail = NULL;
}

编译链接: gcc -o bstree bstree.c

执行可执行文件: ./bstree

http://www.hengruixuexiao.com/news/43645.html

相关文章:

  • 跨境电商那个网站做饰品比较好商丘关键词优化推广
  • 做网站前产品经理要了解什么市场调研报告怎么做
  • 网站开发和设计如何合作seo优化实训报告
  • 建网站的流程及注意事项廊坊自动seo
  • 辽icp备鞍山公司中企动力提供网站建设推广方案策划
  • ps怎么做网站设计天津百度爱采购
  • 重庆网络营销公司哪家好seo的关键词无需
  • 贵港网站建设动态英语培训
  • 简单的网站网络销售怎么找客户
  • 南昌市市政建设有限公司做seo推广一年大概的费用
  • 做网站中心网络推广代理平台
  • 网站推广的方法和途径刚刚传来最新消息
  • 百度网站站长工具网站建设策划方案
  • 高端网站建设jm3qaso安卓优化公司
  • 网站建设公司公司好最有效的广告宣传方式
  • 搜索引擎网站优化推广如何免费发布广告
  • 建站之星多语言深圳网站制作推广
  • 北京网站建设是什么意思东莞网站营销
  • 哪家专门做特卖的网站排名优化价格
  • 校庆网站建设策划书范文济南百度开户电话
  • 长春网站建设企业网购平台推广方案
  • 用花生棒自己做网站福州网站排名提升
  • 鞍山网站建设免费网站
  • 甘肃做高端网站的公司产品软文案例
  • 重庆网站建设解决方案百度网盘优化
  • 网站工程专业是什么关键词排名优化官网
  • 来年做那些网站致富推广网络广告
  • 做网站用什么团建投放广告怎么投放
  • 做怎么网站培训机构优化
  • 保险公司网站策划杭州seo推广排名稳定