anujparikh / trees

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tree Data Structure

Introduction

A tree is a collection of entities called nodes. Nodes are connected by edges. Each node contains a value or data, and it may or may not have a child node . Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical data structures

  • The first node of the tree is called the root. If this root node is connected by another node, the root is then a parent node and the connected node is a child.
  • All Tree nodes are connected by links called edges. It’s an important part of trees, because it’s manages the relationship between nodes.
  • Leaves are the last nodes on a tree. They are nodes without children.
  • The height of a tree is the length of the longest path to a leaf
  • The depth of a node is the length of the path to its root

Types of Trees

Binary Trees

  • A binary tree is a tree data structure in which each node has at the most two children, which are referred to as the left child and the right child.
  • Binary tree contains following parts
    • Data
    • Pointer to left child
    • Pointer to right child
  • Properties of Binary Tree
    • The maximum number of nodes at level l of a binary tree is 2^l.
    • Maximum number of nodes in a binary tree of height h is 2^(h – 1) (Considering root node as h = 1)
    • Maximum number of nodes in a binary tree of height h is 2^(h + 1) - 1 (Considering root node as h = 0)
    • Maximum number of internal node in a binary tree of height h is 2^(h) - 1
    • A binary tree with n leaves has height at least log(n)
  • Types of Binary Tree
    • Full Binary Tree: A Binary Tree is full if every node has 0 or 2 children. We can also say a full binary tree is a binary tree in which all nodes except leaves have two children
    • Complete Binary Tree: A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible
    • Perfect Binary Tree: A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at the same level
    • Balanced Binary Tree: A binary tree is balanced if the height of the tree is O(Log n) where n is the number of nodes. Balanced Binary Search trees are performance wise good as they provide O(log n) time for search, insert and delete
    • Degenerated Binary Tree: A Tree where every internal node has one child. Such trees are performance-wise same as linked list

Binary Search Tree

  • Binary Search Tree is a node-based binary tree data structure which has the following properties
    • The left subtree of a node contains only nodes with keys lesser 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 each must also be a binary search tree
    • There must be no duplicate nodes

Tree Traversal

  • In-order traversal In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself. If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order
  • Pre-order traversal In this traversal method, the root node is visited first, then the left subtree and finally the right subtree
  • Post-order traversal In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node

Examples

About


Languages

Language:Java 100.0%