c++ - Sorting a Binary Tree -
i have written simple binary tree using structures , couple of functions in order add, search, find minimum , maximum values, remove node destroy node, problem have been using recursion , dealing in same way , having hard time trying comprehend if function sorting algorithm efficient enough.
the whole code is:
#include<iostream> using namespace std; /* simple example demonstrating basic binary tree implemented using structurs, later on create using classes, now, structures , pointers suffice */ /* * node created in here, notice how pointer has ability * self reference 2 different positions, means there ability * store 2 different branches of memory * */ struct node{ int key_value; node * p_left; node * p_right; }; /* * * creation of add function add linked list * */ node* add(node * p_tree, int key) { //--the base case of recursive function placed in here //--since binary trees recursive in nature , linked data structures //--are whole in terms of space , memory, recursive function //--suffice cases involving binary trees. //--in case, if given parameter null, create tree //--by allocating necessary memory space if (p_tree == null) { node * pnew_tree = new node; pnew_tree->p_left = null; pnew_tree->p_right = null; pnew_tree->key_value = key; return pnew_tree; }// end of base case //--depending of value of node, determine if add left side or right side of subtree if (key < p_tree->key_value){ // if less value, add left p_tree->p_left = insert(p_tree->p_left, key); } else{ p_tree->p_right = insert(p_tree->p_right, key); } return p_tree; } // end of function /* * * search function created * in here function go on subtrees untill 1 necessary key returned * again, uses recursive functions doing things step step: * * first: see if given tree node empty(null) if yes return null * * second: if find key referencing key value, done , return particular tree * * third: otherwise, left , right sides of tree making recursive calls same function until * 1 looking found. * */ node* search(node *p_tree, key) { //--first: if (p_tree == null) { return null; } //--second: else if (p_tree->key_value = key) { return p_tree; } //--third: else if(key < p_tree->key_value) { search(p_tree->p_left, key); //--thus looks left same recursive algorithm } else { search(p_tree->p_right, key); } }//--end of recursive search function /* * * easiest function implement, since delete key used being whole concept falls inside memory being allocated * list via creation of new nodes(this means using new keyword allocate memory, creating new objects in other languages) * * first: check see if passed tree not null, if not null destroy left , right subtree using same function * else nothing. * * note: return value set void since returns nothing list * */ void destroy_node(node* p_tree) { //--first if (p_tree != null) { destroy(p_tree->p_left); destroy(p_tree->p_right); cout << endl; cout << "destroying left subtree node" << endl; cout << "destroying right subtree node" << endl; cout << "deleting entire node: " << p_tree->key_value << endl; cout << endl; delete p_tree; } }//--end of recursive destroy function /* * * finding max value simple, evaluate left , right node , use base cases see node return * why right? * looking @ theory behind binary trees, tree on right biggest element. how trees sorted. * there no need @ keys, code sort out in space since if not null return highest. */ node* return_max(node* p_tree) { if (p_tree == null) { return null; } if (p_tree->p_right == null) { return p_tree; } return return_max(p_tree->p_right); } //--end of return max recursive function /* * * max node, opposite of avobe taking advantage of fact left node lesser * recursion used again * */ node* return_min(node* p_tree){ if (p_tree == null) { return null; } if(p_tree->p_left == null) { return p_tree; } return return_min(p_tree->p_left); }//--end of recursive return min function /* * * need remove max function in order remove biggest node in case found, way can implement recursive * algorithm inside function in charge of removing node want, can remove node using delete or destroy once * find because destroy entire tree! no no, not good. * */ node* remove_max_node(node* p_tree, node* p_max_node) { if (p_tree == null) { return null;} if (p_tree == p_max_node) { return p_max_node->p_left; //--because left 1 lesser } //--now recursive call, implementing means remove node on right //--basing on sense right tree highest one, go top bottom p_tree->p_right = remove_max_node(p_tree->p_right, p_max_node); //--return tree after changes in addresses have been conducted return p_tree; } /* * * * * * removing tree simple based on recursive nature of element being discussed * * first: check see if tree null, if yess, return null * */ node* removen(node* p_tree, int key) { //--first: if (p_tree == null) { return null;} //--second if(p_tree->key_value == key) { //--third: if (p_tree->p_left == null) { node* p_right_sub = p_tree->p_right; delete p_tree; return p_right_sub; } if (p_tree->p_right == null) { node* p_left_sub = p_tree->p_left; delete p_tree; return p_left_sub; } node* p_maxn = return_max(p_tree->p_left); p_maxn->p_left = remove_max_node(p_tree->p_left, p_max_node); p_maxn->p-right = p-tree->p_right; delete p_tee; return p_max_node; } else if(key < p_tree->key_value) { p_tree->p-left = removen(p_tree->p_left, key); } else { p_tree->p_right = removen(p_tree->p_right, key); } //--after changes have been done return p_tree; } /* * * entire implementation sorted when calling return min , max * * */ node* sortedn(node* p_tree){ if (p_tree == null){return null;} return sortedn(return_max(p_tree)); } int main(int argv, char* []){ cout << "this merely test" << endl; return 0; }
my understanding way defined other functions sort , return state of node in new way being using pointers. maybe wrong think sorting function works. have been day , cannot think of better way, of code written understanding have in , of books, instructor not being came here seeking wisdom.
Comments
Post a Comment