Created
April 9, 2021 17:23
-
-
Save ilkermanap/4debbf3be82b8be61166d797a6f9d46c to your computer and use it in GitHub Desktop.
c/c++ linked list and binary tree examples. node addition and traversing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
/** | |
* Tek yonlu liste tanimi | |
*/ | |
struct Node { | |
int data; | |
struct Node *next; | |
}; | |
/** | |
* Ikili agac tanimi, yeni eleman veriden | |
* buyukse saga, kucukse sola eklenecek | |
*/ | |
struct TreeNode { | |
int data; | |
struct TreeNode *left; | |
struct TreeNode *right; | |
}; | |
void printlist(struct Node *head) { | |
uint rank = 1; | |
struct Node *temp = head; | |
if (head) { | |
cout << rank << " : " << temp->data << endl; | |
} | |
while (temp->next != nullptr) { | |
rank++; | |
temp = temp->next; | |
cout << rank << " : " << temp->data << endl; | |
} | |
} | |
void add_node(int data, Node *head) { | |
struct Node *temp = head; | |
while (temp->next != nullptr) { | |
temp = temp->next; | |
} | |
struct Node *newnode = nullptr; | |
newnode = (Node *)malloc(sizeof(Node)); | |
newnode->data = data; | |
newnode->next = nullptr; | |
temp->next = newnode; | |
} | |
/** | |
* recursive node ekleme. | |
* verilen sayiyi node icindeki sayi ile karsilastirir | |
* kucukse sola, buyukse saga gidecek sekilde kendisini cagirir | |
* sol ya da sagda gidecek dal kalmadiginda, orada yeni node olusturur | |
*/ | |
TreeNode* add_treenode(int data, TreeNode *head) { | |
if(head == nullptr) { | |
head = (TreeNode *)malloc(sizeof(TreeNode)); | |
head->data = data; | |
head->left = nullptr; | |
head->right = nullptr; | |
return head; | |
} | |
if (data < head->data) { | |
head->left = add_treenode(data, head->left); | |
} | |
else { | |
head->right = add_treenode(data, head->right); | |
} | |
return head; | |
} | |
void traverse(TreeNode *head, int order) { // 0 ascending, 1 descending | |
if (head == nullptr) return; | |
if (order == 0) | |
{ | |
traverse(head->left, order); | |
cout << head->data << " "; | |
traverse(head->right, order); | |
} | |
else if (order == 1) { | |
traverse(head->right, order); | |
cout << head->data << " "; | |
traverse(head->left, order); | |
} | |
} | |
int main() | |
{ | |
struct Node *head = nullptr; | |
struct Node *newnode = nullptr; | |
head = (Node *)malloc(sizeof(Node)); | |
head->data = 1; | |
head->next = nullptr; | |
add_node(2, head); | |
add_node(4, head); | |
add_node(6, head); | |
add_node(8, head); | |
add_node(11, head); | |
add_node(3, head); | |
//printlist(head); | |
struct TreeNode *treehead = nullptr; | |
treehead = add_treenode(500, treehead); | |
add_treenode(350, treehead); | |
add_treenode(550, treehead); | |
add_treenode(2345, treehead); | |
add_treenode(32, treehead); | |
add_treenode(10, treehead); | |
add_treenode(3150, treehead); | |
add_treenode(3530, treehead); | |
add_treenode(12, treehead); | |
add_treenode(124, treehead); | |
add_treenode(1, treehead); | |
add_treenode(2, treehead); | |
add_treenode(122350, treehead); | |
traverse(treehead,1); | |
cout << endl; | |
traverse(treehead,0); | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment