Pertemuan 06 Data Structure, Binary Search Tree (L).
Materi :
- Binary Search Tree
- Binary Search Tree Operations.
- Binary Search Tree
- Binary Search Tree Operations.
Binary Tree Search Tree
Binary Search tree is one kind of data structures that supports faster searching, rapid sorting, and easy insertion & deletion. Binary Tree has three basic operations which consist of find, insert and remove.
Binary Search tree is one kind of data structures that supports faster searching, rapid sorting, and easy insertion & deletion. Binary Tree has three basic operations which consist of find, insert and remove.
- Find
Begins at root, recursively decided (if X is less than root's key then goes left, if X is more than root's key then goes right, if X equals root's key than it is found.
- Insert
Begins at root. If X is less than node's key insert into left sub tree. if X is more than node's key, insert into right sub tree. operation is done recursively.
- Remove
If the key that we want to remove is just a leaf, we just need to delete that node. Well, it starts getting complicated when we need to delete a node which has one child. In this case we need to delete the node an connect it's child to it's parent. When we want to remove a data with two children or more, it need to be done recursively.
I've studied some code for today, which is a simple insertion for binary tree.
#include<stdio.h>
#include<stdlib.h>
struct node{
int key;
struct node *left, *right;
};
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
struct node* insert(struct node* node, int key){
if (node == NULL)return newNode(key);
if (key < node->key)node->left = insert(node->left, key);
else node->right = insert(node->right, key);
return node;
}
Comments
Post a Comment