Professional Documents
Culture Documents
Data Structures: Binary Tree
Data Structures: Binary Tree
Binary Tree
Binary
Trees
A structure containing nodes with more than one self-referenced
field. A binary tree is made of nodes, where each node contains
a "left" reference, a "right" reference, and a data element. The
topmost node in the tree is called the root.
Any node can have at most two branches, i.e.,there is no node
with degree greater than two. For binary trees we distinguish
between the subtree on the left and on the right, whereas for
trees the order of the subtree was irrelevant. Also a binary tree
may have zero nodes.
Definition: A binary tree is a finite set of nodes which is either
empty or consists of a root and two disjoint binary trees called
the left subtree and the right subtree.
Every
nodeTrees
(excluding a root) in a tree is connected by a
Binary
directed
edge from exactly one other node. This node is called a
parent. On
the other hand, each node can be connected to arbitrary
number
of nodes, called children.
leaves or external nodes
Nodes with no children are called leaves, or external nodes.
Nodes which are not leaves are called internal nodes.
Siblings
Nodes with the same parent are called siblings.
Binary Trees
Binary trees are characterized by the fact that
any node can have at most two branches
Definition (recursive):
A binary tree is a finite set of nodes that is either
empty or consists of a root and two disjoint binary
trees called the left subtree and the right subtree
B
B
Any tree can be transformed into
binary tree
Binary Trees
The abstract data type of binary tree
Binary Trees
Two special kinds of binary trees:
(a) skewed tree, (b) complete binary tree
The all leaf nodes of these trees are on two adjacent levels
Binary Trees
Level 1
Level 2
D
Level 3
E
6
Binary Trees
Binary tree representations (using array)
Waste spaces: in the worst case, a skewed tree of
depth k requires 2k-1 spaces. Of these, only k
spaces will be occupied
Insertion or deletion
of nodes from the
middle of a tree
requires the
movement of
potentially many nodes
to reflect the change in
the level of these nodes
Binary Trees
Binary tree representations (using link)
Binary Trees
Binary tree representations (using link)
h = O(log n)
Algorithm
structure BTREE
declare CREATE( ) --> btree
ISMTBT(btree,item,btree) --> boolean
MAKEBT(btree,item,btree) --> btree
LCHILD(btree) --> btree
DATA(btree) --> item
RCHILD(btree) --> btree
for all p,r in btree, d in item let
ISMTBT(CREATE)::=true
ISMTBT(MAKEBT(p,d,r))::=false
LCHILD(MAKEBT(p,d,r))::=p; LCHILD(CREATE)::=error
DATA(MAKEBT(p,d,r))::d; DATA(CREATE)::=error
RCHILD(MAKEBT(p,d,r))::=r; RCHILD(CREATE)::=error
end
end BTREE
Advantages of trees
Trees are so useful and frequently used, because
they have some very serious advantages:
Trees reflect structural relationships in the data.
Trees are used to represent hierarchies.
Trees provide an efficient insertion and searching.
Trees are very flexible data, allowing to move
subtrees around with minimum effort .