{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10. Tree Functions

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

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

Tree Print Function void print_nodes (int X) print_helper(root, X); cout << endl End print_nodes

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

View Full Document
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
Binary Search Tree 1 5 3 0 1 0 5 3 5 3 2 4 5 8

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

View Full Document
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
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

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

View Full Document
Setting the Height Difference int difference (Node* T)

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

View Full Document
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->difference = difference (T->right) – difference (T->left) tmp = max( difference (T->left), difference (T->right) ) + 1 fi return tmp End create Creating A Binary Search Tree void create (int sorted, int size) if size <= 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 <= last middle = (first + last)/2 result = new_node(sorted[middle]) result->left = create_helper (sorted,first,middle-1) result->right = create_helper (sorted,middle+1,last) else result = NULL fi return result End create_helper...
View Full Document

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online