31
Binary Trees

Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Embed Size (px)

Citation preview

Page 1: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Binary Trees

Page 2: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are partitioned into

two binary trees These are called the left and right subtrees of the

binary tree

Page 3: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Differences Between A Tree & A Binary Tree

No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree.

A binary tree may be empty; a tree cannot be empty.

The subtrees of a binary tree are ordered; those of a tree are not ordered.

Page 4: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Differences Between A Tree & A Binary Tree

The subtrees of a binary tree are ordered; those of a tree are not ordered

a

b

a

b

• Are different when viewed as binary trees

• Are the same when viewed as trees

Page 5: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Binary Trees Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure that

contains no nodes, or is comprised of three disjoint sets of nodes:

a root a binary tree called its left subtree a binary tree called its right subtree

A binary tree that contains no nodes is called empty Note: the position of a child matters!

Page 6: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Binary Trees

Full binary tree : All internal nodes have two children.

Complete binary tree : All leaves have the same depth All internal nodes have two children

A complete binary tree of height h has 2h-1 internal nodes and 2h leaves

Also: a binary tree with n nodes has height at least lgn

Page 7: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

What is a binary tree? Property1: each node can have up to two successor

nodes (children) The predecessor node of a node is called its parent The "beginning" node is called the root (no parent) A node without children is called a leaf

122232

<=20

Page 8: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Property1: each node can have up to two successor nodes (children)

The predecessor node of a node is called its parent The "beginning" node is called the root (no parent) A node without children is called a leaf

What is a binary tree?

Page 9: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Property2: a unique path exists from the root to every other node

What is a binary tree? (cont.)

Page 10: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Some terminology Ancestor of a node: any node on the path from the root to that

node Descendant of a node: any node on a path from the node to the

last node in the path Level (depth) of a node: number of edges in the path from the

root to that node Height of a tree: number of levels (warning: some books define

it as #levels - 1)

Page 11: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are
Page 12: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Properties Binary Trees

Because in a binary tree all the nodes have must have the same number of children we are forced to change the concepts slightly We say that all internal nodes have two children External nodes have no children

internal node

external node

Page 13: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Recursive definition of a Binary Tree

Most of concepts related to binary trees can be explained recursive

For instance, the definition of what is a binary tree can done recursively

A binary tree is: An external node, or An internal node connected to a left binary tree and a right binary tree

(called left and right subtrees) In programming terms we can see that our definition for a linked

list (singly) can be modified to have two links from each node instead of one.

Page 14: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Mathematical Properties of Binary Trees

Let's us look at some important mathematical properties of binary trees

A good understanding of these properties will help the understanding of the performance of algorithms that process trees

Some of the properties we'll describe relate also the structural properties of these trees. This is the case because performance characteristics of many algorithms depend on these structural properties and not only the number of nodes.

Page 15: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Minimum Number Of Nodes Minimum number of nodes in a binary tree

whose height is h. At least one node at each of first h levels.

minimum number of nodes is h

Page 16: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Maximum Number Of Nodes

All possible nodes at first h levels are present

Maximum number of nodes

= 1 + 2 + 4 + 8 + … + 2h-1 = 2h - 1

Page 17: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Number Of Nodes & Height

Let n be the number of nodes in a binary tree whose height is h.

h <= n <= 2h – 1

log2(n+1) <= h <= n The max heightmax height of a tree with N nodes is N (same

as a linked list) The min heightmin height of a tree with N nodes is log(N+1)

Page 18: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Relationship Between Number of Nodes (Internal - External) A binary tree with N internal nodes has N+1 external

nodes

Let's try to prove this using induction...

A binary tree with no internal nodes has one external node so the property holds for N=0

For N>0, any binary tree with N internal nodes has k internal nodes in the left subtree and N-k-1 in the right subtree, for k between 0 and N-1 (since the root is also an internal node).

By the hypothesis above the left subtree will have k+1 external nodes in the left subtree and N-k in the right subtree which gives a total of N+1 external nodes

Page 19: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Number of edges

A binary tree with N internal nodes has 2N edges

In any rooted tree, each node has a unique parent (except the root)

So there are N-1 edges connecting internal nodes

Also, each of the N+1 external nodes has one edge to its unique parent

So there are N+1 edge connecting external nodes to internal nodes

Combining the two we can see that we have 2N edges in the binary tree

Page 20: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Number of edges

A binary tree with N nodes (internal and external) has N-1 edges

If the tree has N nodes k are internal nodes. Since they all (except the root) have one connection to the parent

There are k-1 edges connecting internal nodes

Also, there are N-k external nodes and each of them have a connection to the parent

So there are N-k edges connecting external nodes to internal nodes

Combining the two we can see that we have N-1 edges in the binary tree

Page 21: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Some More Definitions

The level of a node in a tree is one higher that the level of its parent (root level being 0)

The height of a tree is the maximum of the levels of the tree's nodes

level 0level 0

level 3level 3

he

igh

t =

3

Page 22: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Full Binary Tree A full binary tree of a given height h has 2h – 1

nodes

Height 4 full binary tree

Page 23: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Numbering Nodes in a Full Binary Tree Number the nodes 1 through 2h – 1. Number by levels from top to bottom. Within a level number from left to right.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 24: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Node Number Properties

Parent of node i is node i/2 But node 1 is the root and has no parent

Left child of node i is node 2i But if 2i > n, node i has no left child

Right child of node i is node 2i+1 But if 2i+1 > n, node i has no right child

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 25: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Complete Binary Tree With n Nodes

Start with a full binary tree that has at least n nodes. Number the nodes as described earlier. The binary tree defined by the nodes numbered 1

through n is the unique n node complete binary tree.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Complete binary tree with 10 nodes

Page 26: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Binary Tree Representation

Array representation Linked representation

Page 27: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Array Representation

Number the nodes using the numbering scheme for a full binary tree

Store the node numbered i in tree[i]

tree[]0 5 10

a b c d e f g h i j

b

a

c

d e f g

h i j

1

2 3

4 5 6 7

8 9 10

Page 28: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Right-Skewed Binary Tree

An n node binary tree needs an array whose length is between n+1 and 2n

a

b

1

3

c7

d15

tree[]0 5 10

a - b - - - c - - - - - - -15d

Page 29: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Linked Representation

Each binary tree node is represented as an object whose data type is BinaryTreeNode

The space required by an n node binary tree is n * (space required by one node)

Page 30: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Binary Trees

A binary tree is a tree whose nodes have at most two offspring

Example

typedef … dataType;

struct nodeType {

dataType data;

struct nodeType *left, *right;

};

struct nodeType *tree;

Page 31: Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are

Linked Representation Example

a

cb

d

f

e

g

hleftChildelementrightChild

root