Tree Implementation
Tree can be implemented in 2 ways;
- Tree implementation Using Link List in C++
- Tree implementation Using Arrays in C++
Program to implement Tree with Link List and Pointers in 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
#include <iostream> using namespace std; struct node{ int value; node *Left_Pointer; node *Right_Pointer; }; class Binary_Tree{ public: Binary_Tree(); ~Binary_Tree(); void Add(int key); node *search(int key); void Delete_tree(); void inorder_Display(); void postorder_Display(); void preorder_Display(); private: void Delete_tree(node *leaf); void Add(int key, node *leaf); node *search(int key, node *leaf); void inorder_Display(node *leaf); void postorder_Display(node *leaf); void preorder_Display(node *leaf); node *root; }; Binary_Tree::Binary_Tree(){ root = NULL; } Binary_Tree::~Binary_Tree(){ Delete_tree(); } void Binary_Tree::Delete_tree(node *leaf){ if(leaf != NULL){ Delete_tree(leaf->Left_Pointer); Delete_tree(leaf->Right_Pointer); delete leaf; } } void Binary_Tree::Add(int key, node *leaf){ if(key < leaf->value){ if(leaf->Left_Pointer != NULL){
Add(key, leaf->Left_Pointer); }else{ leaf->Left_Pointer = new node; leaf->Left_Pointer->value = key; leaf->Left_Pointer->Left_Pointer = NULL; leaf->Left_Pointer->Right_Pointer = NULL; } }else if(key >= leaf->value){ if(leaf->Right_Pointer != NULL){ Add(key, leaf->Right_Pointer); }else{ leaf->Right_Pointer = new node; leaf->Right_Pointer->value = key; leaf->Right_Pointer->Right_Pointer = NULL; leaf->Right_Pointer->Left_Pointer = NULL; } } } void Binary_Tree::Add(int key){ if(root != NULL){ Add(key, root); }else{ root = new node; root->value = key
; root->Left_Pointer = NULL; root->Right_Pointer = NULL; } } node *Binary_Tree::search(int key, node *leaf){ if(leaf != NULL){ if(key == leaf->value){ return leaf; } if(key < leaf->value){ return search(key, leaf->Left_Pointer); }else{ return search(key, leaf->Right_Pointer); } }else{ return NULL; } } node *Binary_Tree::search(int key){ return search(key, root); } void Binary_Tree::Delete_tree(){ Delete_tree(root); } void Binary_Tree::inorder_Display(){ inorder_Display(root); cout << "\n"; } void Binary_Tree::inorder_Display(node *leaf){ if(leaf != NULL){ inorder_Display(leaf->Left_Pointer); cout << leaf->value << ","; inorder_Display(leaf->Right_Pointer); } } void Binary_Tree::postorder_Display(){ postorder_Display(root); cout << "\n"; } void Binary_Tree::postorder_Display(node *leaf){ if(leaf != NULL){ inorder_Display(leaf->Left_Pointer); inorder_Display(leaf->Right_Pointer); cout << leaf->value << ","; } } void Binary_Tree::preorder_Display(){ preorder_Display(root); cout << "\n"; } void Binary_Tree::preorder_Display(node *leaf){ if(leaf != NULL){ cout << leaf->value << ","; inorder_Display(leaf->Left_Pointer); inorder_Display(leaf->Right_Pointer); } } int main(){ //Binary_Tree tree; Binary_Tree *tree = new Binary_Tree(); tree->Add(4); tree->Add(66); tree->Add(9); tree->Add(44); tree->Add(3); tree->Add(2); tree->Add(77); tree->preorder_Display(); tree->inorder_Display(); tree->postorder_Display(); delete tree; } |
Output
4, 2, 3, 9, 44, 66, 77
2, 3, 4, 9, 44, 66, 77
2, 3, 9, 44, 66, 77, 4,