JavaAlgorithms/DataStructures/Trees
2021-10-20 18:38:21 +03:00
..
AVLSimple Resolve build errors & cleanup structure (#2334) 2021-09-26 12:26:59 +03:00
AVLTree.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
BinaryTree.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
BSTIterative.java Fix package declarations (#2576) 2021-10-16 16:43:51 +03:00
BSTRecursive.java Add Implementation of Tree Sort and Generic Type BST (#2638) 2021-10-20 18:38:21 +03:00
BSTRecursiveGeneric.java Add Implementation of Tree Sort and Generic Type BST (#2638) 2021-10-20 18:38:21 +03:00
CeilInBinarySearchTree.java Add ceil value in a Binary Search Tree (#2399) 2021-10-10 08:49:22 +03:00
FenwickTree.java Add Fenwick Tree (#2570) 2021-10-20 11:19:30 +03:00
GenericTree.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
LCA.java Fix package declarations (#2576) 2021-10-16 16:43:51 +03:00
LevelOrderTraversal.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
LevelOrderTraversalQueue.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
PrintTopViewofTree.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
README.md Add README for Trees (#2422) 2021-10-08 19:50:57 +03:00
RedBlackBST.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
TreeTraversal.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
TrieImp.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
ValidBSTOrNot.java Formatted with Google Java Formatter 2020-10-24 10:23:28 +00:00
VerticalOrderTraversal.java Add Vertical Order Traversal (#2585) 2021-10-14 17:33:51 +03:00

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

  1. Tree data structure gives the facility to organize data in a hierarchial structure
  2. 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

  1. 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)
  2. AVL Tree and Red-Black Tree: Binary search trees where the height is balanced. Here, searching is guaranteed to be in O(logN) time.
  3. 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.
  4. MultiWay Search Tree: Tree in sorted order, but more than two children in each internal node.
  5. Trie: A character based multiway search tree where words can be retrieved based on their prefix. Useful for implementing prefix based search algorithm.