//中序遍历二叉树(左、根、右)(递归算法) void inOrderPrint(BiTree T, int root){ if(root){//根节点不为空 inOrderPrint(T, T[root].llink); printf("%c ", T[root].data); inOrderPrint(T, T[root].rlink); } }
// A helper function to recursively print the payloads of the specified sub-tree in-order void BST::inOrderPrint(BinaryTreeNode* node) const { if (node == nullptr) return; inOrderPrint(node->Left); std::cout << *node << std::endl; inOrderPrint(node->Right); }
void binaryTree::inOrderPrint(node* node) { if(node == NULL) return; inOrderPrint(node->left); std::cout << node->value << " "; inOrderPrint(node->right); }
/* function to print the values of the nodes of a tree using an in-order traversal */ void inOrderPrint(TreeNodePtr rootPtr) { if (rootPtr != NULL) { inOrderPrint(rootPtr->leftPtr); printf("%d ", rootPtr->data); inOrderPrint(rootPtr->rightPtr); } }
void inOrderPrint(NODE* ptr){ if(ptr->left != NULL) inOrderPrint(ptr->left); printf("%d ", ptr->num); if(ptr->right != NULL) inOrderPrint(ptr->right); return; }
/* function to print nodes in binary search tree in ascending order */ void printBinarySearchTreeValuesInAscendingOrder(TreeNodePtr rootPtr) { /* TODO: CALL APPROPRIATE TREE TRAVERSAL FUNCTION HERE */ /* COMPLETING THIS LINE IS REQUIRED FOR YOUR MINI-ASSIGNMENT */ inOrderPrint(rootPtr); }
void main(){ BiTree tree; int root = 1;//根节点的位置 printf("请按先序次序输入二叉树各节点以#号结束,空树用点号代替:\n"); int pos = 1;//控制加入静态数组的位置 createBiTree(tree, root, pos); printf("先序遍历打印二叉树(递归算法):\n"); preOrderPrint(tree, root); printf("\n"); printf("先序遍历打印二叉树(非递归算法):\n"); preOrderPrint2(tree, root); printf("\n"); printf("中序遍历打印二叉树(递归算法):\n"); inOrderPrint(tree, root); printf("\n"); printf("中序遍历打印二叉树(非递归算法):\n"); inOrderPrint2(tree, root); printf("\n"); printf("后序遍历打印二叉树(递归算法):\n"); postOrderPrint(tree, root); printf("\n"); printf("后序遍历打印二叉树(非递归算法):\n"); postOrderPrint2(tree, root); printf("\n"); printf("按层次遍历打印二叉树(非递归算法):\n"); hierarchicalTraversePrint(tree, root); printf("\n"); int depth = getBiTreeDepth(tree, root); printf("该二叉树的深度为:%d\n", depth); int size = getBiTreeSize(tree, root); printf("该二叉树的结点数为:%d\n", size); int leafNodesNum = getBiTreeLeafNodesNum(tree, root); printf("该二叉树的叶子结点数为:%d\n", leafNodesNum); }
int main(){ NODE* ptr; TREE* tree; int i=0; tree = create_tree(); for(i=0; i<13; i++){ insertNode(tree, i); } printf("PreOrder: "); preOrderPrint(tree->root); printf("\nInOrder: "); inOrderPrint(tree->root); printf("\npostOrder: "); postOrderPrint(tree->root); printf("\nPrint list:"); linklistprint(tree->root); printf("\ntree count: %d\n", tree->count); return 0; }
/* Function to illustrate printing the nodes of a tree using pre-order, in-order, and post-order traversals */ void traversalExamples() { int i, value; TreeNodePtr rootPtr = NULL; /* create example tree by inserting 10 nodes with random values into random positions of tree */ srand(time(NULL)); /* seed random number generator */ for (i=0; i < 10; i++) { value = rand() % 100; insertTreeNodeRandomly(&rootPtr, value); } printf("RANDOM TREE:\n"); printTree(rootPtr, 0); printf("\nPre-order traversal:\n"); preOrderPrint(rootPtr); printf("\nIn-order traversal:\n"); inOrderPrint(rootPtr); printf("\nPost-order traversal:\n"); postOrderPrint(rootPtr); printf("\n"); }
void binaryTree::printin(node* node){ std::cout << "in order: "; return inOrderPrint(root); }