Beruflich Dokumente
Kultur Dokumente
❑ General trees
❑ Binary trees
❑ Threaded binary trees
❑ Binary search trees
❑ Depth first & breadth first search
(a, [
(b, [
(d, []),
(e, [])
]),
(c, [
(f, [])
])
])
➔(a,[(b,[(d,[]),(e,[])]),(c,[(f,[])])])
struct _BT_NODE;
struct _BT_NODE {
int data;
BT_NODE *left, *right;
};
btAddToLeft(a, b);
btAddToRight(a, c);
btAddToRight(b, d);
for (;;) {
for (; n != NULL; n = n->left) push(&stack, n);
if (empty(stack)) break;
n = top(stack);
pop(&stack);
printf("%d ", n->data);
n = n->right;
}
}
18 IT3230: Basic C Programming – 2019/2020
Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology
DFS implementation using stack (VLR)
void btDepthFirstSearch(BT_NODE* n) {
STACK stack;
init(&stack);
push(&stack, n);
while (!empty(stack)) {
n = top(stack);
pop(&stack);
for (;;) {
for (; n != NULL; n = n->left) {
push(&stack, n);
push(&stack, n);
}
if (empty(stack)) break;
n = top(stack);
pop(&stack);
while (!empty(queue)) {
n = top(queue);
dequeue(&queue);
void tbtTraverse(BT_NODE* n) {
BT_NODE* cur = leftmost(root);
if (cur->isRightThread)
cur = cur->right;
else
cur = leftmost(cur->right);
}
}
26 IT3230: Basic C Programming – 2019/2020
Dr. Đào Trung Kiên – Hanoi Univ. of Science and Technology
Insertion
2 cases
To the left of parent node
To the right of parent node
Uses
Left rotation
Right rotation
Balanced node – the root of a balanced tree