This repository was archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.c
120 lines (101 loc) · 2.62 KB
/
binarytree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "stack.c"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//not complete
typedef struct tnode //tree node
{
int data; // expected args for c_asm function
char *token;
struct tnode *left;
struct tnode *right;
//int (*f)(const struct tnode *);
int (*f)(int);
} Node;
typedef struct bstree
{
Node *root;
} BSTree;
void print_x(const struct tnode *p)
{
printf("inside");
}
Node *createNode(int data, char *name);
Node *findNode(Node* head, char *name);
// function type
// typedef int (*foo)(int);
// foo functions[] = {&push, &pop};
void bindNode(Node* leaf, int (*f)(int))
{
// Tree.root->f = push;
// Tree.root->f(&Tree.root);
// printf("%d f id\n", f);
leaf->f = f;
//leaf->f(99); //pushed 99
//f(1);
}
void callNode(Node *leaf, int (*f)(int), int data)
{
leaf->f(data);
}
void callNodeByName(char* name, int data)
{
//leaf->f(data);
}
int TreeInit()
{
//Trivial (sorted) binary tree.
BSTree Tree;
Tree.root = createNode(1, "jump");
Tree.root->right = createNode(0, "print");
Tree.root->left = createNode(0, "dup");
Tree.root->right->right = createNode(1, "push");
Tree.root->right->left = createNode(0, "pop");
Tree.root->left->right = createNode(1, "ifeq");
Tree.root->left->left = createNode(0, "add");
// Can combine the steps but noo need since the tree is not long.
bindNode(Tree.root, jump);
bindNode(Tree.root->right, print);
bindNode(Tree.root->left, dup);
bindNode(Tree.root->right->right, push);
bindNode(Tree.root->right->left, pop);
bindNode(Tree.root->left->right, ifeq);
bindNode(Tree.root->left->left, add);
printf("%s", Tree.root->token);
return 0;
}
Node *findNode(Node *leaf, char *name) {
if (leaf == NULL)
{
return leaf;
}
if (leaf->token == name)
{
// Found the node.
return leaf; // Base case (can combine with above if-statement).
}
if (name < leaf->token) //make a strcmp !!!!!!!!!!!!!!!!!!!!
{
// Check the left subtree.
return findNode(leaf->left, leaf->token);
}
else
{
// Check the right subtree.
return findNode(leaf->right, leaf->token);
}
}
Node *createNode(int data, char *name)
{
Node *newNode = (Node *)malloc(sizeof(Node));
//char *token = (char *)malloc(sizeof(name));
if (newNode != NULL)
{
newNode->token = malloc(strlen(name) + 1);
strcpy(newNode->token, name);
//printf("%s", newNode->token);
newNode->data = data;
newNode->left = newNode->right = NULL;
}
return newNode;
}