Unit 5 Ds
Unit 5 Ds
Tree Terminologies:
• Tree data structure is a hierarchical structure that is used to represent
and organize data in a way that is easy to navigate and search.
• It is a collection of nodes that are connected by edges and has a
hierarchical relationship between the nodes.
• The topmost node of the tree is called the root, and the nodes below it are
called the child nodes. Each node can have multiple child nodes, and
these child nodes can also have their own child nodes, forming a
recursive structure.
struct Node {
int data;
struct Node* first_child;
struct Node* second_child;
struct Node* third_child;
.
.
.
struct Node* nth_child;
};
Types of Trees: General tree, binary tree, binary search tree
• In the data structure, General tree is a tree in which each node can have
either zero or many child nodes.
• It can not be empty.
• In general tree, there is no limitation on the degree of a node. The
topmost node of a general tree is called the root node.
• There are many subtrees in a general tree. The subtree of a general tree is
unordered because the nodes of the general tree can not be ordered
according to specific criteria.
• In a general tree, each node has in-degree(number of parent nodes) one
and maximum out-degree(number of child nodes) n.
Binary tree
The subtree of a general tree do While the subtree of binary tree hold the ordered
not hold the ordered property. property.
In general tree, there is either While in binary tree, there are mainly two
zero subtree or many subtree. subtree: Left-subtree and Right-subtree.
Binary search tree
Applications of BST
Together with these, BST also allows sorted order traversal of data in O(n) time.
1. A Self-Balancing Binary Search Tree is used to maintain sorted stream of
data. For example, suppose we are getting online orders placed and we
want to maintain the live data (in RAM) in sorted order of prices. For
example, we wish to know number of items purchased at cost below a
given cost at any moment. Or we wish to know number of items
purchased at higher cost than given cost.
2. A Self-Balancing Binary Search Tree is used to implement doubly ended
priority queue. With a Binary Heap, we can either implement a priority
queue with support of extractMin() or with extractMax(). If we wish to
support both the operations, we use a Self-Balancing Binary Search Tree
to do both in O(Log n)
3. There are many more algorithm problems where a Self-Balancing BST is
the best suited data structure, like count smaller elements on
right, Smallest Greater Element on Right Side, etc.
4. A BST can be used to sort a large dataset. By inserting the elements of
the dataset into a BST and then performing an in-order traversal, the
elements will be returned in sorted order. When compared to normal
sorting algorithms, the advantage here is, we can later insert / delete items
in O(Log n) time.
5. Variations of BST like B Tree and B+ Tree are used in Database
indexing.
6. TreeMap and TreeSet in Java, and set and map in C++ are internally
implemented using self-balancing BSTs, more formally a Red-Black
Tree.
Binary tree traversal:
i) In order traversal
ii)preorder traversal
iii)Post order traversal
Inorder Traversal:
Inorder traversal visits the node in the order: Left -> Root -> Right
Preorder traversal visits the node in the order: Root -> Left -> Right
Postorder traversal visits the node in the order: Left -> Right -> Root
In the Next step, an operator ‘*’ will going read, so two pointers to trees are
popped, a new tree is formed and a pointer to it is pushed onto the stack
In the Next step, an operator ‘+’ will read, so two pointers to trees are popped,
a new tree is formed and a pointer to it is pushed onto the stack.
Similarly, as above cases first we push ‘D’ into the stack and then in the last
step first, will read ‘/’ and then as previous step topmost element will pop out
and then will be right subtree of root ‘/’ and other nodes will be right
subtree.