binary trees a binary tree is made up of a finite set of nodes that is either empty or consists of a...
Post on 20-Dec-2015
239 views
TRANSCRIPT
Binary Trees
A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees, called the left and right subtrees, which are disjoint from each other and from the root.
Binary Tree Example
Definitions: Node – element of the treeRoot – top nodeSubtree – binary tree associated
with the root node (left and right)
Children – the root node of a node’s subtrees
Edge – a link from a node to its children
Parent – the node to which the current node is a child
Ancestor, descendant
Binary Tree Example
Path – a sequence of nodes n1,n2,…,nk s.t. ni is a parent of ni+1 for 1 <= i < k
*Length of a path – k -1*Depth – length of path from
root to a node*Height – depth of the deepest
node +1 *Level – all nodes of depth dLeaf – node with two empty
childrenInternal node – node with at
least one non-empty child *No standard definitions
Full and Complete Binary Trees
*Full binary tree: Each node is either a leaf or internal node with exactly two non-empty children.
*Complete binary tree: If the height of the tree is d, then all leaves except possibly level d are completely full. The bottom level has all nodes to the left side.
Full Binary Tree Theorem (1)
Theorem: The number of leaves in a non-empty full binary tree is one more than the number of internal nodes.
Proof (by Mathematical Induction):
Base cases: A full, non-empty binary tree with 0 internal nodes must have 1 leaf node. A full binary tree with 1 internal node must have two leaf nodes.
Induction Hypothesis: Assume any full binary tree T containing n internal nodes has n+1 leaves.
Full Binary Tree Theorem (2)
Induction Step: Given tree T with n internal nodes, pick leaf node L and make it internal (by adding two children). T’ is still full. How many internal leaf nodes does it contain?
- We added two new leaf nodes, but lost one- n + 1 + 2 – 1 = n + 2
- We created one new internal node- n + 1
- T’ has n+1 internal nodes, and n+2 leaf nodes, or n’ internal nodes and n’+1 leaf nodes
- By induction, the theorem holds for n >= 0
Traversals (1)
Any process for visiting the nodes in some order is called a traversal.
Any traversal that lists every node in the tree exactly once is called an enumeration of the tree’s nodes.
Traversals (2)
• Preorder traversal: Visit each node before visiting its children.
• Postorder traversal: Visit each node after visiting its children.
• Inorder traversal: Visit the left subtree, then the node, then the right subtree.
• Level-order traversal: Visit all nodes in level 0, then level 1, then level 2, etc.
• Always visiting the left child first
Recursive Implementation
• Pre-, post-, and in-order traversals are all easily written as recursive functions (or with a stack)
• Level-order requires an additional data structure (a queue)
What are the traversals?
Give the pre-, post-, in-, and level order traversal of the tree.
Counting Nodes
We can count the number of nodes in a tree using a traversal.
Key point – the number of nodes in a tree with root node r is the number of nodes in r’s left subtree plus the number of nodes in r’s right subtree plus 1 (for r)
Array Implementation
Array Implementation
• Parent(r) = (r-1)/2 if r <> 0 and r < n.• Leftchild(r) = 2r + 1 if 2r + 1 < n.• Rightchild(r) = 2r + 2 if 2r + 2 < n.• Leftsibling(r) = r - 1 if r is even, r > 0, and r
< n.• Rightsibling(r) = r + 1 if r is odd and r + 1 <
n.
Binary Tree Implementation
Array Implementation
Array Implementation
• Parent(r) = (r-1)/2 if r <> 0 and r < n.• Leftchild(r) = 2r + 1 if 2r + 1 < n.• Rightchild(r) = 2r + 2 if 2r + 2 < n.• Leftsibling(r) = r - 1 if r is even, r > 0, and r
< n.• Rightsibling(r) = r + 1 if r is odd and r + 1 <
n.
Binary Search Trees
BST Property: All elements stored in the left subtree of a node with value K have values < K. All elements stored in the right subtree of a node with value K have values >= K.
BST SearchAlgorithm – search(K,T) for key K and tree T
if currnode is NULL, return false if K < currnode->value
return search(K,currnode->left)else if K > currnode->value
return search(K,currnode->right)else // must equal currnode
return true
BST Insert (1)
BST Insert (2)Insert algorithm (no duplicates)
insert(K,T) – insert key K into Tree T
Find leaf node L where search(K,T) fails
If K < L->valueadd K as L’s left child
else add K as L’s right child
BST Remove (1)
BST Remove
Remove algorithm –
Remove(K,T) – remove key K from tree T
find node N with K using search(K,T)if N is a leaf, remove and exitelse find the smallest node in the right subtree of N, called n N’
exchange values for N and N’remove N’
Cost of BST Operations
Find:Insert:Delete:
All cost depth of the node in question.
• Worst case: (n).
• Average case: (log n).
Heaps
Heap: Complete binary tree with the heap property:
• Min-heap: All values less than child values.• Max-heap: All values greater than child values.
The values are partially ordered.
Heap representation: Normally the array-based complete binary tree representation.
Building the Heap
(a) (4-2) (4-1) (2-1) (5-2) (5-4) (6-3) (6-5) (7-5) (7-6)(b) (5-2), (7-3), (7-1), (6-1)
Siftdown (1)
For fast heap construction:• Work from high end of array to low end.• Call siftdown for each item.• Don’t need to call siftdown on leaf nodes.
Siftdown(H) if(H->value > H->left->value and H > H->right->value) then return;else
H’ = max(H->left, H->right)swap values(H,H’)siftdown(H’)
Siftdown (2)
Buildheap Cost
Cost for heap construction:
log n
(i - 1) n/2i n. i=1
Remove Max Value
RemoveMax(H) -- get the max element off heap H
maxelem = H->value; // Max is at the topH->value = last elem->value;shiftdown(H);
return maxelem;
Priority Queues (1)
A priority queue stores objects, and on request releases the object with greatest value.
Example: Scheduling jobs in a multi-tasking operating system.
The priority of a job may change, requiring some reordering of the jobs.
Implementation: Use a heap to store the priority queue.
Priority Queues (2)
To support priority reordering, delete and re-insert. Need to know index for the object in question.
Huffman Coding Trees
ASCII codes: 8 bits per character.• Fixed-length coding.
Can take advantage of relative frequency of letters to save space.
• Variable-length coding
Build the tree with minimum external path weight.
Z K F C U D L E
2 7 24 32 37 42 42 120
Huffman Tree Construction (1)
Huffman Tree Construction (2)
Assigning Codes
Letter Freq Code Bits
C 32
D 42
E 120
F 24
K 7
L 42
U 37
Z 2
Coding and Decoding
A set of codes is said to meet the prefix property if no code in the set is the prefix of another.
Code for DEED:
Decode 1011001110111101:
Expected cost per letter: