36
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 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,

  • View
    236

  • Download
    2

Embed Size (px)

Citation preview

Page 1: 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,

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.

Page 2: 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,

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

Page 3: 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,

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

Page 4: 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,

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.

Page 5: 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,

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.

Page 6: 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,

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

Page 7: 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,

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.

Page 8: 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,

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

Page 9: 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,

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)

Page 10: 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,

What are the traversals?

Give the pre-, post-, in-, and level order traversal of the tree.

Page 11: 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,

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)

Page 12: 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,

Array Implementation

Page 13: 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,

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.

Page 14: 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,

Binary Tree Implementation

Page 15: 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,

Array Implementation

Page 16: 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,

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.

Page 17: 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,

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.

Page 18: 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,

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

Page 19: 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,

BST Insert (1)

Page 20: 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,

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

Page 21: 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,

BST Remove (1)

Page 22: 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,

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’

Page 23: 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,

Cost of BST Operations

Find:Insert:Delete:

All cost depth of the node in question.

• Worst case: (n).

• Average case: (log n).

Page 24: 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,

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.

Page 25: 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,

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)

Page 26: 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,

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’)

Page 27: 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,

Siftdown (2)

Page 28: 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,

Buildheap Cost

Cost for heap construction:

log n

(i - 1) n/2i n. i=1

Page 29: 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,

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;

Page 30: 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,

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.

Page 31: 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,

Priority Queues (2)

To support priority reordering, delete and re-insert. Need to know index for the object in question.

Page 32: 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,

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

Page 33: 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,

Huffman Tree Construction (1)

Page 34: 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,

Huffman Tree Construction (2)

Page 35: 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,

Assigning Codes

Letter Freq Code Bits

C 32

D 42

E 120

F 24

K 7

L 42

U 37

Z 2

Page 36: 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,

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: