int main(){ printf("\nEnter the no of nodes you want to insert : "); int n; scanf("%d",&n); int i,data; TNode* root=NULL; for(i=0;i<n;i++){ printf("\nEnter data for %d node : ",i); scanf("%d",&data); Insert(&root,data); } printf("\n%d nodes inserted in BST\n"); getch(); printf("\nPress enter to traverse \n\n: "); getch(); InorderTraversal(root); getch(); printf("\n\nEnter range within which nodes should remain in BST\n\n"); int min,max; scanf("%d %d",&min,&max); root=RemoveNodesOutOfRange(root,min,max); printf("\nRemoved\n"); getch(); printf("\nPress enter to traverse\n"); getch(); InorderTraversal(root); getch(); return 0; }
//--------------------------------------------------------------------------- //中序走訪樹並將結果存在 wrItems 陣列 void InorderTraversal(Item *node, Wrap *answer,int &i) { if (node != NULL) { InorderTraversal(node->itmRight, answer, i); answer[i++].itm = node; InorderTraversal(node->itmLeft, answer, i); } }
void InorderTraversal(TreeNode* cur){ if(!cur) return; InorderTraversal(cur->left); if(first == nullptr && pre->val >= cur->val) first = pre; if(first != nullptr && pre->val >= cur->val) second = cur; pre = cur; InorderTraversal(cur->right); }
void InorderTraversal(Node *root) { if (root != NULL) { InorderTraversal(root->left); if (root->val != 0) { printf("%d ", root->val); } InorderTraversal(root->right); } }
void Tree::InorderTraversal(Node *node) { if (node) { InorderTraversal(node->Left); cout << node->data << " "; InorderTraversal(node->Right); } }
void InorderTraversal(BstNode root) { if(root == NULL) { return; } InorderTraversal(root->left); printf("%d ", root->data); InorderTraversal(root->right); }
void InorderTraversal(TNode* root){ if(!root){ return; } InorderTraversal(root->left); printf("%d ",root->data); InorderTraversal(root->right); }
int* InorderTraversal(struct TreeNode* root, int* returnSize, int *result) { if (root == NULL) return result; InorderTraversal(root->left, returnSize, result); (*returnSize)++; result = realloc(result, (*returnSize + 1) * sizeof(int)); result[*returnSize] = root->val; InorderTraversal(root->right, returnSize, result); return result ; }
int main() { srand((int)time(0)); int i, j, pocet, vyska; node *strom; int cisla[7] = {100, 300, 1000, 3000, 10000, 30000, 100000}; printf("%7s %7s %6s %6s", "Pocet", "Prumer", "Vyska", "Podil"); printf("\n-----------------------------\n"); for (i = 0; i < 7; i++) { pocet = cisla[i]; strom = NULL; nodes = 0; elements = 0; for (j = 0; j < pocet; j++) Insert(&strom, rand()); vyska = Heigh(strom); printf("%7i %7.2f %6i %6.2f\n", pocet, ((float)elements / (float)nodes), vyska, (vyska / (log((float)elements)/log(2)))); } InorderTraversal(strom); return 0; }
int main(int argc, char argv[]) { Node *root; int T; int N; scanf("%d ", &T); for (int test_case = 1; test_case <= T; test_case++) { scanf("%d ", &N); root = GetNode(); MakeTree(root, N); printf("# Preorder\n"); PreorderTraversal(root->left); putchar('\n'); printf("# Inorder\n"); InorderTraversal(root->left); putchar('\n'); printf("# Postorder\n"); PostorderTraversal(root->left); putchar('\n'); if (root != NULL) { DeleteNode(root); //DeleteAllNode(); } } return 0; }
//--------------------------------------------------------------------------- void __fastcall TfKnapsack::btAddItemClick(TObject *Sender) { //如果已存在陣列 wrItems 將之清除 if (wrItems) { delete[] wrItems; wrItems = NULL; } //讀取使用者輸入 int iItemWeight = StrToInt(edItemWeight->Text); int iItemProfit = StrToInt(edItemProfit->Text); itmRoot = AddItem(itmRoot, iItemWeight, iItemProfit); //產生新的 wrItems 陣列 iTotalNum += 1; //用來計算現在總共有多少資料 wrItems = new Wrap [iTotalNum]; //用中序走訪樹並將結果存到 wrItems InorderTraversal(itmRoot, wrItems, 0); //顯示所有資料,由 CP 值高到低顯示 if (cbPrintItems->Checked) PrintItems(); memSolution->Lines->Add("1 筆資料新增完成"); }
//--------------------------------------------------------------------------- void __fastcall TfKnapsack::btAddItemsClick(TObject *Sender) { //如果已存在陣列 wrItems 將之清除 if (wrItems) { delete[] wrItems; wrItems = NULL; } //讀取使用者輸入 iItemNum = StrToInt(edItemNum->Text); iWeightRange = StrToInt(edWeightRange->Text); iProfitRange = StrToInt(edProfitRange->Text); //亂數產生 Items 的重量以及價值 //並加入 itmRoot AddItems(); //產生新的 wrItems 陣列 iTotalNum += iItemNum; //用來計算現在總共有多少資料 wrItems = new Wrap [iTotalNum]; //用中序走訪樹並將結果存到 wrItems InorderTraversal(itmRoot, wrItems, 0); //顯示所有資料,由 CP 值高到低顯示 if (cbPrintItems->Checked) PrintItems(); memSolution->Lines->Add(edItemNum->Text + " 筆亂數資料新增完成"); }
void Tree::InorderTraversal() { if (!Root) cout << "Tree is empty" << endl; else InorderTraversal(Root); }
int* inorderTraversal(struct TreeNode* root, int* returnSize) { int *result = malloc(sizeof(int)); result[0] = 0; *returnSize = 0; result = InorderTraversal(root, returnSize, result); return result + 1; }
int main() { node *T = NULL; Insert(&T, 4); Insert(&T, 3); Insert(&T, 16); Insert(&T, 7); Insert(&T, 0); Insert(&T, 15); Insert(&T, 26); InorderTraversal(T); }
void InorderTraversal(node *tree) { static int temp = 0; int i = 0; if (tree == NULL) return; InorderTraversal(tree->child[0]); do { temp++; if ( (temp <= 10) || (temp >= 99991) ) { if (temp == 1) printf("Prvnich deset prvku:\n"); if (temp == 99991) printf("\nPoslednich deset prvku:\n"); printf("%i ", tree->item[i]); } i++; InorderTraversal(tree->child[i]); } while (i < (tree->order - 1)); }
int main(int argc, char* argv[]) { int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int len = LEGNTH(array); BstNode root = BuildBinaryTree(array, len); InorderTraversal(root); printf("\n"); return 0; }
void recoverTree(TreeNode* root) { if(!root) return; //vector<int> vs = InorderTraversal(root); InorderTraversal(root); std::swap(first->val, second->val); }