mirror of
https://github.com/opus-tango/DSA-reference.git
synced 2026-03-20 12:05:24 +00:00
add node struct to c binary tree
This commit is contained in:
47
data-structures/trees/binary-tree/c/binary-tree.c
Normal file
47
data-structures/trees/binary-tree/c/binary-tree.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "binary-tree.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
Node* create_node(int data){
|
||||||
|
Node* node = malloc(sizeof(Node));
|
||||||
|
node->data = data;
|
||||||
|
node->left = NULL;
|
||||||
|
node->right = NULL;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_node(Node* node){
|
||||||
|
if (node->left != NULL) {
|
||||||
|
free_node(node->left);
|
||||||
|
}
|
||||||
|
if (node->right != NULL){
|
||||||
|
free_node(node->right);
|
||||||
|
}
|
||||||
|
free(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_node(Node* node){
|
||||||
|
if (node->right != NULL) {
|
||||||
|
printf("-");
|
||||||
|
print_node(node->right);
|
||||||
|
}
|
||||||
|
printf("%d", node->data);
|
||||||
|
if (node->left != NULL) {
|
||||||
|
printf("-");
|
||||||
|
print_node(node->left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* get_left(Node* node){
|
||||||
|
return node->left;
|
||||||
|
}
|
||||||
|
Node* get_right(Node* node){
|
||||||
|
return node->right;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_left(Node* parent, Node* child){
|
||||||
|
parent->left = child;
|
||||||
|
}
|
||||||
|
void set_right(Node* parent, Node* child){
|
||||||
|
parent->right = child;
|
||||||
|
}
|
||||||
25
data-structures/trees/binary-tree/c/binary-tree.h
Normal file
25
data-structures/trees/binary-tree/c/binary-tree.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef BINARY_TREE_H
|
||||||
|
#define BINARY_TREE_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct Node {
|
||||||
|
int data;
|
||||||
|
struct Node* left;
|
||||||
|
struct Node* right;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
Node* create_node(int data);
|
||||||
|
|
||||||
|
void free_node(Node* node);
|
||||||
|
|
||||||
|
void print_node(Node* node);
|
||||||
|
|
||||||
|
Node* get_left(Node* node);
|
||||||
|
Node* get_right(Node* node);
|
||||||
|
|
||||||
|
void set_left(Node* parent, Node* child);
|
||||||
|
void set_right(Node* parent, Node* child);
|
||||||
Reference in New Issue
Block a user