binary trees

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 called the left and right subtrees which are disjoint from each other and from the root Notation: Node , Children , Edge , Parent , Ancestor , Descendant , Path , Depth , Height , Level , Leaf Node , Internal Node , Subtree . A G F E C D B I H

Upload: kirk-snider

Post on 30-Dec-2015

17 views

Category:

Documents


2 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

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

Notation: Node, Children, Edge, Parent, Ancestor, Descendant, Path, Depth, Height, Level, Leaf Node, Internal Node, Subtree.

A

G

FEC

DB

IH

Page 2: Binary Trees

Full and Complete Binary Trees

A Full binary tree has each node being either a leaf or internal node with exactly two non-empty children.

A Complete binary tree: If the height of the tree is d, then all levels except possibly level d are completely full. The bottom level has all nodes to the left side.

Page 3: Binary Trees

Full Binary Tree Theorem

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 Case: A full binary tree with 1 internal node must have two leaf

nodes.• Induction Hypothesis: Assume any full binary tree T containing n-1

internal nodes has n leaves.• Induction Step: Pick an arbitrary leaf node j of T. Make j an internal

node by giving it two children. The number of internal nodes has now gone up by 1 to reach n. The number of leaves has also gone up by 1.

Corollary: The number of NULL pointers in a non-empty binary tee is one more than the number of nodes in the tree.

Page 4: Binary Trees

Traversals

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.

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.void preorder(BinNode * root) {

if (root==NULL) return;visit(root);preorder(root->leftchild());preorder(root->rightchild()); }

Page 5: Binary Trees

Expression Trees

• Example of (a-b)/((x*y+3)-(6*z))

/

-

a b

-

+

6 z

*

yx

3*

Page 6: Binary Trees

Binary Search Trees

• Left means less – right means greater.• Find

– If item<cur->dat then cur=cur->left

– Else if item>cur->dat then cur=cur->right

– Else found

– Repeat while not found and cur not NULL

• No need for recursion.

Page 7: Binary Trees

Find min and max

• The min will be all the way to the left– While cur->left != NULL, cur=cur->left

• The max will be all the way to the right– While cur->right !=NULL, cur=cur->right

• Insert– Like a find, but stop when you would go to a null and

insert there.

Page 8: Binary Trees

Remove

• If node to be deleted is a leaf (no children), can remove it and adjust the parent node (must keep track of previous)

• If the node to be deleted has one child, remove it and have the parent point to that child.

• If the node to be deleted has 2 children– Replace the data value with the smallest value in the

right subtree

– Delete the smallest value in the right subtree (it will have no left child, so a trivial delete.

Page 9: Binary Trees

Array Implementation

• For a complete binary tree

• Parent(x)= • Leftchild(x)= • Rightchild(x)= • Leftsibling(x)= • Rightsibling(x)=

0

1 2

3 4 56

7 8 9 10 11

(x-1)/22x+12x+2

x-1

x+1

Page 10: Binary Trees

Huffman Coding Trees

• Each character has exactly 8 bits• Goal: to have a message/file take less space• Allow some characters to have shorter bit patterns, but

some characters can have longer.• Will not have any benefit if each character appears with

equal probability.• English does not have equal distribution of character

occurance.• If we let the single bit 1 represent the letter E, then no

other character can start with a 1. So some will have to be longer.

Page 11: Binary Trees

The Tree

• Look at the left pointer as the way to go for a 0 and the right pointer is the way to go for a 1.

• Take input string of 1’s and 0’s and follow them until hit null pointer, then the character in that node is the character being held.

• Example:• 0 = E

• 10 = T

• 110 = P

• 111 = F

• 0110111010=EPFET

E

T

P F

Page 12: Binary Trees

Weighted Tree

• Each time we have to go to another level, takes time. Want to go down as few times as needed.

• Have the most frequently used items at the top, least frequently items at the bottom.

• If we have weights or frequencies of nodes, then we want a tree with minimal external path weight.

Page 13: Binary Trees

Huffman Example

• Assume the following characters with their relative frequencies.Z K F C U D L E

2 7 24 32 37 42 42 120

• Arrange from smallest to largest.• Combine 2 smallest with a parent node with the

sum of the 2 frequencies. • Replace the 2 values with the sum. Combine the

nodes with the 2 smallest values until only one node left.

Page 14: Binary Trees

Huffman Tree Construction

Z K F C U D L E

2 7 24 32 37 42 42 120

306

186

79 107

65

33

9

120E

37U

42D

42L

32C

2Z

7K

24F

Page 15: Binary Trees

Results

Let Freq Code Bits Mess. Len. Old BitsOld Mess. Len.

C 32 1110 4 128 3 96

D 42 101 3 126 3 126

E 120 0 1 120 3 360

F 24 11111 5 120 3 72

K 7 111101 6 42 3 21

L 42 110 3 126 3 126

U 37 100 3 111 3 111

Z 2 111100 6 12 3 6

TOTAL: 785 918

Page 16: Binary Trees

Heap• Is a complete binary tree with the heap property

– min-heap : All values are less than the child values.

– Max-heap: all values are greater than the child values.

• The values in a heap are partially ordered. There is a relationship between a node’s value and the value of it’s children nodes.

• Representation: Usually the array based complete binary tree representation.

Page 17: Binary Trees

Building the Heap

• Several ways to build the heap. As we add each node, or create “a” tree as we get all the data and then “heapify” it.

• More efficient if we wait until all data is in.

1

4 5

2

6 7

3

1

4 2

5

6 3

7

7

4 2

5

6 3

1

7

4 2

5

1 3

6

Page 18: Binary Trees

Heap ADTclass heap{private:

ELEM* Heap;int size;int n;void siftdown(int);

public:heap(ELEM*, int, int);int heapsize() const;bool isLeaf(int) const;int leftchild(int) const;int rightchild(int) const;int parent(int) const;void insert(const ELEM);ELEM removemax();ELEM remove(int);void buildheap();};

Page 19: Binary Trees

Siftdown

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.

void heap::buildheap(){for (int i=n/2-1; i>=0; i--) siftdown(i);}

void heap::siftdown(int pos){assert((pos>=0) && (pos<n));while (!isleaf(pos)) {

int j=leftchild(pos);if ((j<(n-1) && key(Heap[j])<key(Heap[j+1])))

j++; // j now has position of child with greater valueif (key(Heap[pos])>=key(Heap[j])) return;swap(Heap[pos], Heap[j]);pos=j; } }

Page 20: Binary Trees

Priority Queues

• A priority queue stores objects and on request, releases the object with the 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.• To support priority reordering, delete and re-insert. Need to know

index for the object.ELEM heap::remove(int pos) {

assert((pos>0) && (pos<n));swap (Heap[pos], Heap[--n]);if (n!=0)

siftdown(pos);return Heap[n];

}