Hi ,
In this post I am going to tell you something about Tree.
Tree is a Data-structure used when need good performance for ..
- To add an Element
- To remove an Element
- To containment test.
In java we use TreeSet, It is an implementation for Set Interface.
This class provides Set implementation by inter usage of Tree datastructure.
When we go for usage of this Tree, We must know something about it.
We start with
Tree:
Tree:
A tree is a collection of nodes. The collection can be empty. or
A tree consists of a distinguished node r, called root, zero or more noneempty (sub)trees T1. or
A tree is recursively.
A tree can have any number of child nodes.
for example:
Usage:
Trees can be used to evaluate arithmetic expressions.
Trees are used to implement the file system of several popular operation system.
Binary Tree:
Binary trees are slightly differ from normal Trees.
In Binary tree , the maximum number of child nodes at any level will be Two.
A binary tree is a tree data structure
in which each node has at most two child nodes
, usually distinguished as "left" and "right". Nodes with children are parent nodes
,
A binary tree is made of nodes, where each node contains a "left"
pointer, a "right" pointer, and a data element. The "root" pointer
points to the topmost node in the tree. The left and right pointers
recursively point to smaller "subtrees" on either side. A null pointer
represents a binary tree with no elements -- the empty tree. The formal
recursive definition is: a binary tree is
either empty (represented by a null pointer), or is made of a single
node, where the left and right pointers (recursive definition ahead)
each point to a binary tree.
So that, It is called BianaryTree.
Binary ( two --> either 0 or 1 like)
Element insertion will be bases on left ---> right manner.
- A node contains two references (to left and right child nodes.
Binary Search Tree (BST):
A
binary search tree (
BST), sometimes also called an
ordered or
sorted binary tree, is a node-based binary tree data structure which has the following properties:
[1]
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- The left and right subtree must each also be a binary search tree.
- There must be no duplicate nodes.
Generally,
the information represented by each node is a record rather than a
single data element. However, for sequencing purposes, nodes are
compared according to their keys rather than any part of their
associated records.
The
major advantage of binary search trees over other data structures is
that the related sorting algorithms and search algorithms such
as in-order traversal can be very efficient.
This BST is came from Binary Tree, but it follows left --> right manner based on the value of element in every node.
Generally every node has 3 fields.
one - data ( mandatory filed)
two - link to left node (optional)
three - link to right node (optional)
suppose:
To insert some elements into a BST ....
elements - 37, 24, 42, 7, 2, 40, 43, 32, 120
the insertion will be like in the picture below
- start the root node with the value 37, because there is no root or child nodes.
-
next while inserting 24, check the value of root node, here root value
is higher the 24, then check for left node, here only one root node and
no child nodes, so this 24 will go to left to the root node.
-
similarly 42, it checks the value at root node, it is higher than the
value of root node, then check for any right child node, here no right
child node and only we have root and it's left child node, So thid 42
goes to right child of the root node.
- similarly following element insertion is same , It follows recursion at every node.
- like wise we create a tree as above in the picture.
Here For
retrieval of elements in the above tree, it follows
3 types of retrievals.
1) Pre-order ( data--> left --> right)
2) In-order ( left--> data --> right) , retrieved values are in a order. ( sorted)
3) Post-order (left--> right -->data)
from the above example :
pre-order will be : 37, 24, 7, 2, 32, 42, 40, 43, 120
in-order will be : 2,7,24, 32, 37, 40, 42, 43, 120 (sorted)*
post-order will be: 2, 7, 32, 24, 40, 120, 43, 42, 37
sources:
http://en.wikipedia.org/wiki/Binary_search_tree
http://www.iro.umontreal.ca/~pift1025/bigjava/Ch21/ch21.html
http://www.learnalgorithms.in