0% found this document useful (0 votes)
22 views26 pages

Data Structures and Algorithms: Trees and Tree Traversals

The document discusses trees and tree traversal algorithms. It defines what a tree is as a collection of nodes connected by edges. Different types of trees are described including binary trees, binary search trees, B-trees, and heaps. Key terms related to binary trees like root, height, children, and leaf nodes are defined. The document then discusses applications of trees, tree node structures, operations on binary search trees like insertion, search, update, and traversal, and provides pseudocode for functions related to these operations.

Uploaded by

Ahsan Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views26 pages

Data Structures and Algorithms: Trees and Tree Traversals

The document discusses trees and tree traversal algorithms. It defines what a tree is as a collection of nodes connected by edges. Different types of trees are described including binary trees, binary search trees, B-trees, and heaps. Key terms related to binary trees like root, height, children, and leaf nodes are defined. The document then discusses applications of trees, tree node structures, operations on binary search trees like insertion, search, update, and traversal, and provides pseudocode for functions related to these operations.

Uploaded by

Ahsan Javed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Data Structures and

Algorithms [CC2411w]
trees and tree traversals
What are trees

 Variation of linked list


 A linked list is a chain of nodes connect through "next" pointers.
 A tree is similar, but each node can be connected to multiple nodes.

A tree is a collection of entities called nodes. Nodes are connected by edges.


Each node contains a value or data, and it may or may not have a child node
Types of trees

 Binary tree
 Binary Search Trees
 2-3 trees
 M-Tree
 B-trees
 Red black tree
 Avial trees
 Heap
 Graphs
Representation of Trees –Binary tree
Representation of Trees –Binary tree
Terms with binary tree

 Root:
 Height:
 Proper Tree:
 Improper Tree:
 Full tree:
 Complete Tree:
 Children:
 Parent:
 Leaf Node:
 Balance:
Exercise
Applications of trees

 Arithmetic Expression tree


Structure of Tree Node (class)

 Struct Node { };
 Class tree { };
Exercise-BST
Operations on a BST

 Insertion
 Search
 DFS

 BFS

 Update
 getMaxNode
 getMinNode
 Traversal
 Inorder

 Preorder

 postorder

 Delete
 Isbalanced
 Balance
 getHeight
constructor
insertion

void insert(Node *treeNode, int data);


isBalance
bool isBalanced(Node *treeNode);
Search

 bool search(int key);


Update

 bool Update(int key, int newValue);


getMaxNode

Node * getMaxNode()
getMinNode

Node * getMinNode()
getHeight

int getHeight(Node *treeNode)


Traversal-Preorder-used in BFS

 NLR
Traversal-Inorder – used in DFS

 LNR
Traversal-Postorder

 LRN
Deletion from BST
Balance

 Void balance()
 Convert normal BST to a tree with min possible height
 Idea:
 Count number of nodes
 Create dynamic array
 Travese tree in in-order and store results in array
 Construct BST from sorted array
 Rearrange root
destructor
BFS-breadth first search

 Aka level first search

You might also like