10. Tree Functions - int tmp = -1 if T != NULL...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Tree Print Function void print_nodes (int X) print_helper(root, X); cout << endl End print_nodes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
void print_helper (node* T, int X) if T = = NULL OR T->data = = X cout << setw(5) << X else if T->data < X cout << setw(5) << T->data print_helper (T->right, X) else print_helper (T->left, X) cout << setw(5) << T->data fi End print_helper Tree Print Function Continued
Background image of page 2
Binary Search Tree 1 5 3 0 1 0 5 3 5 3 2 4 5 8
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
PROGRAM STACK MAIN X = 32 Print_Helper X = 32 T->Data = 15 Print_Helper T-> Data = 30 Print_Helper T-> Data = 35 Print_Helper T-> Data = 32 Print_Nodes X = 32 T->Data = 15
Background image of page 4
Finding the Height of a Binary Tree int height (Node* T) int tmp = -1 if T != NULL tmp = max( height (T->left), height (T->right) ) + 1 fi return tmp End create
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Setting the Height Difference int difference (Node* T)
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int tmp = -1 if T != NULL T-&gt;difference = difference (T-&gt;right) difference (T-&gt;left) tmp = max( difference (T-&gt;left), difference (T-&gt;right) ) + 1 fi return tmp End create Creating A Binary Search Tree void create (int sorted, int size) if size &lt;= 0 root = NULL else root = create_helper (sorted, 0, size 1) fi End create Creating A Binary Search Tree Continued node * create_helper (int sorted, int first, last) if first &lt;= last middle = (first + last)/2 result = new_node(sorted[middle]) result-&gt;left = create_helper (sorted,first,middle-1) result-&gt;right = create_helper (sorted,middle+1,last) else result = NULL fi return result End create_helper...
View Full Document

Page1 / 8

10. Tree Functions - int tmp = -1 if T != NULL...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online