binary search trees a binary tree:a binary tree: –no node has more than two child nodes (called...

13
Binary Search Trees A binary tree: A binary tree: No node has more than two child nodes No node has more than two child nodes (called child subtrees). (called child subtrees). Child subtrees must be differentiated, Child subtrees must be differentiated, into: into: Left-child subtree Left-child subtree Right-child subtree Right-child subtree A search tree: A search tree: For every node, p: For every node, p: All nodes in the left subtree are All nodes in the left subtree are < < p p All nodes in the right subtree are All nodes in the right subtree are > > p p

Upload: alberta-mckenzie

Post on 02-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Binary Search Trees

• A binary tree:A binary tree:– No node has more than two child nodes (called child No node has more than two child nodes (called child

subtrees).subtrees).– Child subtrees must be differentiated, into: Child subtrees must be differentiated, into:

• Left-child subtreeLeft-child subtree• Right-child subtreeRight-child subtree

• A search tree:A search tree:– For every node, p:For every node, p:

• All nodes in the left subtree are All nodes in the left subtree are << p p • All nodes in the right subtree are All nodes in the right subtree are >> p p

Page 2: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Binary Search Tree - Example

AlexAlex

AngelaAngelaAbnerAbner

AbigailAbigail AdelaAdela

AdamAdam AgnesAgnes

AliceAlice

AllenAllen

AudreyAudrey

ArthurArthur

Page 3: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Binary Search Trees (cont)

• Searching for a value is in a tree of Searching for a value is in a tree of NN nodes nodes is:is:– O(log O(log NN) if the tree is “balanced”) if the tree is “balanced”– O(O(NN) if the tree is “unbalanced”) if the tree is “unbalanced”

Page 4: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

“Unbalanced” Binary Search Trees

• Below is a binary search tree that is NOT Below is a binary search tree that is NOT “balanced”“balanced”

AlexAlex

AngelaAngela

AbnerAdam

AbigailAbigail

AdelaAdelaAdamAbner

AgnesAgnes

AliceAlice

AllenAllen

AudreyAudrey

ArthurArthur

Page 5: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Properties of Binary Trees

• A binary tree is a A binary tree is a fullfull binary tree if and only binary tree if and only if:if:– Each non leaf node has Each non leaf node has exactly twoexactly two child nodes child nodes– All leaf nodes have identical path lengthAll leaf nodes have identical path length

• It is called It is called fullfull since all possible node slots since all possible node slots are occupiedare occupied

Page 6: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

A Full Binary Tree - Example

Page 7: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Full Binary Trees

• A Full binary tree of height h will have A Full binary tree of height h will have how many leaveshow many leaves?

• A Full binary tree of height h will have A Full binary tree of height h will have how many nodeshow many nodes?

Page 8: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Complete Binary Trees

• A A completecomplete binary tree (of height h) binary tree (of height h) satisfies the following conditions:satisfies the following conditions:– Level 0 to h-1 represent a fullLevel 0 to h-1 represent a full binary tree of binary tree of

height h-1height h-1– One or more nodes in level h-1 may have 0, or One or more nodes in level h-1 may have 0, or

1 child nodes1 child nodes– If If j,kj,k are nodes in level h-1, then are nodes in level h-1, then jj has more has more

child nodes than k if and only if child nodes than k if and only if jj is to the left is to the left of kof k

Page 9: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Complete Binary Trees - Example

BB

AA

CC

DD EE

HH II JJ KK

FF GG

Figure 13.8 A complete binary treeFigure 13.8 A complete binary tree

Page 10: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Complete Binary Trees (cont)

• Given a set of N nodes, a complete binary Given a set of N nodes, a complete binary tree of these nodes provides the maximum tree of these nodes provides the maximum number of leaves with the minimal average number of leaves with the minimal average path length (per node)path length (per node)

• The complete binary tree containing n The complete binary tree containing n nodes must have at least one path from root nodes must have at least one path from root to leaf of length to leaf of length log nlog n

Page 11: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Height-balanced Binary TreeHeight-balanced Binary Tree

• A A height-balancedheight-balanced binary tree is a binary binary tree is a binary tree such that:tree such that:– The left & right subtrees for any given node The left & right subtrees for any given node

differ in height by no more than onediffer in height by no more than one

• Note: Each complete binary tree is a height-Each complete binary tree is a height-balanced binary treebalanced binary tree

Page 12: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Height-balanced Binary Tree - ExampleHeight-balanced Binary Tree - Example

N M

N-M<=1

Height balanced is a local property

Page 13: Binary Search Trees A binary tree:A binary tree: –No node has more than two child nodes (called child subtrees). –Child subtrees must be differentiated,

Advantages of Height-balanced Binary TreesAdvantages of Height-balanced Binary Trees

• Height-balanced binary trees are “balanced”Height-balanced binary trees are “balanced”• Operations that run in time proportional to Operations that run in time proportional to

the height of the tree are the height of the tree are O(log n)O(log n), n the , n the number of nodesnumber of nodes with limited performance limited performance variancevariance

• Variance is a very important concern in real Variance is a very important concern in real time applications, e.g. connecting calls in a time applications, e.g. connecting calls in a telephone networktelephone network