Vous êtes sur la page 1sur 29

TREES

Nature Lover’s View Of A Tree

leaves

branches

root
Computer Scientist’s View

root
leaves

branches

nodes
Linear Lists And Trees
• Linear lists are useful for serially ordered
data
– (e0, e1, e2, …, en-1)
– Days of week
– Months in a year
– Students in this class
• Trees are useful for hierarchically ordered
data
– Employees of a corporation
• President, vice presidents, managers, and so on
Hierarchical Data And Trees

• The element at the top of the hierarchy is


the root
• Elements next in the hierarchy are the
children of the root
• Elements next in the hierarchy are the
grandchildren of the root, and so on.
• Elements that have no children are leaves
Definition
• A tree t is a finite nonempty set of elements.
• One of these elements is called the root.
• The remaining elements, if any, are
partitioned into trees, which are called the
subtrees of t.
/a Root

b i
j k
c f
d e g h Subtrees
TREES

Caution: Some
texts start level
numbers at 0
rather than at 1

Terminology
• Node • Sibling
• Edge • Degree of a Tree
• Path • Ancestor
• Degree of a Node • Level
• Leaf or Terminal node • Height/Depth of a Tree
• Parent and Children • Forest
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  the left and right subtrees of the binary
tree
• Any node can have at most 2 children

• Node C: Left Successor of node B


• Node D: Right Successor of node B
• Similar Binary trees (trees (a), (c) and (d))
• Copies (trees (a) and (c))
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 a a

b b
• are different when viewed as binary trees
• are the same when viewed as trees
Binary Tree Form and its Merits
• (a + b) * (c – d) / (e + f)
// The terms that we
introduced for
trees, such as
* + degree, level,
height, leaf, child
e f etc. all apply to
+ - binary tree in the
a b c d same way

• Left and right operands are easy to visualize


• Code optimization algorithms work with the binary
tree form of an expression
• Simple recursive evaluation of expression
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
Maximum Number Of Nodes
• All possible nodes at first h levels are present
• The maximum
number of nodes
on level i of a
binary tree is 2i-1

Maximum number of nodes of a binary tree of height h


= 1 + 2 + 4 + 8 + … + 2h-1
= 2h - 1
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 height of a tree with n nodes is n
(same as a linked list)
• The min height of a tree with n nodes is
log2(n+1)
Special kinds of Binary Trees
• Extended Binary Trees (2 - Trees)
• Full Binary Tree
• Complete Binary Tree
• Skewed Tree
a 1Right Skewed Tree
b 3
b 7
c
c
15
d
Left Skewed Tree
Extended Binary Trees (2 - Trees)
A binary tree T is said to be a 2 – tree, If each
node has either 0 or 2 children

Tree corresponding to any Algebraic Expression


which uses only binary operations
//

* +
e f
+ -
a b c d
Extended Binary Trees

• We can always get an extended binary tree


from a binary tree
• Start with any binary tree and add an
external node wherever there is an empty
subtree
• Result is an extended binary tree
A Binary Tree

An Extended Binary Tree

number of external
nodes is n+1
Full Binary Tree

• A full binary tree of a given height h has


2h – 1 nodes

Height 4 full binary tree


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
Node Number Properties
1
Let n be the number of
2 3 nodes in a binary tree
whose height is h.
4 5 6 7
8 9 10 11 12 13 14 15

• 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
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
• In other words: Complete Binary Tree
– If all its levels, except possibly the last, have
the max no. of possible nodes, and
– If all the nodes at the last level appear as far
left as possible
Example
1

2 3
• Complete
binary tree
4 5 6 7 with 10 nodes
8 9 10 11 12 13 14 15

The depth of a
Complete Binary Tree
with N nodes
is given by log2N +1
If N=1 000 000, then its
depth is 20
Binary Tree Representation

• Array/Sequential Representation
• Linked Representation
Array Representation
• Number the nodes using the numbering scheme
for a full binary tree. The node that is numbered i
is stored in tree[i].
a1

2 3
b c

4 5 6 7
d e f g
8 9 10
h i j
tree[] a b c d e f g h i j
0 5 10
Right-Skewed Binary Tree

a1
b 3
7
c
15
d

tree[] a - b - - - c - - - - - - - d
0 5 10 15

• An n node binary tree needs an array


whose length is between n+1 and 2n.
Binary Trees- Linked Representation

• Each node will have 3 fields:


leftChild: contains the location of the left child
data/info: contains the data at this node
rightChild: contains the location of the right child
of this node
We also need a pointer variable root or T

In actual practice, an entire record may be stored at the node


Linked Representation Example
root a leftChild
data
rightChild
b c

d e
struct node {
dataType data;
struct node *left, *right; g
f };
struct node *tree; h
In actual practice, an entire record may be stored at the node
Binary Trees- Linked Representation
• Other examples:
We also need a pointer variable ROOT or T or tree
Some Binary Tree Operations
• Determine the height.
• Determine the number of nodes.
• Make a clone.
• Determine if two binary trees are clones.
• Display the binary tree.
• Evaluate the arithmetic expression
represented by a binary tree.
• Obtain the infix form of an expression.
• Obtain the prefix form of an expression.
• Obtain the postfix form of an expression.

Vous aimerez peut-être aussi