Dsa 2
Dsa 2
Associate Professor
Galgotias University, Greater Noida
• Imagine that you are hired by company XYZ to organize all of their records
into a computer database.
• The first thing you are asked to do is create a database of names with all the
company's management and employees.
• To start your work, you make a list of everyone in the company along with
their position and other details.
• But this list only shows one view of the company. You also want your
database to represent the relationships between management and
employees at XYZ.
• Although your list contains both name and position, it does not tell
you which managers are responsible for which workers and so on.
• After thinking about the problem for a while, you decide that a tree
diagram is a much better structure for showing the work
relationships at XYZ.
• A tree is connected in the sense that if we start at any node n other than the
root, move to the parent of n, to the parent of the parent of n, and so on,
we eventually reach the root of the tree.
• For instance, starting at n7, we move to its parent, n4, and from there to n4’s
parent, which is the root, n1.
• n1, n2, n6 is a path of length 2 from the root n1 to the node n6.
• A binary tree is a tree in which no node can have more than two subtrees. In
other words, a node can have zero, one or two subtrees.
• The tree ADT stores elements at positions, which are defined relative to
neighboring positions.
• Positions in a tree are its nodes, and the neighboring positions satisfy
the parent-child relationships that define a valid tree.
• Tree nodes may store arbitrary objects.
• The level order traversal algorithm visits the root, then visits each
element on the first level, then visits each element on the second
level, and so forth, each time visiting all the elements on one level
before going down to the next level.
• If the tree is drawn in the usual manner with its root at the top and
leaves near the bottom, then the level order pattern is the same left-
to-right top-to-bottom pattern that you follow to read English text.
• A binary tree is either the empty set or a triple T = (x, L, R), where x is a node
and L and R are disjoint binary trees, neither of which contains x.
• The node x is called the root of the tree T, and the subtrees L and R are
called the left subtree and the right subtree of T rooted at x.
• The definitions of the terms size, path, length of a path, depth of a node,
level, height, interior node, ancestor, descendant, and subtree are the same
for binary trees as for general trees.
• That is, binary trees are not just trees all of whose nodes have two or fewer
children.
• Not only are the two trees in the above Figure are different from each other,
but they have no relation to the ordinary tree consisting of a root and a single
child of the root:
• A binary tree is said to be full if all its leaves are at the same level and every
interior node has two children.
• A complete binary tree is either a full binary tree or one that is full except
for a segment of missing leaves on the right side of the bottom level.
• Figure above shows the incomplete binary tree and the natural mapping of its
nodes into an array which leaves some gaps.
Parent
Data
Left Right
• Advantages
• Direct Access
• Finding the Parent / Children is fast
• Disadvantages
• Wastage of memory
• Insertion and Deletion will be costlier
• Array size and depth
• Advantages
• No wastage of memory
• Insertion and Deletion will be easy
• Disadvantages
• Does not provide direct access
• Additional space in each node.
• The three traversal algorithms that are used for general trees (see Chapter
10) apply to binary trees as well: the preorder traversal, the postorder
traversal, and the level order traversal.
• In addition, binary trees support a fourth traversal algorithm: the inorder
traversal. These four traversal algorithms are given next.
1. Initialize a queue.
2. Enqueue the root.
3. Repeat steps 4–7 until the queue is empty.
4. Dequeue a node x from the queue.
5. Visit x.
6. Enqueue the left child of x if it exists.
7. Enqueue the right child of x if it exists.