Title: Binary Tree
1Binary Tree
- Data Structures
- Rutgers University
2Binary Tree
Terminology Nodes Branches Root Children Left
child
Right child Leaf Internal Nodes Path
- Binary Tree
- Consists of
- Node
- Left and Right sub-trees
- Both sub-trees are binary trees
Note the recursive definition!
3Tree Traversal
- Traversal visiting every node of a tree
- Three basic alternatives
- Pre-order
- Root
- Left sub-tree
- Right sub-tree
x A x B C x D E F
R
L
R
L
L
R
4Tree Traversal
- Traversal visiting every node of a tree
- Three basic alternatives
- In-order
- Left sub-tree
- Root
- Right sub-tree
µ
³
²
A x B C x D x E F
L
L
R
5Tree Traversal
- Traversal visiting every node of a tree
- Three basic alternatives
- Post-order
- Left sub-tree
- Right sub-tree
- Root
µ
³
²
A B C D E x x F x
L
L
R
6Tree Traversal
- Additional alternative
- Level-order traversal
- Starting at the root level, go level by level in
the tree, visiting the nodes at any level in left
to right order
µ
³
²
xAxFxBCDE
7Traversal Examples
NOTE some of the traversals give same result
even if tree has changed
8What good is a traversal?
Standard traversals will be used to process
information in a tree by defining
What actions are performed when a node is
visited Examples Searching Count Height Pr
int
Which traversal method? Pre-order, in-order, po
st-order ? recursive Level-order ? not amenable f
or recursive
9A Binary Tree Class
Recursive definitions. BinaryTree P new Bina
ryTree() BinaryTree Q new BinaryTree() P.mak
eRoot (root) Q.makeRoot (leftOfP) P.attach
Left (Q)
P
Q
R
BinaryTree R new BinaryTree()
R.makeRoot(rightOfQ)
Q.attachRight
Once we have areference to the root, we have
access to all the nodes under it
This is analogous to the linked list where
reference to first node also referred to the
entire list
10Class Structure Binary Tree
Fields BinaryTree left, right , parent
Constructors BinaryTree() //creates an empty bi
n tree Methods Void makeRoot(Object data) //c
reatrs a root node with data //TreeViolation
Exception if not empty Void setData(Object data)
//sets the data at tree node Object getData() //
retrieves the data stored at tree node
BinaryTree root() //returns the root of the
tree Void attachLeft(BinaryTree tree) Void att
achRight(BinaryTree tree) BinaryTree detachLeft()
BinaryTree detachRight() Boolean isEmpty() V
oid clear()
All public functions have running time of O(1)
Exception root ?O(h) time Where h is the height
of the tree
Worst case for root is O(n)
11Traversal Procedures
Writing the recursive traversal procedures is a
breeze! In-order traversal prints the data whi
le visiting Void inorder (BinaryTree T) if
(T! null) inorder (T.left) System.out.pr
intln(T.getData()) inorder (T.right)
12Trees - Searching
- Binary search tree
- Produces a sorted list by in-order traversal
- In order A D E G H K L M N O P T V
13Trees - Searching
- Binary search tree
- Preserving the order
- Observe that this transformation preserves
thesearch tree
14Specifying a Binary Tree
- How to save in a file?
- Signature
- Encoding of a data structure and its contents in
plain text format that can be stored off-line and
used to recreate the data structure in memory
when needed.
Need a scheme to uniquely identify a binary tree
Tree traversals potential candidates
15Unique Specification
- Non Unique specification Each traversal
sequence
- Specifies two different trees
B
A
B
A
A
B
A
B
A
B
B
A
Inorder A B
Pre order A B
Post order A B
- Try combinations
- The signature of a binary tree can be either of
the following paris of traversals
- Preorder and inorder, or
- Postorder and inorder
16Building Binary Tree from Signature
- Pre f - a b c
- In f a b - c
f
-
c
a
b
Pre f - a b c In f a b - c
Step 1 Match in InOrder String
Step 2 repeat match f in left of InOrder st
ring Step 3 After hitting null on left, st
art on right part of the string
Identify left, right
Pre f - a b c In () f ()
Identify left, right
17BinaryTree Class Implementation
Left data right parent
Public class BinaryTree protected Object data
public BinaryTree left public BinaryTree rig
ht public BinaryTree parent public BinaryT
ree() data null left null right
null parent null
18More functions
Public void atachLeft (BinaryTree tree)
Throws TreeViolationException
if (left ! null) throw new TreeViolationExc
eption() if (tree ! null) tree.parent
this right tree
Public BinaryTree root() if (parent null)
return this //this is root BinaryTree
nextParent parent while (nextParent.parent !
null) nextParent nextParent.parent
return nextParent