Rudiments of Trees a Joshua presentation DATA STRUCTURES.

8
Rudiments of Trees Rudiments of Trees a Joshua presentation DATA STRUCTURES DATA STRUCTURES

Transcript of Rudiments of Trees a Joshua presentation DATA STRUCTURES.

Page 1: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

Rudiments of TreesRudiments of Trees

a Joshua presentation

DATA DATA STRUCTURESSTRUCTURES

Page 2: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

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

B C

D E F G H

I

A tree is a special type of graph that contains no cycles.

DefinitionDefinition

Binary Tree

Page 3: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

What is a binary tree?

A binary tree is a tree in which every node has not more than 2 children.

TerminologTerminologyy A

B C

D E F G H

I

nodes

root node

leaves

A

B C

D E F G

I

parent

children

Page 4: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

Binary Search TreeBinary 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

15 50

10 20 30 55

17

NN

> N> N< N

Page 5: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

Creating a Binary Search Creating a Binary Search TreeTreeConsider 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

5656 > 38

45

45 > 38 and < 56

23

18

20

82

70

8

8 < 38, < 14

82 > 38, > 56

23 < 38, > 14

18 < 38, > 14, < 2370 > 38, > 56, < 82

20 < 38, > 14, < 23, > 18

Page 6: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

Creating a Binary Search Tree Creating a Binary Search Tree -- Algorithm

Left INFO Right

X INFO X 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

Page 7: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

Creating a Binary Search Creating a Binary Search Tree -Tree - Algorithm

Root

38

5614

45

Current

ParentRoot = malloc();

While (there is a new number to be added)

{Current = Root;Do{

Parent = Current;If ( Next_no > CurrentINFO )

{Current = CurrentRight; Flag = 0;} else

{Current = CurrentLeft; Flag = 1;}} while (Current != NULL);if (Flag = = 1) Ptr = ParentLeft = malloc();else Ptr = ParentRight = malloc();

PtrINFO = Next_no; PtrLeft = PtrRight = NULL;

}

RootINFO = 1st no;

RootLeft=RootRight= NULL;

Let the list be 38, 56, 14, 45

Page 8: Rudiments of Trees a Joshua presentation DATA STRUCTURES.

Thank you