# lec-15_16 dsa_uaar

Document Sample

```					Trees

Nawazish Naveed
Trees

2
Parts of a Tree

nodes

3
Parts of a Tree

parent
node

4
Parts of a Tree

parent                     child
node                      nodes

5                                      5
Parts of a Tree

parent                     child
node                      nodes

6
Parts of a Tree

leaf
root node
nodes

7
Parts of a Tree

sub-tree

8                                8
Parts of a Tree

sub-tree

9
Parts of a Tree

sub-tree

10
Depth of A Node

nodes                     Depth 0

Depth 1

Depth 2

Depth 3

11
Depth of Tree

   Depth of a Tree
 Depth of a tree is the maximum number
of nodes in a branch.

   Note: depth and height means one and the same
thing
12
Depth of a Tree

    Full Binary Tree
 Every   leaf has the same depth, and every non-leaf
has two children
    Complete binary tree
 All the depths are full, except the deepest, where
the nodes are as far left as possible
 Depth dn of the complete binary tree with n nodes
is given by
 Dn= [log2 n + 1]
 Ex. Dn = ? When n= 10,00,000
13
Full Binary Tree

nodes

14
Complete Binary Tree

nodes

15
Binary Tree
• Each node can have at most 2 children

16
Calculating Number of Nodes

17
Traversal
    Systematic way of visiting all the nodes.

    Methods:
 Preorder,   Inorder, and Postorder

    They all traverse the left subtree before
the right subtree.

    The name of the traversal method
depends on when the node is visited.
18
Pre-Order Traversal

   Visit the node.

   Traverse the left subtree.

   Traverse the right subtree.

19
Example: Pre-Order

43

31                             64

20                  40             56         89

28        33             47        59

43 31 20 28 40 33 64 56 47 59 89
20
In-Order Traversal

   Traverse the left subtree.

   Visit the node.

   Traverse the right subtree.

21
Example: In-Order

43

31                             64

20                  40             56         89

28        33             47        59

20 28 31 33 40 43 47 56 59 64 89
22
Post-Order Traersal

   Traverse the left subtree.

   Traverse the right subtree.

   Visit the node.

23
Example: Post-Order

43

31                             64

20                  40             56         89

28        33             47        59

28 20 33 40 31 47 59 56 89 64 43
24
Trees: Basic terminology[1]
   Hierarchical data structure
   Each position in the tree is called a node
   The “top” of the tree is called the root
   The nodes immediately below a node are called
its children; nodes with no children are called
leaves (or terminal nodes), or terminal nodes,
and the node above a given node is its parent
(or father)
   A node x is ancestor of node y if x is father of y
or father of some ancestor of y. y is called
descendent of x. Ancestor of a node is its
parent, grand parent or grand-grand parent or so
on….
Trees: Basic terminology[2]
 Nodes with the same parent are siblings
 A node plus the collection of nodes
beneath it is called a subtree
 The number of nodes in the longest path
from the root to a leaf is the depth (or
height) of the tree
 isthe depth 2 or 3?
 depends on the author
Root

Node A                          Node B

Node C            Node D          Node E     Node F     Node G

Node H        Node I                        Node J       Node K

Nodes C, H, and I form a subtree                          Node L
Nodes H and I are siblings
C is H’s parent, H and I are children of C
What is the depth of this tree?
Binary Trees
 A commonly used type of tree is a binary
tree
 Each node has at most two children
 The “tree” is a conceptual structure
 The data can be stored either in a
dynamic linked tree structure, or in
contiguous memory cells (array)
according to a set pattern; in other words,
implementation can be pointer-based or
array-based
Binary trees
   A tree is called strictly binary tree if every non
leaf node has exactly two children.
   A strictly binary tree having n leaves always
contain 2n-1 nodes
   Text book definition of depth of a tree
 Depth   of binary tree is maximum level of any leaf in
the tree.
 Root of tree is at level 0, and level of any other node
in the tree is one more than the level of its father
Binary Trees
   A complete binary tree of level d is the strictly
binary tree all of whose leaves are at level d
   A complete binary tree of level d has 2l nodes at
each level l where 0<=l<=d
   Total number of nodes (tn) in a complete binary
tree of depth d will be:
d
tn  2  2  2      2
0     1     2               d
   2
j 0
j
 2 d 1  1

For a binary tree of height h there are O(2h) nodes
Binary Trees
   For a complete binary tree if number of nodes
(tn) are known, then we may find depth of the
binary tree
tn  2 d 1  1
2 d 1  tn  1
d  1  log 2 (tn  1)
 d  log 2 (tn  1) - 1

A binary tree with nodes n has height O(log2n)
Binary Trees Storage
 Array based implementation
structure
   Each node in the tree consists of:
 The  data, or value contained in the
element
 A left child pointer (pointer to first child)
 A right child pointer (pointer to second
child)
structure
   A root pointer points to the root node
 Follow  pointers to find every other element in
the tree
 Add and remove nodes by manipulating
pointers
 Leaf nodes have child pointers set to null
class CBinTree
{
struct Node
{
int value;
Node *LeftChild,*RightChild;
}*Root;
/***Operations*********/
/************************/
};
ROOT
Binary Tree: contiguous storage
   Value in root node stored first, followed by left
child, then right child
   Each successive level in the tree stored left to
right; unused nodes in tree represented by a bit
pattern to indicate nothing stored there
   Children of any given node n is stored in cells 2n
and 2n + 1 (If array index starts at 1)
   Can calculate position for any given node
   Storage allocated as for full tree, even if many
nodes empty
   For a tree of depth h we need array of 2h+1-1
cells
Binary Trees
 Full binary tree of height (depth) h: all
nodes at a height less than h have
exactly two children
 Balanced binary tree: for each node, the
difference in depth of the right and left
subtrees is no more than one
 Completely balanced tree: left and right
subtrees of every node have the same
height
Full Binary Tree

A very sparse,
unbalanced tree
Primitive Operations
   Left(node): Gives index/pointer of left child
   Right(node): Gives index/pointer of right child
   Parent(node): Returns index/pointer of parent
   Brother(node): Returns index/pointer of brother
   Root: Gives index/pointer of root node
   Info(Node): Data/Info stored at node
   IsLeft(node): Is node left child? Yes/No
   IsRight(node): Is node right child? Yes/No
Common Operations
 Tree traversal
 Node deletion
 Destroy
Traversal of Binary Trees
 Pass through all nodes of tree
 Inorder (symmetric traversal)
 Preorder (depth first traversal)
 Postorder
Trees Traversal
   Inorder                            Root
 (Left)   Root (Right)
   Preorder                    Left          Right

 Root     (Left) (Right)
   Postorder
 (Left)   (Right) Root
Inorder Traversal
+
Left Root Right manner
*               +

 Left + Right                 A       B       *           E

 [Left*Right]+[Left+Right]
C               D
 (A*B)+[(Left*Right)+E)
(A*B)+(C*D+E)
 (A*B)+[(C*D)+E]
Preorder Traversal
+
Root Left Right manner
*               +
 + Left Right
 + [*Left Right] [+Left Right]   A       B       *           E

 +(*AB) [+ *Left Right E]
C               D
 +*AB + *C D E
Postorder Traversal
+
Left Right Root manner
*               +

 Left Right +                     A       B       *           E

 [Left Right *] [Left Right+] +
C               D
 (AB*) [Left Right * E + ]+
 (AB*) [C D * E + ]+
 AB* C D * E + +
Binary Search Tree
2, 3, 5 ,4, 9, 15, 7 , 8, 11, 10
   Elements are sorted:
   For any node n in a binary search tree:
 The   value at n is greater than the values of any of the
nodes in its left subtree
 The value at n is less than the values of any of the
nodes in its right subtree
 Its left and right subtrees are binary search trees

   Need to define “greater than” and “less than” for
the specific data
Expression Trees
   Expression tree for:
(a+b*c) +((d*e+f)*g)       •Inorder traversal
•Preorder traversal
•Postorder traversal
Expression Trees

Algorithm to convert postfix expression
into expression tree
e.g. input: ab+cde+**
Expression Trees
Binary Search Tree

   For every node, X, in the tree, the values
of all the keys in its left subtree are
smaller than the key value of X, and the
values of all the keys in its right subtree
are larger than the key value of X.

X

Binary Search Tree Operations

There are many operations one can perform on a binary search tree.

a) Creating a binary search tree
b) Inserting a node into a binary search tree
c) Finding a node in a binary search tree
d) Deleting a node in a binary search tree.

We will use a simple class that implements a binary tree to store
integer values.

Creating a Binary Tree

We create an IntBinaryTree class.
The basic node of our binary tree has the following struct declaration.

struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right;
}

The class IntBinaryTree declaration is -
IntBinaryTree.h
class IntBinaryTree
{
private:
struct TreeNode
{
int value;
TreeNode *left;
TreeNode *right;
};

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 2 posted: 2/25/2012 language: pages: 53