/************new inorderPrint function******************/ void inorderPrint(NodePtr nodePtr){ NodePtr currentPtr=nodePtr; if(currentPtr!=NULL){ inorderPrint(currentPtr->leftPtr); printf("%d\t",currentPtr->number); inorderPrint(currentPtr->rightPtr); } }
void Treap::inorderPrint(tNode* currentNode) { if(!currentNode->isDummy()) { cout<<"Rec: "<<currentNode->getKey()<<" P: "<<currentNode->getPriority()<<endl; cout<<"Left:"<<currentNode->Left()->getKey()<<" Right:"<<currentNode->Right()->getKey()<<endl; } if (!currentNode->isDummy()) { inorderPrint(currentNode->Left()); cout << currentNode->getKey()<<" "<<currentNode->getValue()<<endl; inorderPrint(currentNode->Right()); } }
int inorderPrint(node_t* node) { if (!node) { return -1; } inorderPrint(node->leftChild); printf("%3c", node->data); inorderPrint(node->rightChild); return 0; }
void inorderPrint( command_t root, int* order, struct command array[] ) { // Print all the items in the tree to which root points. // The items in the left subtree are printed first, followed // by the item in the root node, followed by the items in // the right subtree. if ( root != NULL ) { // (Otherwise, there's nothing to print.) inorderPrint( root->u.command[0], order, array); // Print items in left subtree. array[*order] = *root;// Print the root item. order++; inorderPrint( root->u.command[1], order, array); // Print items in right subtree. } } // end inorderPrint()
// Method: inorderPrint // Purpose: prints the 234 tree in order // Pre-cond: none // Post-cond: tree is printed in order void TwoThreeFourTree::inorderPrint(TwoThreeFourTreeNode* nodePtr) { if (nodePtr->kidsArray[0] == NULL){ if (nodePtr->itemsArray[0] != NULL) nodePtr->itemsArray[0]->print(); if (nodePtr->itemsArray[1] != NULL) nodePtr->itemsArray[1]->print(); if (nodePtr->itemsArray[2] != NULL) nodePtr->itemsArray[2]->print(); return; } // end if else inorderPrint(nodePtr->kidsArray[0]); nodePtr->itemsArray[0]->print(); if (nodePtr->kidsArray[1] == NULL){ if (nodePtr->itemsArray[1] != NULL) nodePtr->itemsArray[1]->print(); if (nodePtr->itemsArray[2] != NULL) nodePtr->itemsArray[2]->print(); return; } // end if else inorderPrint(nodePtr->kidsArray[1]); if (nodePtr->itemsArray[1] == NULL) return; else nodePtr->itemsArray[1]->print(); if (nodePtr->kidsArray[2] == NULL){ if (nodePtr->itemsArray[2] != NULL) nodePtr->itemsArray[2]->print(); return; } // end if else inorderPrint(nodePtr->kidsArray[2]); if (nodePtr->itemsArray[2] == NULL) return; else nodePtr->itemsArray[2]->print(); if (nodePtr->kidsArray[2] == NULL) return; else inorderPrint(nodePtr->kidsArray[3]); } // end inorderPrint
int testTreeWithInorder(tree_t* tree) { if (!tree) { return -1; } //printf("INORDER PRINT\n\n"); inorderPrint(tree->root); printf("\n"); return 0; }
/**************new main function*****************/ int main(void) { int element,printChoice; NodePtr rootNodePtr=NULL; puts("enter a number to insert or enter a non-number to stop insert function\n"); printf("?:"); while(scanf("%d",&element)){ if(rootNodePtr==NULL){ rootNodePtr=(NodePtr)malloc(sizeof(Node)); rootNodePtr->number=element; rootNodePtr->leftPtr=NULL; rootNodePtr->rightPtr=NULL; } else{ insert(rootNodePtr,element); } printf("?:"); } getchar(); //第77行 scanf("%d",&i);是告诉电脑,你从输入流输入一个东西, printInstruction(); //如果他是整形(%d)判断,那么他将传递过来的地址 scanf("%d",&printChoice); //(&i是对i取地址)接收,否则将被继续放在缓存中。 //92行输入非数字就会进入死循环! 因为它一直被放在缓存中 while(printChoice!=4){ switch(printChoice){ case 1: inorderPrint(rootNodePtr); break; case 2: preorderPrint(rootNodePtr); break; case 3: postorderPrint(rootNodePtr); break; default: puts("invalid choice\n"); //printInstruction(); break; } fflush(stdin); // to clean the date in the cache printInstruction(); scanf("%d",&printChoice); } puts("function end\n"); return 0; } //end main function
void Treap::sortedDump ( ) { inorderPrint(_root); }
// Method: printTree // Purpose: Traverse the tree and print out each object in search key order // Pre-cond: none // Post-cond: tree printed void TwoThreeFourTree::printTree() { inorderPrint(rootNode); } // end printTree