为什么指针到指针在这个前卫被使用?

编程入门 行业动态 更新时间:2024-10-24 14:20:00
本文介绍了为什么指针到指针在这个前卫被使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的程序展示了如何在C程序中建立一个二叉树。它采用动态内存分配,指针和递归。二叉树是一种非常有用的数据结构,因为它允许在排序列表中插入效率,检索和删除。作为这样的树基本上是一个递归定义的结构,递归编程处理它的自然和有效的方式。

The following program shows how to build a binary tree in a C program. It uses dynamic memory allocation, pointers and recursion. A binary tree is a very useful data-structure, since it allows efficient insertion, searching and deletion in a sorted list. As such a tree is essentially a recursively defined structure, recursive programming is the natural and efficient way to handle it.

tree empty node left-branch right-branch left-branch tree right-branch tree

这里的code:

Here's the code:

#include <stdlib.h> #include <stdio.h> struct tree_el { int val; struct tree_el * right, * left; }; typedef struct tree_el node; void insert(node ** tree, node * item) { if(!(*tree)) { *tree = item; return; } if(item->val<(*tree)->val) insert(&(*tree)->left, item); else if(item->val>(*tree)->val) insert(&(*tree)->right, item); } void printout(node * tree) { if(tree->left) printout(tree->left); printf("%d\n",tree->val); if(tree->right) printout(tree->right); } void main() { node * curr, * root; int i; root = NULL; for(i=1;i<=10;i++) { curr = (node *)malloc(sizeof(node)); curr->left = curr->right = NULL; curr->val = rand(); insert(&root, curr); } printout(root); }

为什么指针到指针使用呢?

Why is pointer-to-pointer used?

推荐答案

由于插入方法需要修改树的根。

Because the insert method needs to modify the root of the tree.

更多推荐

为什么指针到指针在这个前卫被使用?

本文发布于:2023-06-01 10:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/412988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   前卫   在这个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!