Download presentation
Presentation is loading. Please wait.
Published byTyler Lewis Modified over 9 years ago
1
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees
2
C o n f i d e n t i a l Developed By Nitendra Definition of Tree Terminologies Type of Trees – Complete Binary TreeComplete Binary Tree – Full/Strictly Binary TreeFull/Strictly Binary Tree – Binary Search TreeBinary Search Tree – Threaded Binary TreeThreaded Binary Tree – Red Black TreeRed Black Tree – B Tree [Balance Tree]B Tree [Balance Tree] – AVL TreeAVL Tree Binary Tree Traversals Application of Trees Class summary Trees NEXT PREVIOUS
3
C o n f i d e n t i a l Developed By Nitendra Definition of Tree : A general Tree T is a finite nonempty set of elements having unique connection between any pair of vertices. The set of elements are called vertices and connections are called edges. Trees A B C D E F G Tree T HOME NEXT PREVIOUS
4
C o n f i d e n t i a l Developed By Nitendra Trees Terminologies: BFE CDGH Internal node A root leaf parent child Grand Parent General Tree edge HOME NEXT PREVIOUS
5
C o n f i d e n t i a l Developed By Nitendra Trees Terminologies: Vertex: The vertex is the set of non empty elements with some value or weight. The plural form of vertex is vertices Edge: The connectivity between two edges called edge Root: The root is a special type of vertex having no parents Level of a vertex: The level of a vertex is the measured as the distance from the root and root is considered as the level 1. Degree of a vertex: The number of edges connected to that vertex HOME NEXT PREVIOUS
6
C o n f i d e n t i a l Developed By Nitendra Trees Type of Trees: Binary Tree Complete Binary Tree Full/Strict Binary Tree Binary Search Tree Threaded Binary Tree Red Black Tree B - Tree AVL Tree HOME NEXT PREVIOUS
7
C o n f i d e n t i a l Developed By Nitendra Trees Binary Tree A binary tree T is defined as a finite set of elements, called nodes such that T is empty, called NULL tree or empty tree T contains a distinguished node R, called root of T and the remaining nodes of T form an ordered pair of disjoint binary tree T1 and T2. The node contain max two children Examples: HOME NEXT PREVIOUS
8
C o n f i d e n t i a l Developed By Nitendra Trees Complete Binary Tree A full binary tree in which the number of nodes at any level i is 2 i-1, then the tree is said to be a complete binary tree. The complete binary tree is given below. In a complete binary tree, the total number of nodes at level 0 is 1 i.e., 2° Number of nodes at level 1 is 2 i.e., 2 1 Number of nodes at level 2 is 4 i.e., 2 2 Total number of nodes - = 2° + 2 1 + 2 2 + ………….2 d = 2 d+1 - 1 A B C E D H I J K F G L M N O HOME NEXT PREVIOUS
9
C o n f i d e n t i a l Developed By Nitendra Trees Full/Strictly Binary Tree If the degree of every node in a tree is either 0 or 2, then the tree is said to be full/strictly binary tree i.e., each node can have maximum two children or empty left and empty right child. Examples: F G B A C D E HOME NEXT PREVIOUS
10
C o n f i d e n t i a l Developed By Nitendra Trees Binary Search Tree A binary tree T is called a binary search tree or binary sorted tree if each node N of Tree has the following properties: The value at N is grater than every value in the left sub tree of N and is less than every value in the right sub tree of N. No duplicate value is available in Binary Search Tree. HOME NEXT PREVIOUS
11
C o n f i d e n t i a l Developed By Nitendra Trees Threaded Binary Tree A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node, and all left child pointers that would normally be null point to the inorder predecessor of the node. HOME NEXT PREVIOUS
12
C o n f i d e n t i a l Developed By Nitendra Trees Red Black Tree A red-black tree is a binary search tree where each node has a color attribute, the value of which is either red or black. In addition to the ordinary requirements imposed on binary search trees, the following additional requirements of any valid red-black tree apply. - A node is either red or black. - The root is black. (This rule is used in some definitions and not others. Since the root can always be changed from red to black but not necessarily vice- versa this rule has little effect on analysis.) - All leaves are black, even when the parent is black (The leaves are the null children.) - Both children of every red node are black. HOME NEXT PREVIOUS
13
C o n f i d e n t i a l Developed By Nitendra Trees - Every simple path from a node to a descendant leaf contains the same number of black nodes, either counting or not counting the null black nodes. (Counting or not counting the null black nodes does not affect the structure as long as the choice is used consistently.) HOME NEXT PREVIOUS
14
C o n f i d e n t i a l Developed By Nitendra Trees B Tree [Balance Tree] Unlike a binary-tree, each node of a B-Tree may have a variable number of keys and children. The keys are stored in non-decreasing order. Each key has an associated child that is the root of a sub tree containing all nodes with keys less than or equal to the key but greater than the preceding key. A node also has an additional rightmost child that is the root for a sub tree containing all keys greater than any keys in the node. -The order of B Tree implies maximum number of values can be added into a single node. - If the order is 3 then maximum 3 values can be added and 4 links. HOME NEXT PREVIOUS
15
C o n f i d e n t i a l Developed By Nitendra Trees Example 2: Simple Diagram Example 1: Detail Diagram HOME NEXT PREVIOUS
16
C o n f i d e n t i a l Developed By Nitendra Trees AVL Tree The AVL tree is named after its two inventors, G.M. Adelson-Velsky and E.M. Landis, who published it in their 1962 paper "An algorithm for the organization of information“. It is the special types of Balanced Binary Search Tree. There is the Balanced Factor (BF) that must be -1, 0 or +1 for each node. Balanced Factor(BF) = Left Height – Right Height 2020 10 30 40 5 20 0 +1 0 +1 25 0 HOME NEXT PREVIOUS
17
C o n f i d e n t i a l Developed By Nitendra Trees Binary Tree Traversals: Traversing is the most common operation that can be performed on trees. In the traversal technique each node in the tree is processed or visited exactly once systematically one after the other. The different traversal techniques are there. Each traversal technique can be expressed recursively. There are three types of tree traversals Preorder Inorder Postorder HOME NEXT PREVIOUS
18
C o n f i d e n t i a l Developed By Nitendra Trees Preorder [Root – Left – Right ] The preorder traversal of a binary tree can be recursively defined as follows - Process the root Node [N] - Traverse the Left subtree in preorder[L] - Traverse the Right subtree in preorder [R] A B C D E F G Preorder Sequence : A B D F C E G HOME NEXT PREVIOUS
19
C o n f i d e n t i a l Developed By Nitendra Trees Inorder [Left – Root - Right ] The preorder traversal of a binary tree can be recursively defined as follows - Traverse the Left subtree in preorder[L] - Process the root Node [N] - Traverse the Right subtree in preorder [R] A B C D E F G Inorder Sequence : F D B A E G C HOME NEXT PREVIOUS
20
C o n f i d e n t i a l Developed By Nitendra Trees Postorder [Left - Right – Root] The preorder traversal of a binary tree can be recursively defined as follows - Traverse the Left subtree in preorder[L] - Traverse the Right subtree in preorder [R] - Process the root Node [N] A B C D E F G Postorder Sequence : F D B G E C A HOME NEXT PREVIOUS
21
C o n f i d e n t i a l Developed By Nitendra Trees Application of Trees: Trees are often used in implementing games, particularly “board” games node represents a position in the board branches from a node represent all the possible moves from that position the children represent the new positions Ordered binary trees can be used to represent Expression Evaluation and evaluate arithmetic expressions if a node is a leaf, the element in it specifies the value if it is not a leaf, evaluate the children and combine them according to the operation specified by the element HOME NEXT PREVIOUS
22
C o n f i d e n t i a l Developed By Nitendra Class summary Definition of Tree Terminologies Type of Trees – Complete Binary Tree – Full/Strictly Binary Tree – Binary Search Tree –Threaded Binary Tree – Red Black Tree – B Tree [Balance Tree] – AVL Tree Binary Tree Traversals Application of Trees HOME PREVIOUS
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.