コード例 #1
0
/* Print the tree in preorder, there will be no change in the root so no need of sending the address of the root */
void print_tree_preorder(struct node *root)
{
	if(root == NULL)
		return;
	printf("%d ", root->data);
	print_tree_preorder(root->left_child);
	print_tree_preorder(root->right_child);
}
コード例 #2
0
void print_tree_preorder (const std::shared_ptr<BTN<T>>& root)
{
    if (!root) {
        return;
    }
    std::cout << *root << std::endl;
    print_tree_preorder(root->left);
    print_tree_preorder(root->right);
}
コード例 #3
0
int main()
{
	/*
	1. Inserting the elements.
	2. Searching the elements.
	3. Inroder, postorder and preorder.
	4. Height of the tree.
	*/
	struct node *root = NULL;
	insert_elements(&root, 5);
	insert_elements(&root, 5);
	insert_elements(&root, 3);
	insert_elements(&root, 7);

	printf("\nHeight of the BST: %d\n", height_BST(root));
	insert_elements(&root, 1);
	insert_elements(&root, 2);
	insert_elements(&root, 6);
	insert_elements(&root, 8);
	print_tree_inorder(root);
	printf("\n");
	//print_tree_postorder(root);
	insert_elements(&root, 22);
	print_tree_postorder(root);
	printf("\n");
	print_tree_preorder(root);
	printf("\n");
	// A tree with only the root is of height 0
	printf("\nHeight of the BST: %d\n", height_BST(root));
	printf("\n%d\n", search_elements(&root, 4));
}