4969f9f153
* Add bfs to binary tree - `Fixes #2733` * Add note on the equivalence of dfs and inorder - `Fixes #2733` Co-authored-by: Yang Libin <szuyanglb@outlook.com> |
||
---|---|---|
.. | ||
AVLSimple | ||
AVLTree.java | ||
BinaryTree.java | ||
BSTIterative.java | ||
BSTRecursive.java | ||
BSTRecursiveGeneric.java | ||
CeilInBinarySearchTree.java | ||
FenwickTree.java | ||
GenericTree.java | ||
LCA.java | ||
LevelOrderTraversal.java | ||
LevelOrderTraversalQueue.java | ||
PrintTopViewofTree.java | ||
README.md | ||
RedBlackBST.java | ||
SegmentTree.java | ||
TreeTraversal.java | ||
TrieImp.java | ||
ValidBSTOrNot.java | ||
VerticalOrderTraversal.java |
Tree
Description
Tree is a data structure where the data is organized in a hierarchial structure. There should be one root node (which does not have any parent) and all subsequent nodes are represented as children of the root node and its children. If a node has at least one child, it is called internal
node and nodes with no children are called leaf
nodes.
Basic Structure
class Tree<E>{
E value;
Tree left;
Tree right;
}
This basic structure is for a binary tree where each internal tree has at least one and at most two children. left
and right
represent the two children and value
is the placeholder for data.
Properties
- Tree data structure gives the facility to organize data in a hierarchial structure
- Tree nodes can be inserted in a sorted order which can be used for searching and inserting data in O(logN) time where N is the number of nodes.
Types of Trees
- Binary Search Tree: A binary tree where the elements are inserted in asorted order. Here the searching can be done in O(logN) time in (depending on the structure)
- AVL Tree and Red-Black Tree: Binary search trees where the height is balanced. Here, searching is guaranteed to be in O(logN) time.
- Traversal algorithms:
a. BFS: Breadth-first-search where all the children at each level are traversed at once.
b. DFS: Depth-first-search where the first discovered child is traversed first. - MultiWay Search Tree: Tree in sorted order, but more than two children in each internal node.
- Trie: A character based multiway search tree where words can be retrieved based on their prefix. Useful for implementing prefix based search algorithm.