Download presentation
Presentation is loading. Please wait.
1
Data Structures & Algorithm Design
Trees Mais S. Al-Saoud Computer Science Department Karballa University March 2015
2
Tree Definition Tree: a set of elements of the same type such that
it has a distinguished element called the root from which descend zero or more trees (subtrees) Examples in real life: Family tree Table of contents of a book Recursive! 2
3
Tree Terminology Nodes: the elements in the tree
Edges: connections between nodes Root: the distinguished element that is the origin of the tree There is only one root node in a tree Leaf node: a node without an edge to another node Interior node: a node that is not a leaf node Empty tree has no nodes and no edges
4
Tree Terminology Parent or predecessor: the node directly above in the hierarchy A node can have only one parent Child or successor: a node directly below in the hierarchy Ancestors of a node: its parent, the parent of its parent, etc. Descendants of a node: its children, the children of its children, etc.
5
Tree Terminology Subtree of a node: consists of a child node and all its descendants A subtree is itself a tree A node may have many subtrees If A is the root of a binary tree and B is the root of its left or right subtrees, then A is said to be the father of B and B is said to be the left/right son of A. Two nodes are brothers if they are left and right sons of the same father.
6
Tree Terminology Root Subtrees of the root
7
Subtrees E Subtrees of the node labeled E
8
Tree Terminology Root Interior nodes Leaf nodes
9
Height of a Tree A path is a sequence of edges leading from one node to another Length of a path: number of edges on the path Height of a (non-empty) tree : length of the longest path from the root to a leaf (maximum level of any node) What is the height of a tree that has only a root node? By convention, the height of an empty tree is -1 9
10
Level and Degree of a Node
Level of a node : number of edges between root and node Level of root node is 0 Level of a node that is not the root node is level of its parent + 1 Degree of a node: the number of children it has Degree of a tree: the maximum of the degrees of the tree’s nodes
11
Level of a Node Level 0 Level 1 Level 2 Level 3
12
Binary Trees General tree: a tree each of whose nodes may have any number of children n-ary tree: a tree each of whose nodes may have no more than n children Binary tree: a tree each of whose nodes may have no more than 2 children a binary tree is a tree with degree 2
13
Binary Trees Tree with 0–2 children per node
The children (if present) are called the left child and right child a tree where every node has left and right subtrees is a binary tree
14
Binary Trees Strictly Binary Tree Complete Binary Tree
All non-leaf nodes have two branches Complete Binary Tree All its leaf nodes at the same level Almost Complete Binary Tree The last level is partially filled from left to right.
15
Binary Trees A B C D E F G H I J K
Not Complete BT, Strictly BT, Almost complete BT
16
Binary Tree Properties
The number of nodes n in a complete binary tree is at least n = 2h and at most n = 2(h + 1) − 1 where h is the height of the tree. The number of leaf nodes L in a complete binary tree can be found using this formula: L = 2h where h is the height of the tree. The number of nodes n in a perfect binary tree can also be found using this formula: n = 2L − 1 where L is the number of leaf nodes in the tree.
17
Binary Tree Properties-Example
18
Binary Search Tree Binary Search Tree is a binary tree in which nodes are arranged according to their values. The left node has a value less than its parent The right node has a values greater than its parent
19
Binary Search Tree Construction
How to build & maintain binary trees? Insertion Deletion Maintain key property Smaller values in left subtree Larger values in right subtree
20
Declaration BT struct node { A node consists of three fields such as :
Left Child (LChild) Information of the Node (Info) Right Child (RChild) struct node { int info; struct node *left; struct node *right; };
21
Binary Search Tree – Insertion
Perform comparison for value X Comparison will end at node Y (if X not in tree) If X < Y, insert new leaf X as new left subtree for Y If X > Y, insert new leaf X as new right subtree for Y
22
Not a binary search tree
Insertion- Examples 5 10 10 2 45 5 30 5 45 30 2 25 45 2 25 30 10 25 Binary search trees Not a binary search tree
23
Insertion- Example Insert ( 20 ) 10 20 >10, right 20 < 30, left
Insert 20 as left leaf 5 30 2 25 45 20
24
Binary Search Tree Given the following sequence of numbers,
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 The following binary search tree can be constructed.
26
Algorithm for insert node in BST
Steps processing (by Recursion) 1.check if tree not contain any node if Root = NULL create node in tree 2.Else if x< Root Inf Add the new value in the left subtree Rootleft = insert(Rootleft, x); 3.Else Add the new value in the right subtree Rootright = insert(Rootright, x);
27
Insertion of node in BST
node * Insert(node *p, int x) { if(p==NULL) { p=make(x); return p; } else { if(x<p->inf) p->left=Insert(p->left, x); else p->right=Insert(p->right,x); return p; } }
28
Traversing : Means visiting all the nodes of the tree, in some specific order for processing. There are THREE ways of Traversing a Tree 1. Preoder Traversal 2. Inorder Traversal 3. Postorder Traversal 4. Levelorder Traversal
29
Preorder Traversal Method
Traversing a binary tree in preorder (Root – Left – Right) 1. Visit the root. 2. Traverse the left subtree in preorder. 3. Traverse the right subtree in preorder.
30
Ex 1 Ex 2
31
Preorder Traversal Method
void PreOrder(node *&h) { if (h!=NULL) cout<<h->inf; PreOrder(h->left); PreOrder(h->right); } }
32
Inorder Traversal Method
Traversing a binary tree in inorder (Left – Root – Right) 1. Traverse the left subtree in inorder. 2. Visit the root. 3. Traverse the right subtree in inorder
33
Inorder Traversal Method
int InOrder(node *&h) { if (h!=NULL) InOrder(h->left); cout<<h->inf; InOrder(h->right); } }
34
Ex 1 Ex 2
35
Postorder Traversal Method
Traversing a binary tree in postorder (Left – Right – Root) 1. Traverse the left subtree in postorder. 2. Traverse the right subtree in postorder. 3. Visit the root postorder
36
Ex 1 Ex 2
37
Postorder Traversal Method
int PostOrder(node *&h) { if (h!=NULL) PostOrder(h->left); PostOrder(h->right); cout<<h->inf; } }
38
Levelorder Traversal Method
levelorder Example (Visit = print) Travers tree level by level
39
Traversals: Example
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.