×

Close

Type:
**Other**Downloads:
**268**Views:
**3458**Uploaded:
**4 months ago**Add to Favourite

Touch here to read

Page-1

Topic/Link:

TREE DATA STRUCTURES
GENERAL DEFINITION : In computer science, a tree is a widely used abstract data type (ADT) that
simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node,
represented as a set of linked nodes.
RECURSIVE DEFINITION : A tree data structure can be defined recursively (locally) as a collection
of nodes (starting at a root node), where each node is a data structure consisting of a value, together with a
list of references to nodes (the "children"), with the constraints that no reference is duplicated, and none
points to the root.
TERMINOLOGY USED IN TREES
Root: The top node in a tree.
Child: A node directly connected to another node when moving away from the Root.
Parent: The converse notion of a child.
Siblings: A group of nodes with the same parent.
Descendant: A node reachable by repeated proceeding from parent to child.
Ancestor: A node reachable by repeated proceeding from child to parent.
Leaf (or External node): A node with no children.
Branch (Internal node): A node with at least one child.
Degree: The number of sub trees of a node.
Edge: The connection between one node and another.
Path: A sequence of nodes and edges connecting a node with a descendant.
Level: The level of a node is defined by 1 + (the number of connections between the node and the root).
Height of node: The height of a node is the number of edges on the longest path between that node and a
leaf.
Height of tree: The height of a tree is the height of its root node.
Depth: The depth of a node is the number of edges from the tree's root node to the node.
Forest: A forest is a set of n ≥ 0 disjoint trees.
Common Operations
•
•
•
•
•
•
•
•
Enumerating all the items
Enumerating a section of a tree
Searching for an item
Adding a new item at a certain position on the tree
Deleting an item
Pruning: Removing a whole section of a tree
Grafting: Adding a whole section to a tree
Finding the root for any node
Common Uses
•
•
•
•
•
Representing hierarchical data
Storing data in a way that makes it efficiently searchable
Representing sorted lists of data
As a workflow for compositing digital images for visual effects
Routing algorithms

BINARY TREE DATA STRUCTURES
GENERAL DEFINITION : A binary tree is a tree data structure in which each node has at most two children,
which are referred to as the left child and the right child.
A binary tree may also be called a bifurcating arborescence.
RECURSIVE DEFINITION : A full binary tree is either
•
•
A single vertex.
A graph formed by taking two (full) binary trees, adding a vertex, and adding an edge directed from the
new vertex to the root of each binary tree.
An extended binary tree is defined as
•
•
the empty set is an extended binary tree
if T1 and T2 are extended binary trees, then denote by T1 • T2 the extended binary tree obtained by adding
a root r connected to the left to T1 and to the right to T2 by adding edges when these sub-trees are nonempty.
Types of BINARY TREES
•
•
A rooted binary tree has a root node and every node has at most two children.
A full binary tree (sometimes referred to as a proper or plane binary tree) is a tree in which every node
in the tree has either 0 or 2 children.
A full binary tree
•
•
A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the
same depth or same level. An example of a perfect binary tree is the ancestry chart of a person to a given
depth, as each person has exactly two biological parents.
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the
last level are as far left as possible. It can have between 1 and 2h nodes at the last level h. An alternative
definition is a perfect tree whose rightmost leaves (perhaps all) have been removed.
A complete binary tree

•
•
In the infinite complete binary tree, every node has two children. The set of all nodes is countably
infinite, but the set of all infinite paths from the root is uncountable, having the cardinality of the
continuum. These paths correspond by an order-preserving bijection to the points of the Cantor set, or to
the set of positive irrational numbers.
A balanced binary tree has the minimum possible maximum height (a.k.a. depth) for the leaf nodes,
because for any given number of leaf nodes, the leaf nodes are placed at the greatest height
possible.[clarification needed]
h
0:
Balanced
ABCDE
/
\
1:
ABCD
E
/
\
2: AB
CD
/ \ / \
3: A B C D
4:
•
Unbalanced, h = (n + 1)/2 - 1
ABCDE
/
\
ABCD
E
/
\
ABC
D
/
\
AB
C
/ \
A
B
A degenerate (or pathological) tree is where each parent node has only one associated child
node.This means that performance-wise, the tree will behave like a linked list data structure.
Properties of binary trees
•
•
The number of nodes in a full binary tree, is at least and at most , where is the height of the tree. A tree
consisting of only a root node has a height of 0.
The number of leaf nodes in a perfect binary tree, is because the number of non-leaf (a.k.a. internal)
•
•
nodes
.
This means that a perfect binary tree with leaves has nodes.
In a balanced full binary tree,
•
•
In a perfect full binary tree, thus
.
The maximum possible number of null links (i.e., absent children of the nodes) in a complete binary tree
of n nodes is (n+1), where only 1 node exists in bottom-most level to the far left.
•
•
The number of internal nodes in a complete binary tree of n nodes is
.
For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2, n0 = n2 + 1.
Common Operations
There are a variety of different operations that can be performed on binary trees.
Insertion
Nodes can be inserted into binary trees in between two other nodes or added after a leaf node. In binary
trees, a node that is inserted is specified as to which child it is.
•
Leaf nodes
To add a new node after leaf node A, A assigns the new node as one of its children and the new node
assigns node A as its parent.
•
Internal nodes

The process of inserting a node into a binary tree
Insertion on internal nodes is slightly more complex than on leaf nodes. Say that the internal node is node A
and that node B is the child of A. (If the insertion is to insert a right child, then B is the right child of A, and
similarly with a left child insertion.) A assigns its child to the new node and the new node assigns its parent to
A. Then the new node assigns its child to B and B assigns its parent as the new node.
Deletion
Deletion is the process whereby a node is removed from the tree. Only certain nodes in a binary tree can be
removed unambiguously.[27]
•
Node with zero or one children
The process of deleting an internal node in a binary tree
Suppose that the node to delete is node A. If A has no children, deletion is accomplished by setting the child
of A's parent to null. If A has one child, set the parent of A's child to A's parent and set the child of A's parent
to A's child.
•
Node with two children
In a binary tree, a node with two children cannot be deleted unambiguously.[27] However, in certain binary
trees (including binary search trees) these nodes can be deleted, though with a rearrangement of the tree
structure.
Traversal
Pre-order, in-order, and post-order traversal visit each node in a tree by recursively visiting each node in the
left and right subtrees of the root.
•
Depth-first order
In depth-first order, we always attempt to visit the node farthest from the root node that we can, but with the
caveat that it must be a child of a node we have already visited. Unlike a depth-first search on graphs, there is
no need to remember all the nodes we have visited, because a tree cannot contain cycles. Pre-order is a
special case of this. See depth-first search for more information.
•
Breadth-first order
Contrasting with depth-first order is breadth-first order, which always attempts to visit the node closest to the
root that it has not already visited.
Methods for storing binary trees
Binary trees can be constructed from programming language primitives in several ways.
Nodes and references
In a language with records and references, binary trees are typically constructed by having a tree node
structure which contains some data and references to its left child and its right child. Sometimes it also
contains a reference to its unique parent. If a node has fewer than two children, some of the child pointers
may be set to a special null value, or to a special sentinel node.
This method of storing binary trees wastes a fair bit of memory, as the pointers will be null (or point to the
sentinel) more than half the time; a more conservative representation alternative is threaded binary tree.[25]
In languages with tagged unions such as ML, a tree node is often a tagged union of two types of nodes, one
of which is a 3-tuple of data, left child, and right child, and the other of which is a "leaf" node, which contains

## Leave your Comments