A simple tree implementation using structured nodes.
More...
#include <iostream>
Go to the source code of this file.
A simple tree implementation using structured nodes.
- Todo
- update code to use C++ STL library features and OO structure
- Warning
- This program is a poor implementation - C style - and does not utilize any of the C++ STL features.
Definition in file binary_search_tree.cpp.
◆ BFT()
Definition at line 92 of file binary_search_tree.cpp.
92 {
93 if (n != NULL) {
94 std::cout << n->val << " ";
95 enqueue(n->left);
96 enqueue(n->right);
97 BFT(dequeue());
98 }
99}
◆ dequeue()
◆ enqueue()
◆ findMaxInLeftST()
int findMaxInLeftST |
( |
node * | n | ) |
|
Definition at line 53 of file binary_search_tree.cpp.
53 {
54 while (n->right != NULL) {
55 n = n->right;
56 }
57 return n->val;
58}
◆ In()
Definition at line 109 of file binary_search_tree.cpp.
109 {
110 if (n != NULL) {
111 In(n->left);
112 std::cout << n->val << " ";
113 In(n->right);
114 }
115}
◆ Insert()
void Insert |
( |
node * | n, |
|
|
int | x ) |
Definition at line 29 of file binary_search_tree.cpp.
29 {
30 if (x < n->val) {
31 if (n->left == NULL) {
33 temp->val = x;
34 temp->left = NULL;
35 temp->right = NULL;
36 n->left = temp;
37 } else {
38 Insert(n->left, x);
39 }
40 } else {
41 if (n->right == NULL) {
43 temp->val = x;
44 temp->left = NULL;
45 temp->right = NULL;
46 n->right = temp;
47 } else {
48 Insert(n->right, x);
49 }
50 }
51}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
◆ main()
Definition at line 125 of file binary_search_tree.cpp.
125 {
128 int value;
129 int ch;
131 std::cout << "\nEnter the value of root node :";
132 std::cin >> value;
133 root->val = value;
134 root->left = NULL;
135 root->right = NULL;
136 do {
137 std::cout << "\n1. Insert"
138 << "\n2. Delete"
139 << "\n3. Breadth First"
140 << "\n4. Preorder Depth First"
141 << "\n5. Inorder Depth First"
142 << "\n6. Postorder Depth First";
143
144 std::cout << "\nEnter Your Choice : ";
145 std::cin >> ch;
146 int x;
147 switch (ch) {
148 case 1:
149 std::cout << "\nEnter the value to be Inserted : ";
150 std::cin >> x;
151 Insert(root, x);
152 break;
153 case 2:
154 std::cout << "\nEnter the value to be Deleted : ";
155 std::cin >> x;
156 Remove(root, root, x);
157 break;
158 case 3:
159 BFT(root);
160 break;
161 case 4:
162 Pre(root);
163 break;
164 case 5:
165 In(root);
166 break;
167 case 6:
168 Post(root);
169 break;
170 }
171 } while (ch != 0);
172
173 return 0;
174}
◆ Post()
Definition at line 117 of file binary_search_tree.cpp.
117 {
118 if (n != NULL) {
119 Post(n->left);
120 Post(n->right);
121 std::cout << n->val << " ";
122 }
123}
◆ Pre()
Definition at line 101 of file binary_search_tree.cpp.
101 {
102 if (n != NULL) {
103 std::cout << n->val << " ";
104 Pre(n->left);
105 Pre(n->right);
106 }
107}
◆ Remove()
void Remove |
( |
node * | p, |
|
|
node * | n, |
|
|
int | x ) |
Definition at line 60 of file binary_search_tree.cpp.
60 {
61 if (n->val == x) {
62 if (n->right == NULL && n->left == NULL) {
63 if (x < p->val) {
64 p->right = NULL;
65 } else {
66 p->left = NULL;
67 }
68 } else if (n->right == NULL) {
69 if (x < p->val) {
70 p->right = n->left;
71 } else {
72 p->left = n->left;
73 }
74 } else if (n->left == NULL) {
75 if (x < p->val) {
76 p->right = n->right;
77 } else {
78 p->left = n->right;
79 }
80 } else {
81 int y = findMaxInLeftST(n->left);
82 n->val = y;
83 Remove(n, n->right, y);
84 }
85 } else if (x < n->val) {
86 Remove(n, n->left, x);
87 } else {
88 Remove(n, n->right, x);
89 }
90}
◆ queue