Created
February 27, 2021 13:37
-
-
Save suvasish114/aaefebceb6ffac30b39d67cc4c646ff1 to your computer and use it in GitHub Desktop.
binary tree using array
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> | |
#define SIZE 10 | |
using namespace std; | |
char tree[SIZE]; | |
void root(char key); | |
void set_left(char key, int parent); | |
void set_right(char key, int parent); | |
void display(void); | |
int main(){ | |
root('A'); | |
set_left('B', 0); | |
set_right('C', 0); | |
set_left('D', 1); | |
set_right('E', 1); | |
set_right('M', 3); | |
display(); | |
return 0; | |
} | |
void root(char key){ | |
if(tree[0] != '\0') | |
cout<<"tree already had root"; | |
else | |
tree[0] = key; | |
} | |
void set_left(char key, int parent){ | |
if(tree[parent] == '\0') | |
cout<<"No parent found at "<<(parent*2)+1; | |
else | |
tree[(parent*2)+1] = key; | |
} | |
void set_right(char key, int parent){ | |
if(tree[parent] == '\0') | |
cout<<"No parent found at "<<(parent*2)+2; | |
else | |
tree[(parent*2)+2] = key; | |
} | |
void display(void){ | |
cout << "\n"; | |
for(int i = 0; i < 10; i++) | |
{ | |
if(tree[i] != '\0') | |
cout << tree[i]; | |
else | |
cout << "-"; | |
} | |
cout<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment