



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!




3 4 5 6 root 7 1 2
**How do you construct an expression tree from a postfix expression? E.g a b + c d e + * ***
1 2 3 4 5 6 7 root
1 2 3 4 5 6 7 root
Binary Trees Theorem: In a complete binary tree with n vertices and height H
Binary Trees Proof:
n = 2^0 + 2^1 + … 2 k n = 1 + 2^1 + 2^2 +… 2 k^ (Geometric Progression) n = (2k + 1^ – 1) / (2-1) n = 2k + 1^ - 1 Binary Trees Let k = H
Let k = H – 1
2 H^ ≤ n < 2H + 1 Binary Tree Traversals Inorder traversal
Inorder traversal root 1 2 3 4 5 6 7 2 44 55 2 3 66 77 3 1 Binary Tree Traversals Other traversals apply to binary case:
vertex, left subtree, right subtree
left subtree, vertex, right subtree
left subtree, right subtree, vertex
vertex, left children, right children
Vector Representation of Complete Binary Tree Tree navigation
0 l r ll lr
Vector Representation of Complete Binary Tree Tree navigation
0 l r ll lr rl
Vector Representation of Complete Binary Tree Tree navigation
0 l r ll lr rl rr
1 3 5 7 Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search tree iff: There is an order relation ≤ defined for the vertices of B For any vertex v , and any descendant u of v.left , u ≤ v For any vertex v , and any descendent w of v.right , v ≤ w root 4 2 6 Binary Search Tree Consequences: The smallest element in a binary search tree (BST) is the “left-most” node The largest element in a BST is the “right-most” node Inorder traversal of a BST encounters nodes in increasing order (^4) 2 6 1 3 5 7 root Binary Search using BST Assumes nodes are organized in a totally ordered binary tree Begin at root node Descend using comparison to make left/right decision if (search_value < node_value) go to the left child else if (search_value > node_value) go to the right child else return true (success) Until descending move is impossible Return false (failure)
Binary Search using BST Runtime <= descending path length <= depth of tree If tree has enough branching, runtime <= O(log size) Reading assignment for next class Section 4.