0% found this document useful (0 votes)
56 views39 pages

C S 1 1 3 2 Design: - Data Structures and Software

This document provides an overview of graphs and binary trees. It defines graphs as consisting of nodes and edges between nodes. Examples of graphs and trees are given. Binary search trees can be constructed from sorted arrays. Different traversal techniques for binary trees like preorder, inorder and postorder are explained along with code examples and illustrations. Applications of trees like sorting a binary search tree using inorder traversal and representing trees using arrays are also covered.

Uploaded by

kerhun123
Copyright
© Attribution Non-Commercial (BY-NC)
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)
56 views39 pages

C S 1 1 3 2 Design: - Data Structures and Software

This document provides an overview of graphs and binary trees. It defines graphs as consisting of nodes and edges between nodes. Examples of graphs and trees are given. Binary search trees can be constructed from sorted arrays. Different traversal techniques for binary trees like preorder, inorder and postorder are explained along with code examples and illustrations. Applications of trees like sorting a binary search tree using inorder traversal and representing trees using arrays are also covered.

Uploaded by

kerhun123
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 39

Lecture 6

CS 1132 DATA STRUCTURES AND SOFTWARE DESIGN

Introduction to Graphs
A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of
a finite set V called the set of nodes, and a set E that is a subset of VxV. That is, E is a set of pairs of the form (x,y) where x and y are nodes in V

Examples of Graphs
V={1,2,3,4,5} E={(1,2), (2,3), (1,4), (4,1), (3,3), (5,4)}

2 3 5 4

When (x,y) is an edge, we say that x is adjacent to y. 1 is adjacent to 2. 2 is not adjacent to 1. 4 is not adjacent to 3.
3

Trees
A tree is a connected acyclic undirected graph. The following are three trees:

2 1

7
5 12 3 4 6 11

10

Make binary tree by sorted Array


Array A[15] = { 1, 4,8,11,14,19,23,29,40,89,91,105,110,124,200}
29

A[15] : 105 The First is A[0] 11 And last is A[14] And Now we have two array one of the is bigger than 29 that is in the right side So (0+14)/2And is the other4 one has 7 19 int that all of the are smaller than 29 89 124 A[7] which is equal To 29
1 8

14

23

40

91

110

200

Illustration of Insert
15 8 2 6 3 7 11 10 12 14 20 27 22 30 3 2 8 11 15 20 27 22 14 30

6
7

10 12

25

Before inserting 25

After inserting 25

10 min for Array To Binary Search Tree function

De-allocating binary trees


Three things to do
Free current node Recursively free left subtree Recursively free right subtree

C Binary Tree node


struct btnode
{ int data; struct btnode *left_p; struct btnode *right_p; };

Creating a tree
struct btnode *root;
struct btnode *mynode = (struct btnode *) malloc (sizeof(struct btnode)); root = mynode;

// use root to access the tree, like head for a linked list

Creating nodes
struct node * NewNode(int data)
{ struct node *mynode = (struct node *) malloc (sizeof(struct node)); mynode->data = data; mynode->left_p = NULL; mynode->right_p = NULL; return(node); }

10 min for t_insert and Search function

Insert function

Binary Tree Traversal


Traversal is the process of visiting every node once Visiting a node entails doing some processing at that node, but when describing a traversal strategy, we need not concern ourselves with what that processing is

Binary Tree Traversal Techniques


Three recursive techniques for binary tree traversal
In each technique, the left subtree is traversed recursively,

the right subtree is traversed recursively,


and the root is visited What distinguishes the techniques from one another is the order of those 3 tasks

Preoder, Inorder, Postorder


In Preorder, the root is visited before (pre) the subtrees traversals In Inorder, the root is visited in-between left and right subtree traversal In Preorder, the root is visited after (pre) the subtrees traversals
Preorder Traversal: 1. Visit the root 2. Traverse left subtree 3. Traverse right subtree

Inorder Traversal: 1. Traverse left subtree 2. Visit the root 3. Traverse right subtree Postorder Traversal: 1. Traverse left subtree 2. Traverse right subtree 3. Visit the root

Illustrations for Traversals


Assume: visiting a node is printing its label Preorder:
4 5 6 11 3 8 1 7 9

Inorder:

10

12

Postorder:

Illustrations for Traversals


Assume: visiting a node is printing its label Preorder: 1 3 5 4 6 7 8 9 10 11 12 Inorder:
4
5 8 9 10 1

6
11

4 5 6 3 1 8 7 9 11 10 12
Postorder: 4 6 5 3 8 11 12 10 9 7 1
12

Illustrations for Traversals (Contd.)


Assume: visiting a node is printing its data Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30 Postorder: 3 7 6 2 10 14
3 6 10 12 7 14 22 8 15 20 11

27 30

12 11 8 22 30 27 20 15

Code for the Traversal Techniques


The code for visit is up to you to provide, depending on the application A typical example for visit() is to void preOrder(Tree *tree){ if (tree->isEmpty( )) return; visit(tree->getRoot( )); preOrder(tree->getLeftSubtree()); preOrder(tree->getRightSubtree()); } void inOrder(Tree *tree){ if (tree->isEmpty( )) return; inOrder(tree->getLeftSubtree( )); visit(tree->getRoot( )); inOrder(tree->getRightSubtree( )); } void postOrder(Tree *tree){ if (tree->isEmpty( )) return; postOrder(tree->getLeftSubtree( )); postOrder(tree->getRightSubtree( )); visit(tree->getRoot( )); }

print out the data


part of its input node

Application of Traversal Sorting a BST


Observe the output of the inorder traversal of the BST example two slides earlier
It is sorted

This is no coincidence
As a general rule, if you output the keys (data) of the nodes of a BST using inorder traversal, the data comes out sorted in increasing order

Depth/Height of Full Trees and Almost Complete Trees


The height (or depth ) h of such trees is O(log n)
Proof: In the case of full trees,
The number of nodes n is: n=1+2+22+23++2h=2h+1-1 Therefore, 2h+1 = n+1, and thus, h=log(n+1)-1 Hence, h=O(log n)

For almost complete trees, the proof is left as an exercise.

Canonical Labeling of Almost Complete Binary Trees


Same labeling inherited from full binary trees

Same relationship holding between the labels of children and parents:

Relationships between labels of children and parent: i 2i 2i+1

Trees and arrays


Map current node, left child, and right child to array positions t[i] , t[2i+1] , t[2i+2]

Order in the array by level in the tree

Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compact-binary-heaps/

Trees and arrays


Map current node, left child, and right child to array positions

Order in the array by level in the tree

Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compact-binary-heaps/

Application of Almost Complete Binary Trees: Heaps


A heap (or min-heap to be precise) is an almost complete binary tree where
Every node holds a data value (or key) The key of every node is the keys of the children

Note: A max-heap has the same definition except that the Key of every node is >= the keys of the children

Search function

Delete a Complete Tree


To delete a tree we must traverse all the nodes of the tree and delete them one by one. So which traversal we should use Inorder or Preorder or Postorder. Answer is simple Postorder, because before deleting the parent node we should delete its children nodes first

10 min for t_insert function

You might also like