Ans. Binary Tree: Trees
Ans. Binary Tree: Trees
Ans.
Binary Tree
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.
Every node (excluding a root) in a tree is connected by a 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. Nodes with no children are
called leaves, or external nodes. Nodes which are not leaves are called internal nodes.
Nodes with the same parent are called siblings.
1. Array Representation
In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a binary
tree.
To represent a binary tree of depth 'n' using array representation, we need one dimensional array with
a maximum size of 2n + 1.
We use a double linked list to represent a binary tree. In a double linked list, every node consists of
three fields. First field for storing left child address, second for storing actual data and third for storing
follows...
Q2. Write short notes on:
a) Red-Black trees
b) splay trees
Ans (a).
Red-Black Trees
A red-black tree is a binary search tree with one extra attribute for each node:
the colour, which is either red or black. We also need to keep track of the parent of
each node, so that a red-black tree's node structure would be:
struct t_red_black_node {
enum { red, black } colour;
void *item;
struct t_red_black_node *left,
*right,
*parent;
}
For the purpose of this discussion, the NULL nodes which terminate the tree are
considered to be the leaves and are coloured black.
The number of black nodes on any path from, but not including, a node x to a leaf is
called the black-height of a node, denoted bh(x). We can prove the following lemma:
Ans (b). A splay tree is just a binary search tree that has excellent performance
in the cases where some data is accessed more frequently than others. The tree
self-adjusts after lookup, insert and delete operations.
A splay tree does not necessarily have mininum height, nor does it
necessarily have logarithmically bounded height.In fact a splay tree may
even be degenerate. However, it is based on the idea that if you recently used
something you'll likely need it again soon, so it keeps the most commonly used
data near the top where it is accessed most quickly.
How it Works
After each insert, delete, or lookup, you splay:
Ans.
We construct a binary search tree for the given elements.
We write the inorder traversal sequence from the binary search tree so obtained.
Inorder sequence: D B E A F C
Preorder sequence: A B D E C F