The document discusses trees and their representations in graphs. It defines trees as acyclic, connected graphs with one designated root node. Trees can be represented recursively or using adjacency lists and matrices. Binary trees are discussed, with examples of full and complete binary trees. Common tree traversal algorithms are presented: preorder, inorder and postorder. Applications of trees include decision trees, file systems and representing algebraic expressions. Infix, prefix and postfix notations are explained using binary expression trees.
Trees and Graphs in data structures and AlgorithmsBHARATH KUMAR
The document discusses tree data structures and binary search trees. It defines key concepts related to trees including root, parent, child, leaf nodes, internal nodes, height, depth, and traversal methods. It then describes properties of binary trees including full, complete, and perfect binary trees. Binary search trees are defined as having left and right subtrees containing only nodes with keys less than or greater than the parent node's key. Basic binary search tree operations like search, insert, and traversal methods are also outlined. Sample C code for traversing a binary tree using preorder, inorder, and postorder methods is provided.
Improvement of Random Forest Classifier through Localization of Persian Handw...IDES Editor
The random forest (RF) classifier is an ensemble
classifier derived from decision tree idea. However the parallel
operations of several classifiers along with use of randomness
in sample and feature selection has made the random forest a
very strong classifier with accuracy rates comparable to most
of currently used classifiers. Although, the use of random
forest on handwritten digits has been considered before, in
this paper RF is applied in recognizing Persian handwritten
characters. Trying to improve the recognition rate, we suggest
converting the structure of decision trees from a binary tree
to a multi branch tree. The improvement gained this way
proves the applicability of the idea.
Tree is a non-linear data structure that can represent hierarchical relationships. It consists of nodes connected by edges. The root node has child nodes, and nodes may be leaf nodes or have child nodes themselves. Binary trees restrict nodes to having at most two children. General trees can be converted to binary trees by making the first child the left child and next siblings the right child. Binary trees can be built from input data by comparing values to determine left or right placement, or from traversal operations.
1. The document discusses fundamental concepts about graphs, including definitions of graphs and graph isomorphism.
2. Key points include that a graph is bipartite if its vertices can be partitioned into two sets such that edges only connect vertices in different sets, and that two graphs are isomorphic if there exists a bijection between their vertices that preserves adjacency.
3. An example is given showing that the Petersen graph, which contains odd cycles, is not isomorphic to other graphs that are bipartite. Bipartite graphs cannot contain odd cycles.
The Mississippi Center for Justice is seeking law student interns for the summer of 2014 to advance racial and economic justice through campaigns addressing predatory lending, foreclosure prevention, affordable housing, healthcare access, education equality, and other policies. Although stipends are not provided, the Center will assist interns with obtaining course credit or grant funding and offers a unique legal experience outside of the courtroom working to improve lives across Mississippi. Students should submit a resume, cover letter outlining their interests and experience, and references by February 1st for placement consideration.
1. The document discusses trees as a non-linear data structure that can represent hierarchical relationships. It specifically focuses on binary trees, their basic structure, properties, representations, and converting general trees to binary trees.
2. Binary trees have properties like a root node, leaf nodes, parent and child nodes, as well as left and right subtrees. They can be represented sequentially using arrays or linked lists.
3. General trees can be converted to binary trees by deleting branches except the leftmost one, and drawing new branches from each node to its right neighbor at the same level.
Trees are hierarchical data structures that can represent relationships between data items. They are useful for representing organizational charts, file systems, and programming environments. Key tree concepts include the root node, internal and leaf nodes, ancestors and descendants, subtrees, depth, height, and degree. Common tree operations include traversing the tree using preorder, inorder, and postorder traversal methods, evaluating expression trees, and using trees for data compression through Huffman coding. Huffman coding assigns variable-length binary codes to characters based on their frequency, allowing more common characters to have shorter codes to reduce the overall file size.
The document discusses various tree data structures and operations including binary trees, binary search trees, and threaded binary trees. It begins with introductions to tree terminology and representations. It then covers properties, traversals, and additional operations for binary trees such as copying and testing equality. Binary search trees and their search operation are discussed next. The document concludes with descriptions of threaded binary trees and how they reduce null pointers.
Trees and Graphs in data structures and AlgorithmsBHARATH KUMAR
The document discusses tree data structures and binary search trees. It defines key concepts related to trees including root, parent, child, leaf nodes, internal nodes, height, depth, and traversal methods. It then describes properties of binary trees including full, complete, and perfect binary trees. Binary search trees are defined as having left and right subtrees containing only nodes with keys less than or greater than the parent node's key. Basic binary search tree operations like search, insert, and traversal methods are also outlined. Sample C code for traversing a binary tree using preorder, inorder, and postorder methods is provided.
Improvement of Random Forest Classifier through Localization of Persian Handw...IDES Editor
The random forest (RF) classifier is an ensemble
classifier derived from decision tree idea. However the parallel
operations of several classifiers along with use of randomness
in sample and feature selection has made the random forest a
very strong classifier with accuracy rates comparable to most
of currently used classifiers. Although, the use of random
forest on handwritten digits has been considered before, in
this paper RF is applied in recognizing Persian handwritten
characters. Trying to improve the recognition rate, we suggest
converting the structure of decision trees from a binary tree
to a multi branch tree. The improvement gained this way
proves the applicability of the idea.
Tree is a non-linear data structure that can represent hierarchical relationships. It consists of nodes connected by edges. The root node has child nodes, and nodes may be leaf nodes or have child nodes themselves. Binary trees restrict nodes to having at most two children. General trees can be converted to binary trees by making the first child the left child and next siblings the right child. Binary trees can be built from input data by comparing values to determine left or right placement, or from traversal operations.
1. The document discusses fundamental concepts about graphs, including definitions of graphs and graph isomorphism.
2. Key points include that a graph is bipartite if its vertices can be partitioned into two sets such that edges only connect vertices in different sets, and that two graphs are isomorphic if there exists a bijection between their vertices that preserves adjacency.
3. An example is given showing that the Petersen graph, which contains odd cycles, is not isomorphic to other graphs that are bipartite. Bipartite graphs cannot contain odd cycles.
The Mississippi Center for Justice is seeking law student interns for the summer of 2014 to advance racial and economic justice through campaigns addressing predatory lending, foreclosure prevention, affordable housing, healthcare access, education equality, and other policies. Although stipends are not provided, the Center will assist interns with obtaining course credit or grant funding and offers a unique legal experience outside of the courtroom working to improve lives across Mississippi. Students should submit a resume, cover letter outlining their interests and experience, and references by February 1st for placement consideration.
1. The document discusses trees as a non-linear data structure that can represent hierarchical relationships. It specifically focuses on binary trees, their basic structure, properties, representations, and converting general trees to binary trees.
2. Binary trees have properties like a root node, leaf nodes, parent and child nodes, as well as left and right subtrees. They can be represented sequentially using arrays or linked lists.
3. General trees can be converted to binary trees by deleting branches except the leftmost one, and drawing new branches from each node to its right neighbor at the same level.
Trees are hierarchical data structures that can represent relationships between data items. They are useful for representing organizational charts, file systems, and programming environments. Key tree concepts include the root node, internal and leaf nodes, ancestors and descendants, subtrees, depth, height, and degree. Common tree operations include traversing the tree using preorder, inorder, and postorder traversal methods, evaluating expression trees, and using trees for data compression through Huffman coding. Huffman coding assigns variable-length binary codes to characters based on their frequency, allowing more common characters to have shorter codes to reduce the overall file size.
The document discusses various tree data structures and operations including binary trees, binary search trees, and threaded binary trees. It begins with introductions to tree terminology and representations. It then covers properties, traversals, and additional operations for binary trees such as copying and testing equality. Binary search trees and their search operation are discussed next. The document concludes with descriptions of threaded binary trees and how they reduce null pointers.
Trees are useful for representing hierarchically ordered data like directory structures and family trees. A tree is a collection of nodes connected by branches, with each node having at most one parent. It is defined recursively as a root node with zero or more subtrees. Binary trees restrict nodes to having at most two children. Trees can be traversed in preorder, inorder, or postorder. Binary search trees organize nodes to keep all left descendants less than the parent and all right descendants greater than the parent. Graphs generalize trees by allowing many-to-many relationships between nodes.
The document discusses binary trees and their implementation. It defines a binary tree as a tree where each node has at most two children. Binary trees are implemented by having each node store the data and pointers to its left and right children. The document also discusses traversing binary trees using preorder, postorder and inorder traversals and provides examples of using these traversals to list files in a directory and calculate their total size. Expression trees are given as an example of binary trees, where leaves are operands and internal nodes are operators. Algorithms are provided to construct expression trees from postfix notation.
The document discusses trees as a data structure. It begins by defining basic tree concepts such as nodes, branches, degrees of nodes, roots, leaves, internal nodes, parents, children, siblings, ancestors, descendants, paths, levels, heights, and subtrees. It then discusses binary trees specifically and their properties including traversal methods. Finally, it covers balanced binary search trees and techniques for maintaining balance such as rotations during insertions and deletions.
- A tree is a connected graph without simple circuits. Properties include a unique path between vertices and no loops or multiple edges.
- Trees can be used to represent hierarchies and organize data. Examples include organizational charts and file systems.
- Binary search trees allow efficient searching through ordered data by storing keys so that all values in the left subtree are less than the root and all values in the right subtree are greater.
- Traversal algorithms like preorder, inorder and postorder visit the vertices of a rooted tree in a systematic way and have applications in parsing expressions and serializing data.
This document discusses binary trees and their representations and uses. It defines key terms related to binary trees like nodes, leaves, siblings, and levels. It describes different types of binary trees like complete and extended binary trees. It explains how to represent binary trees in memory using linked and sequential representations. It provides examples of how binary trees can be used to represent expressions and model tournaments. It also discusses algorithms for traversing binary trees in preorder, inorder and postorder sequences.
This document discusses binary trees. It defines a binary tree as a structure containing nodes with two self-referenced fields - a left reference and a right reference. Each node can have at most two child nodes. It provides examples of common binary tree terminology like root, internal nodes, leaves, siblings, depth, and height. It also describes different ways to represent binary trees using arrays or links and their tradeoffs. Complete binary trees are discussed as an optimal structure with height proportional to log of number of nodes.
Tree is a hierarchical data structure that stores information in a parent-child relationship. There are different types of trees including binary trees, binary search trees, and expression trees. Binary trees restrict each node to having zero, one, or two children while binary search trees further require that all left descendants of a node are less than the node and all right descendants are greater than or equal. Expression trees represent mathematical expressions with operators as internal nodes and operands as leaves.
The document discusses different types of binary trees and tree traversal methods. It defines binary trees and outlines their key properties. It also describes different types of binary trees such as strictly binary trees, complete binary trees, and almost complete binary trees. Finally, it discusses two methods for traversing trees - depth-first traversal and breadth-first traversal, and covers preorder, inorder and postorder traversal techniques for binary trees.
The document discusses trees and graphs. It begins by defining a tree as a set of nodes where one node is the root and all other nodes can be partitioned into non-empty sub-trees. It then provides terminology for trees such as root node, sub-trees, leaf nodes, paths, ancestors, descendants, levels, and degrees. It describes different types of trees including general trees, forests, binary trees, expression trees, and tournament trees. It also covers linked and sequential representations of binary trees and traversing algorithms for binary trees including pre-order, in-order, post-order, and level-order traversals. Finally, it discusses applications of trees and binary search trees.
- A tree is a nonlinear hierarchical data structure that stores elements with parent-child relationships. Common examples include family trees, book tables of contents, and file system directories.
- Trees have nodes connected by edges, with one node designated as the root. Nodes have properties like parents, children, siblings, ancestors, descendants. A tree can be empty or have a root node with zero or more subtrees.
- Tree traversal algorithms like preorder, inorder, and postorder recursively visit each node by following different traversal orders. These are used to search, display, or evaluate information stored in the tree.
This document provides information about tree data structures and binary trees. It defines key tree terminology like nodes, leaves, ancestors, descendants. It also defines binary tree terms like complete binary tree, strictly binary tree, and expression trees. The document discusses tree traversals like inorder, preorder and postorder traversal. It provides examples of tree representations and operations like insertion, deletion and searching in binary search trees. It also covers heaps and their applications. Sample Python code is given to implement a binary search tree with functions for insertion, searching and deletion of nodes.
This document discusses various tree data structures and algorithms. It begins by defining a tree as a hierarchical data structure consisting of nodes connected by edges. It then discusses different tree terminology like root, child, parent, leaves, siblings, etc. It describes different types of trees like binary trees and general trees. It covers implementations of trees using arrays and linked lists. It discusses various tree traversal algorithms like inorder, preorder and postorder traversals. It describes applications of trees like representing directories in a file system and calculating the size of directories. It covers binary search trees and algorithms for operations on them like search, insert, find minimum and find maximum.
This document defines and explains the key concepts of binary trees. It begins by defining a binary tree as a collection of nodes where each node contains data and pointers to its left and right children. It describes the root node and how trees can be empty or non-empty. It then explains terms like subtrees, successors, leaf nodes, siblings, levels, and degrees of nodes. The document also covers traversing binary trees using pre-order, in-order, and post-order algorithms and representing binary trees in memory using linked and sequential structures. It concludes with an example of using a binary tree to represent an algebraic expression.
The document discusses binary trees and binary search trees. It begins with definitions of tree, binary tree, and binary search tree. It describes the key properties and terminology used for trees including nodes, degrees, heights, paths, etc. It then covers various tree traversal methods like preorder, inorder and postorder traversal. Operations for binary search trees like searching, insertion and deletion of nodes are explained along with algorithms. Different representations of binary trees using arrays and linked lists are also presented.
Trees are hierarchical data structures that can represent relationships between data items. A tree consists of nodes connected by edges, with one root node at the top. Examples of trees include biological classification trees, file system directories, and HTML page elements.
Key terminology includes nodes, edges, the root node, leaf nodes, paths between nodes, parent and child relationships, subtrees, levels, heights, and depths. Binary trees have at most two child nodes per parent, and can be traversed in preorder, inorder, or postorder to visit the nodes.
This document provides information about different tree data structures including binary trees, binary search trees, AVL trees, red-black trees, splay trees, and B-trees. Binary search trees allow for fast searching and maintain an ordered structure. AVL and red-black trees are self-balancing binary search trees that ensure fast search, insert, and delete times by keeping the tree balanced. B-trees are multiway search trees that allow for efficient storage and retrieval of data in databases and file systems.
The document provides information about different types of trees:
- A tree is a connected undirected graph without cycles. It has a unique path between any two vertices.
- Rooted trees have one distinguished root vertex. They define the level, height and relationships between parent/child and ancestor/descendant vertices.
- Binary trees restrict each vertex to having zero, one or two children. Ordered binary trees further define the left and right children.
- Expression trees represent mathematical expressions, with operators in internal nodes and operands in leaves. Their structure dictates evaluation order.
- Other tree properties discussed include those of full/complete binary trees and algorithms for finding minimum spanning trees like Prim's and K
The document discusses various tree data structures and algorithms, including binary trees, binary search trees, and expression trees. It covers tree terminology, implementations, traversals like inorder and preorder, and applications such as representing file systems and evaluating expressions. Non-linear data structures like trees allow storing hierarchical data in a way that facilitates fast search, insert, and delete operations.
The document discusses different types of trees used in data structures and algorithms. It defines trees and their key terminology like root, edge, parent, child, leaf nodes, etc. It then describes different types of trees including general trees, binary trees, binary search trees, and their properties. It also covers tree traversal methods like preorder, inorder and postorder traversal and representations of binary trees using arrays and linked lists.
Here are the steps to construct the binary search tree from the given pre-order and in-order traversals:
1. The root node is the first element of pre-order traversal, which is 'a'.
2. Search for 'a' in in-order traversal. Elements before 'a' are in left subtree and elements after are in right subtree.
3. Recursively construct left subtree with pre-order elements 'b,c,d' and in-order elements 'c,d,b'.
4. Recursively construct right subtree with pre-order elements 'e,g,h,j' and in-order elements 'h,g,j,e'.
Trees are useful for representing hierarchically ordered data like directory structures and family trees. A tree is a collection of nodes connected by branches, with each node having at most one parent. It is defined recursively as a root node with zero or more subtrees. Binary trees restrict nodes to having at most two children. Trees can be traversed in preorder, inorder, or postorder. Binary search trees organize nodes to keep all left descendants less than the parent and all right descendants greater than the parent. Graphs generalize trees by allowing many-to-many relationships between nodes.
The document discusses binary trees and their implementation. It defines a binary tree as a tree where each node has at most two children. Binary trees are implemented by having each node store the data and pointers to its left and right children. The document also discusses traversing binary trees using preorder, postorder and inorder traversals and provides examples of using these traversals to list files in a directory and calculate their total size. Expression trees are given as an example of binary trees, where leaves are operands and internal nodes are operators. Algorithms are provided to construct expression trees from postfix notation.
The document discusses trees as a data structure. It begins by defining basic tree concepts such as nodes, branches, degrees of nodes, roots, leaves, internal nodes, parents, children, siblings, ancestors, descendants, paths, levels, heights, and subtrees. It then discusses binary trees specifically and their properties including traversal methods. Finally, it covers balanced binary search trees and techniques for maintaining balance such as rotations during insertions and deletions.
- A tree is a connected graph without simple circuits. Properties include a unique path between vertices and no loops or multiple edges.
- Trees can be used to represent hierarchies and organize data. Examples include organizational charts and file systems.
- Binary search trees allow efficient searching through ordered data by storing keys so that all values in the left subtree are less than the root and all values in the right subtree are greater.
- Traversal algorithms like preorder, inorder and postorder visit the vertices of a rooted tree in a systematic way and have applications in parsing expressions and serializing data.
This document discusses binary trees and their representations and uses. It defines key terms related to binary trees like nodes, leaves, siblings, and levels. It describes different types of binary trees like complete and extended binary trees. It explains how to represent binary trees in memory using linked and sequential representations. It provides examples of how binary trees can be used to represent expressions and model tournaments. It also discusses algorithms for traversing binary trees in preorder, inorder and postorder sequences.
This document discusses binary trees. It defines a binary tree as a structure containing nodes with two self-referenced fields - a left reference and a right reference. Each node can have at most two child nodes. It provides examples of common binary tree terminology like root, internal nodes, leaves, siblings, depth, and height. It also describes different ways to represent binary trees using arrays or links and their tradeoffs. Complete binary trees are discussed as an optimal structure with height proportional to log of number of nodes.
Tree is a hierarchical data structure that stores information in a parent-child relationship. There are different types of trees including binary trees, binary search trees, and expression trees. Binary trees restrict each node to having zero, one, or two children while binary search trees further require that all left descendants of a node are less than the node and all right descendants are greater than or equal. Expression trees represent mathematical expressions with operators as internal nodes and operands as leaves.
The document discusses different types of binary trees and tree traversal methods. It defines binary trees and outlines their key properties. It also describes different types of binary trees such as strictly binary trees, complete binary trees, and almost complete binary trees. Finally, it discusses two methods for traversing trees - depth-first traversal and breadth-first traversal, and covers preorder, inorder and postorder traversal techniques for binary trees.
The document discusses trees and graphs. It begins by defining a tree as a set of nodes where one node is the root and all other nodes can be partitioned into non-empty sub-trees. It then provides terminology for trees such as root node, sub-trees, leaf nodes, paths, ancestors, descendants, levels, and degrees. It describes different types of trees including general trees, forests, binary trees, expression trees, and tournament trees. It also covers linked and sequential representations of binary trees and traversing algorithms for binary trees including pre-order, in-order, post-order, and level-order traversals. Finally, it discusses applications of trees and binary search trees.
- A tree is a nonlinear hierarchical data structure that stores elements with parent-child relationships. Common examples include family trees, book tables of contents, and file system directories.
- Trees have nodes connected by edges, with one node designated as the root. Nodes have properties like parents, children, siblings, ancestors, descendants. A tree can be empty or have a root node with zero or more subtrees.
- Tree traversal algorithms like preorder, inorder, and postorder recursively visit each node by following different traversal orders. These are used to search, display, or evaluate information stored in the tree.
This document provides information about tree data structures and binary trees. It defines key tree terminology like nodes, leaves, ancestors, descendants. It also defines binary tree terms like complete binary tree, strictly binary tree, and expression trees. The document discusses tree traversals like inorder, preorder and postorder traversal. It provides examples of tree representations and operations like insertion, deletion and searching in binary search trees. It also covers heaps and their applications. Sample Python code is given to implement a binary search tree with functions for insertion, searching and deletion of nodes.
This document discusses various tree data structures and algorithms. It begins by defining a tree as a hierarchical data structure consisting of nodes connected by edges. It then discusses different tree terminology like root, child, parent, leaves, siblings, etc. It describes different types of trees like binary trees and general trees. It covers implementations of trees using arrays and linked lists. It discusses various tree traversal algorithms like inorder, preorder and postorder traversals. It describes applications of trees like representing directories in a file system and calculating the size of directories. It covers binary search trees and algorithms for operations on them like search, insert, find minimum and find maximum.
This document defines and explains the key concepts of binary trees. It begins by defining a binary tree as a collection of nodes where each node contains data and pointers to its left and right children. It describes the root node and how trees can be empty or non-empty. It then explains terms like subtrees, successors, leaf nodes, siblings, levels, and degrees of nodes. The document also covers traversing binary trees using pre-order, in-order, and post-order algorithms and representing binary trees in memory using linked and sequential structures. It concludes with an example of using a binary tree to represent an algebraic expression.
The document discusses binary trees and binary search trees. It begins with definitions of tree, binary tree, and binary search tree. It describes the key properties and terminology used for trees including nodes, degrees, heights, paths, etc. It then covers various tree traversal methods like preorder, inorder and postorder traversal. Operations for binary search trees like searching, insertion and deletion of nodes are explained along with algorithms. Different representations of binary trees using arrays and linked lists are also presented.
Trees are hierarchical data structures that can represent relationships between data items. A tree consists of nodes connected by edges, with one root node at the top. Examples of trees include biological classification trees, file system directories, and HTML page elements.
Key terminology includes nodes, edges, the root node, leaf nodes, paths between nodes, parent and child relationships, subtrees, levels, heights, and depths. Binary trees have at most two child nodes per parent, and can be traversed in preorder, inorder, or postorder to visit the nodes.
This document provides information about different tree data structures including binary trees, binary search trees, AVL trees, red-black trees, splay trees, and B-trees. Binary search trees allow for fast searching and maintain an ordered structure. AVL and red-black trees are self-balancing binary search trees that ensure fast search, insert, and delete times by keeping the tree balanced. B-trees are multiway search trees that allow for efficient storage and retrieval of data in databases and file systems.
The document provides information about different types of trees:
- A tree is a connected undirected graph without cycles. It has a unique path between any two vertices.
- Rooted trees have one distinguished root vertex. They define the level, height and relationships between parent/child and ancestor/descendant vertices.
- Binary trees restrict each vertex to having zero, one or two children. Ordered binary trees further define the left and right children.
- Expression trees represent mathematical expressions, with operators in internal nodes and operands in leaves. Their structure dictates evaluation order.
- Other tree properties discussed include those of full/complete binary trees and algorithms for finding minimum spanning trees like Prim's and K
The document discusses various tree data structures and algorithms, including binary trees, binary search trees, and expression trees. It covers tree terminology, implementations, traversals like inorder and preorder, and applications such as representing file systems and evaluating expressions. Non-linear data structures like trees allow storing hierarchical data in a way that facilitates fast search, insert, and delete operations.
The document discusses different types of trees used in data structures and algorithms. It defines trees and their key terminology like root, edge, parent, child, leaf nodes, etc. It then describes different types of trees including general trees, binary trees, binary search trees, and their properties. It also covers tree traversal methods like preorder, inorder and postorder traversal and representations of binary trees using arrays and linked lists.
Here are the steps to construct the binary search tree from the given pre-order and in-order traversals:
1. The root node is the first element of pre-order traversal, which is 'a'.
2. Search for 'a' in in-order traversal. Elements before 'a' are in left subtree and elements after are in right subtree.
3. Recursively construct left subtree with pre-order elements 'b,c,d' and in-order elements 'c,d,b'.
4. Recursively construct right subtree with pre-order elements 'e,g,h,j' and in-order elements 'h,g,j,e'.
2. Tree Terminology
● A special type of graph called a tree turns out to be a
very useful representation of data.
● DEFINITION: TREE A tree is an acyclic,
connected graph with one node designated as the root
of the tree.
● An acyclic, connected graph with no designated root
node is called a nonrooted tree or a free tree.
Section 5.2 Trees and Their Representations 1
3. Defining Trees Recursively
● A tree can also be defined recursively. A single node
is a tree (with that node as its root). If T1, T2, ... , Tt are
disjoint trees with roots r1, r2, ... , rt, the graph formed
by attaching a new node r by a single arc to each of r1,
r2, ... , rt is a tree with root r. The nodes r1, r2, ... , rt
are children of r, and r is a parent of r1, r2, ... , rt.
Section 5.2 Trees and Their Representations 2
4. Tree Terminology
● The depth of a node in a tree is the length of the path
from the root to the node; the root itself has depth 0.
● The depth (height) of the tree is the maximum depth
of any node in the tree; in other words, it is the length
of the longest path from the root to any node.
● A node with no children is called a leaf of the tree.
● All nonleaves are internal nodes.
● A forest is an acyclic graph (not necessarily
connected).
Section 5.2 Trees and Their Representations 3
5. Tree Terminology
● Binary trees are those where each node has at most two
children.
● Each child of a node is designated as either the left child or the
right child.
● A full binary tree (as seen in the middle figure below) occurs
when all internal nodes have two children and all leaves are at
the same depth.
● A complete binary tree (as seen in the right figure below) is an
almost-full binary tree; the bottom level of the tree is filling
from left to right but may not have its full complement of
leaves.
Section 5.2 Trees and Their Representations 4
6. Applications of Trees
● Decision trees were used to solve counting problems
in Chapter 3.
● By using trees, a collection of records can be
efficiently searched to locate a particular record or to
determine that a record is not in the collection.
● A family tree is usually, indeed, a tree.
● Files stored on a computer are organized in a
hierarchical (treelike) structure.
● Algebraic expressions involving binary operations can
be represented by labeled binary trees.
Section 5.2 Trees and Their Representations 5
7. Binary Tree Representation
● Because a tree is also a graph, representations for
graphs in general can also be used for trees.
● Binary trees, however, have special characteristics that
we want to capture in the representation, namely, the
identity of the left and right child.
● The equivalent of an adjacency matrix is a two-
column array (or an array of records) where the data
for each node is the left and right child of that node.
● The equivalent of the adjacency list representation is a
collection of records with three fields containing,
respectively, the current node, a pointer to the record
for the left-child node, and a pointer to the record for
the right-child node.
Section 5.2 Trees and Their Representations 6
8. Binary Tree Representation Example
● The tree represented by the figure above has the following
adjacency list and adjacency matrix representations.
Section 5.2 Trees and Their Representations 7
9. Tree Traversal Algorithms
● If a tree structure is being used to store data, it is often
helpful to have a systematic mechanism for writing out the
data values stored at all the nodes.
● This can be accomplished by traversing the tree, that is,
visiting each of the nodes in the tree structure.
● The three common tree traversal algorithms are preorder,
inorder, and postorder traversal.
● The terms preorder, inorder, and postorder refer to the
order in which the root of a tree is visited compared to the
subtree nodes.
Section 5.2 Trees and Their Representations 8
10. Tree Traversal Algorithms
● In preorder traversal, the root of the tree is visited first
and then the subtrees are processed left to right, each in
preorder.
● ALGORITHM Preorder
Preorder(tree T)
//Writes the nodes of a tree with root r in preorder
write(r)
for i 1 to t do
Preorder(Ti)
end for
end Preorder
Section 5.2 Trees and Their Representations 9
11. Tree Traversal Algorithms
● In inorder traversal, the left subtree is processed by an inorder
traversal, then the root is visited, and then the remaining
subtrees are processed from left to right, each in inorder. If the
tree is binary, the result is that the root is visited between
processing of the two subtrees.
● ALGORITHM Inorder
Inorder(tree T )
//Writes the nodes of a tree with root r in inorder
Inorder(T1)
write(r)
for i 2 to t do
Inorder(Ti)
end for
end Inorder
Section 5.2 Trees and Their Representations 10
12. Tree Traversal Algorithms
● In postorder traversal, the root is visited last, after
all subtrees have been processed from left to right in
postorder.
● ALGORITHM Postorder
Postorder(tree T )
//Writes the nodes of a tree with root r in postorder
for i 1 to t do
Postorder(Ti)
end for
write(r)
end Postorder
Section 5.2 Trees and Their Representations 11
13. Tree Traversal Algorithms Example
● What is the preorder, inorder, and postorder traversal
for the following tree?
● The preorder (root, left, right) traversal produces: a, b, d, e, c, f,
h, i, g.
● The inorder (left, root, right) traversal produces: d, b, e, a, h, f, i,
c, g.
● The postorder (left, right, root) traversal produces: d, e, b, h i, f,
g, c, a.
Section 5.2 Trees and Their Representations 12
14. Infix Notation
● The binary tree in the figure below represents the algebraic
expression (2 + x) - (y * 3). If we do an inorder traversal of
the expression tree, we retrieve the original algebraic
expression.
● Parentheses are added as we complete the processing of a
subtree.
● This is called infix notation.
Section 5.2 Trees and Their Representations 13
15. Polish Notation
● A preorder traversal of a tree as seen in this figure gives the expression
* (2 + x) x 4.
● Here the operation symbol precedes its operands. This form of an
expression is called prefix notation, or Polish notation. The
expression can be translated into infix form as follows:
* + 2 x 4 → * (2 + x) 4 → (2 + x ) * 4
● A postorder traversal gives the expression 2 x 4 *, where the operation
symbol follows its operands. This form of an expression is called
postfix notation, or reverse Polish notation (or just RPN). The
expression can be translated into infix form as follows:
2 x 4 * → (2 + x) 4 * → (2 + x) * 4
● Neither prefix nor postfix form requires parentheses to avoid
ambiguity.
Section 5.2 Trees and Their Representations 14