0% found this document useful (0 votes)
89 views24 pages

Trees: - Introduction - Representation of Trees - Binary Trees - Binary Tree Traversals - Binary Search Trees

Trees can be represented using nodes connected by branches. Binary trees restrict nodes to having at most two child nodes. Traversal of binary trees includes inorder (left-right-root), preorder (root-left-right), and postorder (left-right-root) traversals. Binary search trees organize nodes so that all left descendants of a node are less than the node and all right descendants are greater than the node, allowing efficient search, insertion, and deletion operations in O(h) time where h is the tree height.
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)
89 views24 pages

Trees: - Introduction - Representation of Trees - Binary Trees - Binary Tree Traversals - Binary Search Trees

Trees can be represented using nodes connected by branches. Binary trees restrict nodes to having at most two child nodes. Traversal of binary trees includes inorder (left-right-root), preorder (root-left-right), and postorder (left-right-root) traversals. Binary search trees organize nodes so that all left descendants of a node are less than the node and all right descendants are greater than the node, allowing efficient search, insertion, and deletion operations in O(h) time where h is the tree height.
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/ 24

TREES

• Introduction
• Representation Of Trees
• Binary Trees
• Binary Tree Traversals
• Binary Search Trees
INTRODUCTION

• A tree structure means that the data are organized so that


items of information are related by branches
• Examples:
• Definition (recursively): A tree is a finite set of one or
more nodes such that
• There is a specially designated node called root.
• The remaining nodes are partitioned into n>=0 disjoint set T1,
…,Tn, where each of these sets is a tree. T1,…,Tn are called the
subtrees of the root.
• Every node in the tree is the root of some subtree
• Some Terminology
• node: the item of information plus the branches to each node.
• degree: the number of subtrees of a node
• degree of a tree: the maximum of the degree of the nodes in the
tree.
• terminal nodes (or leaf): nodes that have degree zero
• nonterminal nodes: nodes that don’t belong to terminal nodes.
• children: the roots of the subtrees of a node X are the children
of X
• parent: X is the parent of its children.
• Some Terminology (cont’d)
• siblings: children of the same parent are said to be siblings.
• Ancestors of a node: all the nodes along the path from the
root to that node.
• The level of a node: defined by letting the root be at level
one. If a node is at level l, then it children are at level l+1.
• Height (or depth): the maximum level of any node in the
tree
• Representation Of
Trees (cont’d)
• Left Child-
Right Sibling
Representation
BINARY TREES
• Binary trees are characterized by the fact that any node can
have at most two branches
• Definition (recursive):
• A binary tree is a finite set of nodes that is either empty or consists
of a root and two disjoint binary trees called the left subtree and the
right subtree

• Thus the left subtree and the right subtree are distinguished
A A

B B

• Any tree can be transformed into binary tree


• by left child-right sibling representation
• Two special kinds of binary trees:
(a) skewed tree, (b) complete binary tree
• The all leaf nodes of these trees are on two adjacent levels
• A full binary tree of depth k is a binary tree of death k having 2k-1 nodes,
k  0.
• A binary tree with n nodes and depth k is complete iff its nodes
correspond to the nodes numbered from 1 to n in the full binary tree of
depth k.
• From Lemma 5.1, the
height of a complete
binary tree with n nodes
is log2(n+1)
• Binary tree representations (using array)
• Waste spaces: in the worst case, a skewed tree of depth k requires
2k-1 spaces. Of these, only k spaces will be occupied
• Insertion or deletion
of nodes from the
middle of a tree
requires the
movement of
potentially many nodes
to reflect the change in
the level of these nodes
• Binary tree representations
• Binary tree representations (using link)
BINARY TREE TRAVERSALS
• How to traverse a tree or visit each node in the tree exactly
once?
• There are six possible combinations of traversal

LVR, LRV, VLR, VRL, RVL, RLV


• Adopt convention that we traverse left before
right, only 3 traversals remain

LVR (inorder), LRV (postorder), VLR (preorder)

left_child data right_child


V
L: moving left : R: moving right
visiting
node
• Arithmetic Expression using binary tree
• inorder traversal (infix expression)
A/B*C*D+E
• preorder traversal (prefix expression)
+**/ABCDE
• postorder traversal
(postfix expression)
AB/C*D*E+
• level order traversal
+*E*D/CAB
• Inorder traversal (LVR) (recursive version)

output: A / B* C * D + E

ptr
L
V
R
• Preorder traversal (VLR) (recursive version)

output: + * * / A B C D E

V
L
R
• Postorder traversal (LRV) (recursive version)

output: A B / C * D * E +

L
R
V
• Iterative inorder traversal
• we use a stack to simulate recursion
5 84 11
3 14
2 17
1
A B
/ *C D
* E
+

L
V

output: A / B*C *D + E node


BINARY SEARCH TREES

• Definition of binary search tree:


• Every element has a unique key
• The keys in a nonempty left subtree (right subtree) are
smaller (larger) than the key in the root of subtree
• The left and right subtrees are also binary search trees
• Example: (b) and (c) are binary search trees

medium

smaller larger
• Searching a
binary search
tree

O(h)
• Inserting into a binary search tree

An empty tree
• Deletion from a binary search tree
• Three cases should be considered
• case 1. leaf  delete
• case 2.
one child  delete and change the pointer to this child
• case 3. two child  either the smallest element in the right subtree or the
largest element in the left subtree
• Height of a binary search tree
• The height of a binary search tree with n elements can become as large
as n.
• It can be shown that when insertions and deletions are made at
random, the height of the binary search tree is O(log2n) on the average.
• Search trees with a worst-case height of O(log2n) are called balance
search trees

You might also like