Download presentation
Presentation is loading. Please wait.
Published byOswald Bruce Modified over 9 years ago
1
Rudiments of Trees a Joshua presentation DATA STRUCTURES
2
What is a tree? Tree is a non-linear data structure used to represent data containing a hierarchical relationship between themselves. Eg. Family trees A BC DEFGH I A tree is a special type of graph that contains no cycles. Definition Binary Tree
3
What is a binary tree? A binary tree is a tree in which every node has not more than 2 children. Terminology A BC DEFGH I nodes root node leaves A BC DEFG I parent children
4
Binary Search Tree What is a binary search tree? A binary search tree is a binary tree in which each node N is greater than every node in the left sub-tree and less than every node in the right sub-tree. 25 1550 10203055 17N > N < N
5
Creating a Binary Search Tree Consider the following list : 38 14 56 45 8 82 23 18 70 20 How do we create a binary search tree out of this list? 38 Place 38 as the root 14 14 < 38 56 56 > 38 45 45 > 38 and < 56 23 18 20 82 70 8 8 < 38, < 14 82 > 38, > 56 23 14 18 14, < 23 70 > 38, > 56, < 82 20 14, 18
6
Creating a Binary Search Tree - Creating a Binary Search Tree - Algorithm Left INFO Right X INFO X Root_node ROOT Struct Node Struct Node Root, Current, Parent; { int INFO; Struct Node *Left, *Right; }; A binary tree can be implemented using a linked list
7
Creating a Binary Search Tree - Creating a Binary Search Tree - Algorithm Root 38 5614 45 Current Parent Root = malloc(); While (there is a new number to be added) { Current = Root; Do { Parent = Current; If ( Next_no > Current INFO ) {Current = Current Right; Flag = 0;} else {Current = Current Left; Flag = 1;} } while (Current != NULL); if (Flag = = 1) Ptr = Parent Left = malloc(); else Ptr = Parent Right = malloc(); Ptr INFO = Next_no; Ptr Left = Ptr Right = NULL; } Root INFO = 1 st no; Root Left=Root Right= NULL; Let the list be 38, 56, 14, 45
8
Thank you
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.