Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals...
-
Upload
tobias-reed -
Category
Documents
-
view
217 -
download
0
description
Transcript of Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals...
![Page 1: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/1.jpg)
TreesDefinitions• Read Weiss, 4.1 – 4.2
Implementation• Nodes and Links• One Arrays• Three Arrays
Traversals• Preorder, Inorder, Postorder
K-ary Trees• Converting trees: k-ary ↔ binary
![Page 2: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/2.jpg)
Definitions
NodesEdgesRoot nodeInterior nodeLeafParentChildren
Ancestor / Proper ancestorDescendant / Proper
descendantLevel of a nodeHeight of a node / treeDegree of a node / treeDepth of nodePathAcyclic graph
![Page 3: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/3.jpg)
bj
c
dg
k
m l
i h f e
a root, height=4, depth=level=0
leaf, height=0, depth=level=4
degree=2
interior node, height=2, depth=level=2
degree of tree = 2height of tree = 4
descendants of “a”
proper descendants of “a”
degree=0
degree=1
![Page 4: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/4.jpg)
Implementing a Tree • Nodes and Links
A
B C
D
▲▲ ▲
▲ ▲
Node { Object value; Node lchild; Node rchild;} // Node
▲=null link
![Page 5: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/5.jpg)
Implementing a Tree • One array
A
B C
D
▲▲ ▲
▲ ▲
A: 0 1 2 3 4 5 6 7 8 9
- A B C - - D - - -
▲=null link
• A[1] is root• lchild of A[i] is A[2i]• rchild of A[i] is A[2i+1]• “-” means array element is null / not used• A[0] not used as a node• A[0] may be used to hold general info (e.g., number of nodes in tree)
![Page 6: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/6.jpg)
Preorder N L R
Traversals
preorder (Node t) if (t == null) return;
visit (t.value()); preorder (t.lchild()); preorder (t.rchild());
} // preorder
![Page 7: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/7.jpg)
Traversalsinorder (Node t) if (t == null) return;
inorder (t.lchild()); visit (t.value()); inorder (t.rchild());
} // inorder
• Inorder L N R
![Page 8: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/8.jpg)
Traversalspostorder (Node t) if (t == null) return;
postorder (t.lchild()); postorder (t.rchild()); visit (t.value());
} // postorder
• Postorder
L R N
![Page 9: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/9.jpg)
bj
c
dg
k
m l
i h f e
a
preorder: a j k m l b c g i h d f e
inorder: m k l j a b i g h c f d e
postorder: m l k j i h g f e d c b a
![Page 10: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/10.jpg)
K-ary Trees
n
g
e
jk
md
c
b
f
ip
q
a
degree of tree = 4degree of nodes f and n = 3height of tree = 3depth=level of m = 2
![Page 11: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/11.jpg)
K-ary Tree => Binary Tree
n
g
e
jk
md
c
b
f
ip
q
a
K-ary Binaryroot rootleftmost child left childright sibling right child
![Page 12: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/12.jpg)
n
g
e
jk
md
c
b
f
ip
q
a
n
g
e
jk
md
c
b
f
ip
q
a
TraversalsK-ary Tree Binary Tree
Preorder:Inorder:Postorder:
Preorder:Inorder:Postorder:
![Page 13: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/13.jpg)
Binary Search Trees
![Page 14: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/14.jpg)
Have all properties of binary treeItems in left subtree are smaller than items in
any nodeItems in right subtree are larger than items in
any node
BST Properties
![Page 15: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/15.jpg)
Items must be comparableAll items have a unique valueGiven two distinct items x and y either
value(x) < value(y)value(x) > value(y)
If value(x) = value(y) then x = yIt will simplify programming to assume there
are no duplicates in our set of items.
Items
![Page 16: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/16.jpg)
Need to map Items to a numerical valueIntegers
Value(x) = xPeople
Value(x) = ssnValue(x) = student id
Items
![Page 17: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/17.jpg)
ConstructorInsertFind
FindminFindmax
Remove
BST Operations
![Page 18: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/18.jpg)
Generally Recursive
BST Operations
BinaryNode operation( Comparable x, BinaryNode t ) {// End of path
if( t == null ) return null;
if( x.compareTo( t.element ) < 0 )return operation( x, t.left );
else if( x.compareTo( t.element ) > 0 )return operation( x, t.right );
else return t; // Match}
![Page 19: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/19.jpg)
private BinaryNode find( Comparable x, BinaryNode t )
{ if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return find( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return find( x, t.right ); else return t; // Match }
BST Find Method
![Page 20: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/20.jpg)
RemoveNode is leaf
Remove nodeNode has one child
Replace node with childNode has two children
Replace node with smallest child of right subtree.
BST Remove Operations
![Page 21: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/21.jpg)
private BinaryNode remove( Comparable x, BinaryNode t ) { if( t == null ) return t; // Item not found; do nothing if( x.compareTo( t.element ) < 0 ) t.left = remove( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = remove( x, t.right ); else if( t.left != null && t.right != null ) // Two children { t.element = findMin( t.right ).element; t.right = remove( t.element, t.right ); } else t = ( t.left != null ) ? t.left : t.right; return t; }
Remove method
![Page 22: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/22.jpg)
Review depth/heightDepth
Depth is number of path segments from root to node
Depth of node is distance from root to that node.
Depth is uniqueDepth of root is 0
Internal Path Length
![Page 23: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/23.jpg)
HeightHeight is maximum distance from node to a
leaf.There can be many paths from a node to a leaf.The height of the tree is another way of saying
height of the root.
Internal Path Length
![Page 24: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/24.jpg)
IPL is the sum of the depths of all the nodes in a tree
It gives a measure of how well balanced the tree is.
Internal Path Length
![Page 25: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/25.jpg)
Internal Path Length
11
2
N = 4IPL = 1 + 1 + 2 = 4
![Page 26: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/26.jpg)
Internal Path Length
1
2
N = 4IPL = 1 + 2 + 3 = 6
3
![Page 27: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/27.jpg)
Calculate IPL of all possible trees
Average IPL for N nodesN = 4
1
2
3
11
2
1
2 2
![Page 28: Definitions Read Weiss, 4.1 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.](https://reader033.fdocuments.in/reader033/viewer/2022052709/5a4d1b687f8b9ab0599b1b67/html5/thumbnails/28.jpg)
Simple to understandWorks for small datasetsBasis for more complicated treesUsing inheritance can implement
AVL treesSplay treesRed Black trees
Where do BST fit in