Title: General Tree Concepts Binary Trees
1General Tree ConceptsBinary Trees
Mark Allen Weiss Data Structures and Algorithm
Analysis in Java
- Data Structures and Algorithms
2Outline
- Tree Data Structure
- Examples
- Definition
- Terminology
- Binary Tree
- Definitions
- Examples
- Tree Traversal
- Binary Search Trees
- Definition
3Linear Lists and Trees
- Linear lists (arrays, linked list)are useful for
serially ordered data - (e1,e2,e3,,en)
- Days of week
- Months in a year
- Students in a class
- Trees are useful for hierarchically ordered data
- Khalids descendants
- Corporate structure
- Government Subdivisions
- Software structure
4Examples of trees
- directory structure
- family trees
- all descendants of a particular person
- all ancestors born after year 1800 of a
particular person - evolutionary tress (also called phylogenetic
trees) - albegraic expressions
5Khalids Descendants
What are other examples of hierarchically ordered
data?
6Property of a Tree
- A tree t is a finite nonempty set of nodes
- One of these elements is called the root
- Each node is connected by an edge to some other
node - A tree is a connected graph
- There is a path to every node in the tree
- There are no cycles in the tree.
- It can be proved that a tree has one less edge
than the number of nodes. - Usually depicted with the root at the top
7Is it a Tree?
NO! All the nodes are not connected
yes! (but not a binary tree)
yes!
NO! There is a cycle and an extra edge (5 nodes
and 5 edges)
yes! (its actually the same graph as the blue
one) but usually we draw tree by its levels
8Subtrees
9Tree Terminology
- The element at the top of the hierarchy is the
root. - Elements next in the hierarchy are the children
of the root. - Elements next in the hierarchy are the
grandchildren of the root, and so on. - Elements at the lowest level of the hierarchy are
the leaves.
10Other Definitions
- Leaves, Parent, Grandparent, Siblings,Ancestors,
Descendents
Leaves Hassan,Gafar,Sami,Mohmmed
Parent(Musa) Khalid
Grandparent(Sami) Musa
Siblings(Musa) Ahmed,Omer
Ancestors(Hassan) Amed,Khalid
Descendents(Musa)Fahad,Sami
11Levels and Height
- Root is at level 0 and its children are at level
1.
12 Degree of a node
- Node degree is the number of children it has
13Tree Degree
- Tree degree is the maximum of node degrees
tree degree 3
14Measuring Trees
- The height of a node v is the number of nodes on
the longest path from v to a leaf - The height of the tree is the height of the root,
which is the number of nodes on the longest path
from the root to a leaf - The depth of a node v is the number of nodes on
the path from the root to v - This is also referred to as the level of a node
- Note that there are slightly different
formulations of the height of a tree - Where the height of a tree is said to be the
length (the number of edge) on the longest path
from node to a leaf
15Trees - Example
Level
0
1
2
3
16Tree Terminology Example
- A is the root node
- B is the parent of D and E
- C is the sibling of B
- D and E are the children of B
- D, E, F, G, I are external nodes, or leaves
- A, B, C, H are internal nodes
- The depth, level, or path length of E is 2
- The height of the tree is 3
- The degree of node B is 2
17Tree Terminology Example
A
path from A to D to G
B
D
subtree rooted at B
leaves C,E,F,G
G
E
F
18Tree Representation
Class TreeNode Object element
TreeNode firstChild TreeNode
nextSibling
19Binary Trees
- A finite (possibly empty) collection of elements
- A nonempty binary tree has a root element and the
remaining elements (if any) are partitioned into
two binary trees - They are called the left and right subtrees of
the binary tree
A
B
C
right child of A
left subtree of A
F
G
D
E
right subtree of C
H
I
J
20Difference Between a Tree a Binary Tree
- A binary tree may be empty a tree cannot be
empty. - No node in a binary tree may have a degree more
than 2, whereas there is no limit on the degree
of a node in a tree. - The subtrees of a binary tree are ordered those
of a tree are not ordered.
21Binary Tree for Expressions
22Height of a Complete Binary Tree
At each level the number of the nodes is doubled.
total number of nodes
1 2 22 23 24 - 1 15
23Nodes and Levels in a Complete Binary Tree
Number of the nodes in a tree with M levels 1
2 22 . 2M 2 (M1) - 1 22M - 1 Let
N be the number of the nodes. N 22M - 1,
22M N 1 2M (N1)/2 M log( (N1)/2
) N nodes log( (N1)/2 ) O(log(N))
levels M levels 2 (M1) - 1 O(2M )
nodes
24Binary Tree Properties
- The drawing of every binary tree with n elements,
n gt 0, has exactly n-1 edges. - A binary tree of height h, h gt 0, has at least h
and at most 2h-1 elements in it.
25Binary Tree Properties
- 3. The height of a binary tree that contains n
elements, n gt 0, is at least ?(log2(n1))? and
at most n.
26Full Binary Tree
- A full binary tree of height h has exactly 2h-1
nodes. - Numbering the nodes in a full binary tree
- Number the nodes 1 through 2h-1
- Number by levels from top to bottom
- Within a level, number from left to right
27Node Number Property of Full Binary Tree
- Parent of node i is node ?(i/2)?, unless i 1
- Node 1 is the root and has no parent
28Node Number Property of Full Binary Tree
- Left child of node i is node 2i, unless 2i gt
n,where n is the total number of nodes. - If 2i gt n, node i has no left child.
29Node Number Property of Full Binary Tree
- Right child of node i is node 2i1, unless 2i1 gt
n,where n is the total number of nodes. - If 2i1 gt n, node i has no right child.
30Complete Binary Tree with N Nodes
- Start with a full binary tree that has at least n
nodes - Number the nodes as described earlier.
- The binary tree defined by the nodes numbered 1
through n is the n-node complete binary tree. - A full binary tree is a special case of a
complete binary tree
31Example of Complete Binary Tree
- Complete binary tree with 10 nodes.
- Same node number properties (as in full binary
tree) also hold here.
32Binary Tree Representation
- Array representation
- Linked representation
33Array Representation of Binary Tree
- The binary tree is represented in an array by
storing each element at the array position
corresponding to the number assigned to it.
34Incomplete Binary Trees
- Complete binary tree with some missing elements
35Example Binary Tree
To find a nodes parent use this
formula  Parents Index (Childs Index 1) /
2
Array index from 0 Ex. To find nodes 2 left and
right child Left Childs Index 2 Parents
Index 1 Right Childs Index 2 Parents
Index 2
36Linked Representation of Binary Tree
- The most popular way to present a binary tree
- Each element is represented by a node that has
two link fields (leftChild and rightChild) plus
an element field - Each binary tree node is represented as an object
whose data type is binaryTreeNode - The space required by an n node binary tree isn
sizeof(binaryTreeNode)
37Linked Representation of Binary Tree
38Node Class For Linked Binary Tree
- TreeNode class
- private class TreeNode
-
- private String data
- private TreeNode left
- private TreeNode right
- Â
- public TreeNode(String element)
-
- data element
- left null
- right null
-
-
39Common Binary Tree Operations
- Determine the height
- Determine the number of nodes
- Make a copy
- Determine if two binary trees are identical
- Display the binary tree
- Delete a tree
- If it is an expression tree, evaluate the
expression - If it is an expression tree, obtain the
parenthesized form of the expression
40Binary Tree Traversal
- A traversal of a tree is a systematic way of
accessing or "visiting" all the nodes in the
tree. - There are three basic traversal schemes
preorder, postorder, inorder - .In any of these traversals we always visit the
left subtree before the right
41Binary Tree Traversal Methods
- Preorder
- The root of the subtree is processed first before
going into the left then right subtree (root,
left, right). - Inorder
- After the complete processing of the left subtree
the root is processed followed by the processing
of the complete right subtree (left, root,
right). - Postorder
- The root is processed only after the complete
processing of the left and right subtree (left,
right, root). - Level order
- The tree is processed by levels. So first all
nodes on level i are processed from left to right
before the first node of level i1 is visited
42Preorder Traversal
- public void preOrderPrint()
-
- preOrderPrint(root)
-
- Â private void preOrderPrint(TreeNode tree)
-
- if (tree ! null)
-
- System.out.print(tree.data " ") //
Visit the root - preOrderPrint(tree.left)// Traverse
the left subtree - preOrderPrint(tree.right) // Traverse
the right subtree -
-
- Â
43Preorder Example (visit print)
N-L-R
O M N K I J L G E F C A B D H
44Preorder of Expression Tree
- / a b - c d e f
- Gives prefix form of expression.
45Inorder Traversal
public void inOrderPrint()
inOrderPrint(root) Â public void
inOrderPrint(TreeNode t) if (t ! null)
inOrderPrint(t.left)
System.out.print(t.data " ")
inOrderPrint(t.right)
46Inorder Example (visit print)
O N M L K J I H G F E D C B A
L-N-R
47Inorder by Projection
48Inorder of Expression Tree
- Gives infix form of expression, which is how we
normally write math expressions. What about
parentheses?
49Postorder Traversal
public void ostOrderPrint()
postOrderPrint(root) Â public void
postOrderPrint(TreeNode t) if (t ! null)
postOrderPrint(t.left)
postOrderPrint(t.right) System.out.print(t.
data " ")
50Postorder Example (visit print)
L-R-N
H L N O M J K I D F G E B C A
51Postorder of Expression Tree
- a b c d - e f /
- Gives postfix form of expression.
52Level Order Example (visit print)
- Add and delete nodes from a queue
- Output a b c d e f g h i j
53Space and Time Complexity
- The space complexity of each of the four
traversal algorithm is O(n) - The time complexity of each of the four traversal
algorithm is O(n)
54Summary
- Tree, Binary Tree
- In order to process the elements of a tree, we
consider accessing the elements in certain order - Tree traversal is a tree operation that involves
"visiting (or" processing") all the nodes in a
tree. - Types of traversing
- Pre-order Visit node first, pre-order all its
subtrees from leftmost to rightmost. - Inorder Inorder the node in left subtree and
then visit the root following by inorder
traversal of all its right subtrees. - Post-order Post-order the node in left subtree
and then post-order the right subtrees followed
by visit to the node.