作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 c 编写一个二叉搜索树,并且在函数的声明和头文件中,我不断收到愚蠢的“错误:在 '*' 标记之前预期 ')' 消息。
主文件
#include <stdlib.h>
#include <stdio.h>
#include "func.h"
#define ARRAYSIZE 12
int main(int argc, char *argv[3]) {
typedef struct node {
int value;
struct node * left;
struct node * right;
} * node;
node* root = NULL;
int nodes[ARRAYSIZE] = {3,6,2,1,7,8,3,5,7,2,9,4};
int i;
for(i = 0; i < ARRAYSIZE; i++) {
root = insert(root, nodes[i]);
}
if (strcmp(argv[1], "q") == 0) quit();
if (strcmp(argv[1], "i") == 0) insert(argv[2]);
if (strcmp(argv[1], "d") == 0) delete(argv[2]);
if (strcmp(argv[1], "s") == 0) search(argv[2]);
if (strcmp(argv[1], "e") == 0) empty();
return 0;
}
函数文件
#include "func.h"
node * createNode(int value) {
node * new_node = (node*)malloc(sizeof(node));
if(new_node == NULL) {
exit(1);
}
new_node->value = value;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
void insert(node * root, int n){
if(root == NULL) {
root = createNode(n);
}
else {
int left = 0;
node * current = root;
node * previous = NULL;
while(current != NULL){
previous = current;
if((n == current->n) < 0) {
left = 1;
current = current->left;
}
else if((n == current->n) > 0) {
left = 0;
current = current->right;
}
}
if(left)
previous->left = createNode(n);
else
previous->right = createNode(n);
}
}
void delete(node * root, int n){
node *current, *parent, *successor, *presuccessor, q;
if (root->left == NULL) {
printf("\ntree is empty! (deleting)");
}
parent = root;
current = root->left;
while (current !=NULL && n != current->value) {
parent = current;
current = (n next) ? current->left : current->right;
/* maybe add the } here ;) */
if (current == NULL) {
printf("\n %d is missing \n", n);
}
// Item found, now delete it
if (current->left == NULL)
q = current->right;
else if(current->right == NULL)
q = cur->left;
else {
// Obtain the inorder successor and its parent
presuccessor = current;
current = current->left;
while (successor->left != NULL) {
presuccessor = successor;
successor = successor->left;
}
if (current == presuccessor) { /*situation 1*/
successor->left = current->right;
}
else { /*situation 2*/
successor->left = current->left;
presuccessor->left = successor->right;
successor->right = current->right;
}
q = successor;
}
if (parent->left == current)
parent->left = q;
else
parent->right = q;
freeNode(current);
}
void search(node * root, int n){
if (root == NULL){
return NULL;
}
node * current = root;
while (current != null){
if (current->value > n) {
current = current->left;
}
else if (current->value < n){
current = current->right;
}
else
printf("%d is present", n)
}
printf("n is missing");
}
void empty(node * root){
if(root != NULL) {
empty(root->left);
empty(root->right);
free(root);
}
}
/* Print the value at each node with a single space character */
/* Else if tree is empty print "tree is empty" */
/* Do this for the following functions */
/*void inTraversal(node * root){
if node == null
return;
inTraversal(node.left)
visit(node)
inorder(node.right)
}
void preTraversal(){
preorder(node)
if node == null then return
visit(node)
preorder(node.left)
preorder(node.right)
}
void postTraversal(){
postorder(node)
if node == null then return
postorder(node.left)
postorder(node.right)
visit(node)
}*/
int quit() {
exit(1);
return 0;
}
标题
#ifndef FUNC_H
#define FUNC_H
node * createNode(int value);
void insert(node * root, int n);
void search(node * root, int n);
void empty(node * root);
void delete(node * root, int n);
int quit();
#endif
最佳答案
大概是这样的:
node * createNode(int value);
节点
此时未知。
将 node
的定义移至头文件中。
关于C - 二分搜索树中 ")"之前应为 "*",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22746395/
我正在尝试编写一个程序,在名为 items 的数组中进行顺序搜索和二分搜索,该数组具有 10000 个已排序的随机 int 值。第二个名为 targets 的数组加载了 1000 个 int 值(50
当我尝试使用图表并为其编写一些代码但没有成功时,我遇到了一个问题:/!! 我想创建一些东西来获取图形数据并检查它是否:1- 连接2-二分法3-有循环4-是一棵树 所以我想知道,例如,是否可以将其写入以
我是一名优秀的程序员,十分优秀!