4 views

Uploaded by Anjaney Sharma

This lecture is to understand the topic of Trees in Computer Science. It teaches basics of Trees and its applications in computer science.

- Csa Brochure
- 06-Trees+BST
- brf plus.pdf
- ACM Problem Set Volume 1
- Avl Tree
- A QoS-Aware Multicast Routing Protocol
- 4th sem
- Property Tree
- tree_traversal
- Libro7
- Red Black Tree
- Data Structures and Algorithms
- Huffman Coding
- ocaml-3.08-refman
- B+ Trees
- Master Lab (3)
- Unit2-DS.pdf
- OOP LAB.pdf
- Efficient Allocation of CQI Channels in Broadband.pdf
- Presentacion Capitulo 8 (Abstraciones de Datos)

You are on page 1of 31

Anirudh Agnihotry

INTRODUCTION

Arrays , stacks , linked lists , queues are linear data structures whereas trees

are hierarchical data structures.

Trees are of various types e.g. binary trees ,Avl trees , Binary search trees.

A tree whose elements have at most two children is called a binary tree . As it

can have only two children we call them as left and right child

Structure of Node

A node in a tree consists of following parts

Data

Pointer to right child

Pointer to left child

Parent pointer (optional)

The nodes which does not have any child nodes are called as leaves.

Rest of the nodes are called as internal nodes.

Continued ...

5

root

Internal

node

leaf

struct node

{

int data;

struct node *left;

struct node *right;

};

int main()

{

node* root = NULL;

return 0;

}

//create node and allocate memory

node* create_node(int data)

{

node* new_node = (node*) malloc(sizeof(node));

new_node->val = val;

new_node->left = NULL;

new_node->right = NULL;

return new_node;

}

Building a Tree

int main()

{

node* root=create_node(5);

root->left=create_node(2);

//creating right child

root->right=create_node(7);

return 0;

}

To traverse (or walk) the binary tree is to visit each node in the binary tree

exactly once

There are three kinds of traversals

Preorder :- node,left,right

Inorder :- left,node,right

Postorder:- left,right,root

Inorder walk

Inorder walk(root)

1.

2.

3.

Inorder walk(root->left)

Visit node

Inorder walk(root->right)

Time Complexity ?

{

if(root == NULL)//base case

return;

Else{

Inorderwalk(root->left);

printf(%d,root->data);

Inorderwalk(root->right);

}

}

Preorder traversal ???

Postorder traversal ???

{

if(root == NULL)//base case

return false;

Else if(root->data == key)

return true;

Else{

return IsPresent(root->left,key) ||

IsPresent(root->right,key);

}

}

Time complexity ?

Height of a Tree

Height of a node is the length of the longest path in its subtree to a leaf

Height of a Binary Tree is the height of its root node.

Given a Binary Tree, write a function which calculates its height.

int height(Node* root)

{

if(root == NULL)//base case

Return -1;

Else{

Return max(height(root->left),height(root->right))+1;

}

}

Time complexity ?

Height of a leaf ?

Maximum height possible with N nodes ?

Questions ?

The left subtree of a nodes contains nodes with keys with value less than its own key

The right subtree of a nodes contains nodes with keys with value higher than its own key

The left subtree and right subtree should be binary search trees

The above ordering among keys make search operation faster but slows

down insertion.

Examples

2

10

11

6

2

15

{

if(root == NULL)//base case

return NULL;

Else if(root->data == key)

return root;

Else if(root->data > key){

Return search(root->left,key);

}

Else{

Return search(root->right,key);

}

}

Time complexity ?

Space Complexity ?

Complexity

Time : O(h) where h is the height of the tree.

In the worst case, h may be O(n)!

Space : Need O(h) for stack space for the recursive algorithm. It can be done

iteratively in constant space.

Insert In a BST

Algorithm(key)

starting at the root it probes down the tree till it finds a node whose left or

right pointer is empty and is a logical place for the new value.

Time Complexity is O(h)

10

Insert 12

12 > 10

13

13>12

12

15

Node* insert(Node* root,int key)

{

if(root == NULL) //base case

return create_node(key);

Else if(root->data > key){

root->left = insert(root->left,key);

Return root;

}

Else{

root->right = insert(root->right,key);

Return root;

}

}

10

Int main()

{

node* root = NULL;

root = Insert(root,10);

Insert(root,20);

Insert(root,30);

}

20

30

Deletion in BST

delete(root,key)

1.

2.

Search the key in the tree

a.

b.

3.

If found then proceed to step 3

a.

b.

c.

d.

Case 1 :- The node has no children then replace the link in parent node with NULL

Case 2:- the node has both left and right child

i. Find the leftmost element in right subtree and leftmost element in right subtree,swap

with the node.

ii. Now delete the leftmost element

Case 3:- The node has no left child

i. Link the parent of the node to right subtree

Similarly when the node has no right child

Delete 12

7

parent

10

12

Removing 12

replace the link in the parent with

null

Delete 10

7

10

Removing 10

replace the link in the parent with its

non-null child

Delete 4

7

Removing 4

Swap 4 with the leftmost node and

remove not that node.

Final tree

Properties of BST

Insertion, deletion and search takes O(h) time.

The minimum possible height for n nodes in bst is O(logn)

The maximum possible height for n nodes in bst is O(n)

Solution :- Find the leftmost element of bst for smallest element and rightmost

element for largest element

Node* smallest(Node* root)

{

if(root == NULL) //base case

return NULL;

Else if(root->left == NULL){

return root;

}

Else if{

return smallest(root->left);

}

}

The balanced binary trees are those trees in which the height difference of left

child node and right child node is atmost 1.

Height of such trees become atmost O(logn).

So all the insert,search and delete operations can be done in O(logn).

Thanks :)

- Csa BrochureUploaded byRohitGupta
- 06-Trees+BSTUploaded byIris Zhong
- brf plus.pdfUploaded byeswarscribd
- ACM Problem Set Volume 1Uploaded by蘇意喬
- Avl TreeUploaded byadityatheaaa
- A QoS-Aware Multicast Routing ProtocolUploaded byAyuni Senseii Ny Janius
- 4th semUploaded bySurendar Subramaniam
- Property TreeUploaded byManasa Gopal
- tree_traversalUploaded byGaurav Tyagi
- Libro7Uploaded byMike Gálvez
- Red Black TreeUploaded byBelLa Quiennt Beyca
- Data Structures and AlgorithmsUploaded byMohammed Jeelan
- Huffman CodingUploaded bySyed Umair Anwer
- ocaml-3.08-refmanUploaded byChiritoiu Tiberiu
- B+ TreesUploaded bymanishbhardwaj8131
- Master Lab (3)Uploaded byKala Gnanamuthu
- Unit2-DS.pdfUploaded byKarthikeyan Ramajayam
- OOP LAB.pdfUploaded byhappyskd1993
- Efficient Allocation of CQI Channels in Broadband.pdfUploaded byPutra
- Presentacion Capitulo 8 (Abstraciones de Datos)Uploaded bychapuzas22
- vulunerability classificationUploaded byanon-500134
- Questions on cUploaded byVishwas Jadhav
- Lecture 1Uploaded byalvaro_65
- Algorithm DesignUploaded byMuhammad Ihsan
- mulvariate homeworkUploaded byDurbadal Ghosh
- CAD2016 Interactive InspectionUploaded bysnagareddy
- may 2010 c and dsUploaded bySanethel Maan
- MELJUN CORTES Instructional Manual Data StructuresUploaded byMELJUN CORTES, MBA,MPA
- Chap8 SolutionsUploaded byForm 4B
- B-Trees slidesUploaded byTuhin Utsab Paul

- TREES AND OPTIMIZATIONUploaded byRoozbeh Bassirian
- UNIT 2&3 finalUploaded bykashokcse16
- HeapsUploaded byManoj Kumar G
- Mergeable HeapsUploaded bySitmCompSansthan
- A Lot of Solved MCQs CS301 for Final 2010 by RisingpakistanUploaded byrisingpakistan
- C Program Day-19Uploaded byeshamu
- Delet B TreesUploaded byTalalsaud
- Binary Tree (Part 1)-Chapter 6Uploaded byYudin Gituloh
- Leftist and SkewUploaded byRam Kumar
- Chapter 10Uploaded byBobDover
- Binary search treeUploaded byarulrajme
- TreeUploaded bysumit
- Heap SortUploaded byapi-3825915
- ID3 AlgorithmUploaded byMihir Shah
- Binary TreeUploaded bySameer Kodilkar
- 460-HeapDeapUploaded byDaniel Felipe
- Efficient Binary TreesUploaded byVishal Kumar
- Assignment DiscreatemathsUploaded byParameshwar Reddy Kottapalli
- AVL TREEUploaded byAnand Yadav
- cs301 (1)Uploaded byRizwanIlyas
- Red Black TreesUploaded bymahesh
- 15925_K3R15Uploaded byAlok Bishnoi
- Assignment IC Binary TreesUploaded byAditya Pawar
- AVL Trees - Horowitz Sahni CPP - Lec43Uploaded byhailos
- quad treeUploaded byThaddeus Moore
- Cs301 Fall 2009 Final TermUploaded byvuallstudymaterial
- Short Questions on DSUploaded byKattupalli Sudhakar
- Cs521 Midterm CheatsheetUploaded byFrankXiao
- Left Child-Right Sibling RepresentationUploaded bysasipaper
- Thm07 - Augmenting Ds p1Uploaded bySirc Ann Ares