Tree (18M) Visit for more Learning Resources Data structure
Store and organize data in computer.
Linear Data Structure Arrays Linked List Stacks Queues Logic of Tree Logic of Tree Tree Definition
Tree is a non-linear data structure which
organizes data in hierarchical structure and this is a recursive definition. Tree terminology... 1.Root: The first node is called as Root Node. Every tree must have root node, there must be only one root node. Root node doesn't have any parent. 2. Edge In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges. 3. Parent In a tree data structure, the node which is predecessor of any node is called as PARENT NODE. In simple words, the node which has branch from it to any other node is called as parent node. Parent node can also be defined as "The node which has child / children". 4. Child The node which has a link from its parent node is called as child node. In a tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are child nodes. 5. Siblings The nodes with same parent are called as Sibling nodes. 6. Leaf Node The node which does not have a child is called as LEAF Node. The leaf nodes are also called as External Nodes or 'Terminal' node. 7. Internal Nodes An internal node is a node with atleast one child. Nodes other than leaf nodes are called as Internal Nodes. The root node is also said to be Internal Node if the tree has more than one node. Internal nodes are also called as 'Non- Terminal' nodes. 8. Degree In a tree data structure, the total number of children of a node is called as DEGREE of that Node. 9. Level In a tree data structure, the root node is said to be at Level 0 and the children of root node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each level (Step). 10. Height the total number of egdes from leaf node to a particular node in the longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to be height of the tree. 11. Depth In a tree data structure, the total number of egdes from root node to a particular node is called as DEPTH of that Node. 12. Path In a tree data structure, the sequence of Nodes and Edges from one node to another node is called as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J has length 4. 13. Sub Tree Every child node will form a subtree on its parent node. Type of Trees • General tree • Binary tree • Binary Search Tree General Tree • A general tree is a data structure in that each node can have infinite number of children . • In general tree, root has in-degree 0 and maximum out-degree n. • Height of a general tree is the length of longest path from root to the leaf of tree. Height(T) = {max(height(child1) , height(child2) , … height(child-n) ) +1} • Binary tree • A Binary tree is a data structure in that each node has at most two nodes left and right • In binary tree, root has in-degree 0 and maximum out-degree 2. • In binary tree, each node have in-degree one and maximum out- degree 2. • Height of a binary tree is : Height(T) = { max (Height(Left Child) , Height(Right Child) + 1} Representation of Binary Tree 1. Array Representation 2. Linked List Representation. Complete Binary Tree
A complete binary tree is a tree in which
1.All leaf nodes are at n or n-1 level 2.Levels are filled from left to right Full Binary Tree
A full binary tree (sometimes proper binary tree or 2-tree) is
a tree in which every node other than the leaves has two children or no childeren. Every level is completely filled up. No of nodes= 2h+1 -1 Skewed Binary Tree
A binary tree is said to be Skewed Binary Tree if every node in
the tree contains either only left or only right sub tree. If the node contains only left sub tree then it is called left-skewed binary tree and if the tree contains only right sub tree then it is called right-skewed binary tree. Strictly Binary Tree A nod will have either two children or no child at all. Binary Tree Traversal
1. Preorder traversal:-In this traversal method first
process root element, then left sub tree and then right sub tree. Procedure:- Step 1: Visit root node Step 2: Visit left sub tree in preorder Step 3: Visit right sub tree in preorder 2. Inorder traversal:-
In this traversal method first process left element,
then root element and then the right element. Procedure:- Step 1: Visit left sub tree in inorder Step 2: Visit root node Step 3: Visit right sub tree in inorder 3. Postorder traversal:-
In this traversal first visit / process left sub tree,
then right sub tree and then the root element. Procedure:- Step 1: Visit left sub tree in postorder Step 2: Visit right sub tree in postorder Step 3: Visit root node Binary Search Tree A binary search tree (BST) or "ordered binary tree" is a empty or in which each node contains a key that satisfies following conditions: 1. All keys are distinct.’ 2. , all elements in its left subtree are less to the node (<), and all the elements in its right subtree are greater than the node (>). Binary search tree Binary search tree property For every node X All the keys in its left subtree are smaller than the key value in X All the keys in its right subtree are larger than the key value in X Binary Search Trees