Created
November 23, 2017 13:17
-
-
Save tina1998612/11af7cede1279f4ae66649d39a058bf0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
#include "TreeNode.h" | |
void insertNodeHelper( TreeNode< int > **ptr, int arr[], int len) | |
{ | |
cout<<len<<endl; | |
if (len == 1) {*ptr = new TreeNode< int >( arr[0] ); cout<<"len 0"<<endl;} | |
else if (len == 0) { | |
return; | |
} | |
else{ | |
int left_arr[100]; | |
int right_arr[100]; | |
int maxi = -99; | |
int index = 0; | |
for(int i=0;i<len;i++){ | |
if(arr[i]>maxi) { | |
maxi = arr[i]; | |
index = i; | |
} | |
} | |
for(int i=0;i<index;i++){ | |
left_arr[i] = arr[i]; | |
} | |
cout<<"left "<<index<<endl; | |
int j=0; | |
if(index+1>len) return; | |
for(int i=index+1;i<len;i++){ | |
right_arr[j++] = arr[i]; | |
} | |
cout<<"right "<<j<<endl; | |
insertNodeHelper( &( ( *ptr )->leftPtr ), left_arr, index ); | |
insertNodeHelper( &( ( *ptr )->rightPtr ), right_arr, j ); | |
} | |
} // end function insertNodeHelper | |
void inOrderHelper( TreeNode< int > *ptr ) | |
{ | |
if ( ptr != 0 ) | |
{ | |
inOrderHelper(ptr->leftPtr); | |
cout << ptr->data << ' '; | |
inOrderHelper(ptr->rightPtr); | |
} // end if | |
} | |
int main() | |
{ | |
int arr_size = 6; | |
int arr[100] = {3,2,1,6,0,5}; | |
TreeNode< int >* my_tree = NULL; | |
insertNodeHelper( &my_tree, arr, arr_size); | |
cout << "\nInorder traversal\n"; | |
inOrderHelper(my_tree); | |
return 0; // indicates successful termination | |
} // end main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment