0% found this document useful (0 votes)
46 views

Binary Trees

Binary trees are trees where every node has at most two children, labeled as left or right. They can be defined recursively as either being empty or consisting of a root node and left and right subtrees. Binary trees have an interface for accessing and checking nodes and operations for inserting, removing, and attaching nodes that run in O(1) time. The height and number of nodes in a binary tree are related, and binary trees can be drawn by assigning x and y coordinates to nodes based on their position in an in-order traversal and depth.

Uploaded by

RAJENDRAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Binary Trees

Binary trees are trees where every node has at most two children, labeled as left or right. They can be defined recursively as either being empty or consisting of a root node and left and right subtrees. Binary trees have an interface for accessing and checking nodes and operations for inserting, removing, and attaching nodes that run in O(1) time. The height and number of nodes in a binary tree are related, and binary trees can be drawn by assigning x and y coordinates to nodes based on their position in an in-order traversal and depth.

Uploaded by

RAJENDRAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Binary trees

Binary trees
Definition: A binary tree is a tree such that
every node has at most 2 children
each node is labeled as being either a left chilld or a right child

Recursive definition:
a binary tree is empty;
or it consists of
a node (the root) that stores an element
a binary tree, called the left subtree of T
a binary tree, called the right subtree of T

Binary tree interface


Tree T
left(v)
right(v)
hasLeft(v)
hasRight(v)
+ isInternal(v), is External(v), isRoot(v), size(), isEmpty()

Properties of binary trees


In a binary tree
level 0 has <=
level 1 has <=
level 2 has <=
...
level i has <=

1 node
2 nodes
4 nodes

d=0
d=1

2^i nodes

d=2

d=3

Proposition: Let T be a binary tree with n nodes and height h. Then


h+1 <= n <= 2 h+1 -1
lg(n+1) - 1 <= h <= n-1

Binary tree operations


insertLeft(v,e):
create and return a new node w storing element e, add w as the left child of v
an error occurs if v already has a left child
insertRight(v,e)

remove(v):
remove node v, replace it with its child, if any, and return the element stored at v
an error occurs if v has 2 children
addRoot(e):
create and return a new node r storing element e and make r the root of the tree;
an error occurs if the tree is not empty
attach(v,T1, T2):
attach T1 and T2 respectively as the left and right subtrees of the external node v
an error occurs if v is not external

Performance
all

O(1)
left(v)
right(v)
hasLeft(v)
hasRight(v)
isInternal(v)
is External(v)
isRoot(v)
size()
isEmpty()
addRoot(e)
insertLeft(v,e)
insertRight(v,e)
remove(e)

Application: Tree drawing


We can use an in-order traversal for drawing a tree. We can draw a binary tree by assigning
coordinate x and y of each node in the following way:
x(v) is the number of nodes visited before v in the in-order traversal of v
y(v) is the depth of v

0
1
2
3
4

You might also like