-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytree.h
67 lines (54 loc) · 1.85 KB
/
binarytree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef BINARYTREE_H
#define BINARYTREE_H
struct TreeNode
{
char data;
struct TreeNode* left;
struct TreeNode* right;
};
/* Alloc a new node with given data. */
struct TreeNode* createNode(char data);
/* Insert data at appropriate place in BST, return new tree root. */
struct TreeNode* insertBST(struct TreeNode* root, char data);
/* Remove data from BST pointed to by rootRef, changing root if necessary.
* For simplicity's sake, always choose node's in-order
* successor in the two-child case.
* Memory for removed node should be freed.
* Return 1 if data was present, 0 if not found. */
int removeBST(struct TreeNode** rootRef, char data);
/* Return minimum value in non-empty binary search tree. */
char minValueBST(struct TreeNode* root);
/* Return maximum depth of tree. Empty tree has depth 0. */
int maxDepth(struct TreeNode* root);
/* A tree is balanced if both subtrees are balanced and
* the difference in height between the subtrees is no more than 1.
* Return 1 if tree is balanced, 0 if not. */
int isBalanced(struct TreeNode* root);
/* Return 1 if tree is a binary search tree, 0 if not. */
int isBST(struct TreeNode* root);
/* Print data for inorder tree traversal on single line,
* separated with spaces, ending with newline. */
void printTree(struct TreeNode* root);
/* Print data for leaves on single line,
* separated with spaces, ending with newline.*/
void printLeaves(struct TreeNode* root);
/* Print data for a preorder tree traversal on a single line
* as a sequence of (data, depth) pairs
* separated with spaces, ending with newline.
* (The root node has a depth of 1)
*
* So, the tree
*
* A
* / \
* B C
* / \
* D E
*
* will produce the output
* (A,1) (B,2) (D,3) (E,3) (C,2)
*/
void printTreeVerbose(struct TreeNode* root);
/* Free memory used by the tree. */
void freeTree(struct TreeNode* root);
#endif