当且仅当节点 i 的左子树或右子树为空时,节点被称作外节点(实际上保存在二叉树中的节点都是内节点,外节点是逻辑上存在而无需保存。把一颗二叉树补上全部的外节点,则称为extended binary tree)。节点i的距离是节点 i 到它的后代中的最近的外节点所经过的边数。特别的,如果节点 i 本身是外节点,则它的距离为0;而空节点的距离规定为 -1。[1]
publicNodemerge(Nodex,Nodey){if(x==null)returny;if(y==null)returnx;// if this was a max height biased leftist tree, then the // next line would be: if(x.element < y.element)if(x.element.compareTo(y.element)>0){// x.element > y.elementNodetemp=x;x=y;y=temp;}x.rightChild=merge(x.rightChild,y);if(x.leftChild==null){// left child doesn't exist, so move right child to the left sidex.leftChild=x.rightChild;x.rightChild=null;}else{// left child does exist, so compare s-valuesif(x.leftChild.s<x.rightChild.s){Nodetemp=x.leftChild;x.leftChild=x.rightChild;x.rightChild=temp;}// since we know the right child has the lower s-value, we can just// add one to its s-valuex.s=x.rightChild.s+1;}returnx;}