0% found this document useful (0 votes)
38 views29 pages

DSA Lec 6

This lecture covers the concept of trees as hierarchical data structures, including basic terminologies such as nodes, roots, and leaves. It discusses binary trees, their properties, storage techniques, and traversal methods like inorder, preorder, and postorder. Additionally, it introduces the tree abstract data type (ADT) and its associated operations.

Uploaded by

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

DSA Lec 6

This lecture covers the concept of trees as hierarchical data structures, including basic terminologies such as nodes, roots, and leaves. It discusses binary trees, their properties, storage techniques, and traversal methods like inorder, preorder, and postorder. Additionally, it introduces the tree abstract data type (ADT) and its associated operations.

Uploaded by

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

Lecture

Trees
Course Instructor
Engr. Anum Raza
Lecture Outline
 Trees
 Basic Terminologies
 Tree Properties
 Binary Tree
Trees
 Hierarchical data structure

 Examples:
 Indexes in a book have a shallow tree structure
 A family tree

 Others?
Unix / Windows file structure
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), 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 and 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
 is the depth 2 or 3?
 depends on the author

 Text book definition of depth of a tree


 Depth of a 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
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?
Tree Properties
A
Property Value
Number of nodes
B C Height
Root Node
Leaves
Number of levels
D E F
Ancestors of H
Descendants of B
Siblings of E
Right subtree
G

H I
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
Binary Trees
 Full binary tree of height (depth) h: all nodes at a
height less than h have exactly two children
 in which each node has exactly zero or 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
Balanced Binary Tree
a a
b c b
c e
d e f g
d f
h i j
g h
A balanced binary tree
i j
An unbalanced binary tree

 A binary tree is balanced if every level above the lowest is


“full” (contains 2n nodes)

12
Tree ADT
 Objects: any type of objects can be stored in a tree
 accessor methods
 root() – return the root of the tree
 parent(p) – return the parent of a node
 children(p) – returns the children of a node
 query methods
 size() – returns the number of nodes in the tree
 isEmpty() - returns true if the tree is empty
 elements() – returns all elements
 isRoot(p), isInternal(p), isExternal(p)
 other methods
 Tree traversal, Node addition/deletion, create/destroy
Lecture 6b
Lecture Outline
 Binary Tree
 Storage Techniques
 Traversal Techniques
 Inorder with algorithm
Binary Trees Storage
 Linked List based implementation
 Array based implementation
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
A
[1] A
[2] B
[3] --
B
[4] C
[5] --
C [6] --
[7] --
D [8] D
[9] --
. .
E
[16] E
A

B C
[1] A
[2] B
D E F G
[3] C
[4] D
[5] E
H I [6] F
[7] G
[8] H
[9] I
Binary Tree: as a linked 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)
Binary Tree: as a linked 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*********/
/************************/
};
a

b c

d e f

g h i j k

l
24
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 * +
 [Left*Right]+[Left+Right]
 (A*B)+[(Left*Right)+E) A B * E

 (A*B)+[(C*D)+E]
C D

(A*B)+(C*D+E)
Algorithm for Inorder Traversal
 [push NULL onto stack and initialize PTR]
 1) Set Top=1, Stack[1]=NULL and Ptr=Root
 2) Repeat while (Ptr ≠ NULL)
 Top=Top+1, Stack[Top]=Ptr (Pushes left most path onto Stack)
 Ptr=Left[Ptr]

 3) Ptr=Stack[Top] and Top=Top-1


A
 4) Repeat Step 5 to 7 while Ptr ≠ NULL
 5) Apply Process to Data[Ptr] B C
 6) [Right child?] If Right[Ptr] ≠ NULL
 a) Set Ptr= Right[Ptr] and Display
 b) Go to Step 3 D E
 7) Set Ptr=Stack[Top] and Top=Top-1
 8) Exit

You might also like