



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




COP 3502 โ Lab Notes Tree Traversals and BSTs This lab is designed to give you some practice with binary tree traversal algorithms and binary search trees. The last question is from a previous foundation exam. Binary Tree Traversals
a b c
a b c d e f g h i j
Binary Tree Traversal Algorithms Preorder (Depth-First Search) void preorder(struct treeNode * p) { if (p !=NULL) { printf(โ%d\nโ, p->data); preorder(p->left_child); preorder(p->right_child); } } Inorder void inorder(struct treeNode *p) { if (p !=NULL) { inorder(p->left_child); printf(โ%d\nโ, p->data); inorder(p->right_child); } } Postorder void postorder(struct treeNode *p) { if (p !=NULL) { postorder(p->left_child); postorder(p->right_child); printf(โ%d\nโ, p->data); } }
Binary Search Trees Binary search tree is a binary tree that is either empty or in which each node contains a data value that satisfies the following: a) all data values in the left subtree are smaller than the data value in the root. b) the data value in the root is smaller than all values in its right subtree. c) the left and right subtrees are also binary search tees. This structure allows us to quickly search for a particular value.
c) Perform the following procedure on the tree above, listing the output in the spaces below and leaving any unused spaces blank. Assume that the procedure is initially called with: P6(root, 30) and that the tree nodes and pointers are defined as: struct treeNode{ int data; struct tree_node *left, *right; } struct tree_node *tree_ptr; void P6(struct tree_ptr *node_ptr, int key) { if (node_ptr != NULL) { P6(node_ptr->right, (key โ 5)); P6(node_ptr->left, (key + 5)); if (node_ptr->data < key) printf(โ%d โ,node_ptr->data); } } ANSWER 28 22 20 14 24