









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!










A
C (^) D E F G
H I
B
J (^) K L M N
P Q
n i is the parent of n i+1 (1 ≤ i < k); The no. of edges in the path is called the length of the path; A length 0 path is a path from a node to itself. There is a unique path from root to any node n i ; The length of this path is called the depth of n i ; thus, the root is a depth 0 node.
depth of the deepest leaf node, which is the same as the height of the tree.
left child right child
root
left child right child
root
Array implementation of binary tree
We can embed the nodes of a binary tree into a one-dimensional array by defining
a relationship between the position of each parent node and the position of its children.
How much space is required for the array to represent a tree of depth d?
Array Implementation:
47
25 77
11 43 65 93
7 17 31 44 68
555
555
501
555
501 701
555
501 701
358
555
501 701
358 513
555
501 701
358 513 561
555
501 701
358 513 561 797 555
501 701
358 513 561 797
345 490 345
555
501 701
358 513 561 797
490 724
(^555 501 701 358 513 561 797 345 490 )
void inorder(tree_pointer ptr) { if (ptr) { inorder(ptr->left_child); printf(“%d”, ptr->data); inorder(ptr->right_child); } } Pre-order: (NLR) void preorder(tree_pointer ptr) { if (ptr) { printf(“%d”, ptr->data); preorder(ptr->left_child); preorder(ptr->right_child); } }
Post-order: (LRN) void postorder(tree_pointer ptr) { if (ptr) { postorder(ptr->left_child); postorder(ptr->right_child); printf(“%d”, ptr->data); } }
Example: Expression Tree
Example: An Expression Tree
A B (^) C /
E F
Adjacency List
Summary