# 12 - Binary Search Trees Data Structures and Algorithms...

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

Binary Search Trees Data Structures and Algorithms Andrei Bulatov

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

View Full Document
Algorithms – Binary Search Trees 12-2 Binary Search Tree A binary search tree is a binary rooted tree, in which keys satisfy the Binary Search Tree property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] key[x]. If y is a node in the right subtree of x, then key[x] key[y] 5 3 7 2 8 5 5 7 8 6 5
Algorithms – Binary Search Trees 12-3 Inorder Tree Walk Having a binary search tree one can print its content in sorted order Inorder-Tree-Walk(x) if x Nil then do Inorder-Tree-Walk(left[x]) print key[x] Inorder-Tree-Walk(right[x]) To print the entire tree just call Inorder-Tree-Walk(root[T]) It is called inorder because the root is printed between the subtrees A tree walk can also be preorder and postorder

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

View Full Document
Algorithms – Binary Search Trees 12-4 Inorder Tree Walk (cntd) Lemma If x is the root of an n-node subtree, then the call Inorder-Tree-Walk(x) takes Θ (n) time Proof Let T(n) denote the running time If x = Nil then T(0) = c, a constant If n > 0 then T(n) = T(k) + T(n – k – 1 ) + d where k is the number of nodes in the left subtree We prove that T(n) = (c+d) n + c For n = 0 we have (c + d) 0 + c = c = T(0)
Algorithms – Binary Search Trees 12-5 Inorder Tree Walk (cntd) Proof For n > 0 we have T(n) = T(k) + T(n – k – 1) + d = ((c + d) k + c) + ((c + d) (n – k – 1) + c) + d = (c + d) n + c – (c + d) + c + d = (c + d) n + c QED

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.

{[ snackBarMessage ]}

### Page1 / 22

12 - Binary Search Trees Data Structures and Algorithms...

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

View Full Document
Ask a homework question - tutors are online