面试常见算法2(找树中某个节点的前驱/后继节点)

34人阅读 评论(0) 收藏 举报
分类:

Node类的定义:

 public static class Node {
        public int value;
        public Node left;
        public Node right;
        public Node parent;

        public Node(int value) {
            this.value = value;
        }
    }

1、找后继节点

    // 若有右子树,则为右子树上最左的节点;
    // 若没有右子树,则为第一个满足某种要求的父节点,要求是我在该父节点的左子树上
    public static Node findSuccessorNode(Node head) {
        if (head == null) return null;
        if (head.right != null) return findLeftMostNode(head.right);
        else return findFatherWhichIamInHisLeft(head);
    }

    public static Node findLeftMostNode(Node node) {
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }

    public static Node findFatherWhichIamInHisLeft(Node node) {
        Node parent = node.parent;
        while (parent != null) {
            if (parent.left == node) {
                return parent;
            } else {
                node = parent;
                parent = parent.parent;
            }
        }
        return parent;
    }

测试函数:

  public static void main(String[] args) {
        Node head = new Node(6);
        head.parent = null;
        head.left = new Node(3);
        head.left.parent = head;
        head.left.left = new Node(1);
        head.left.left.parent = head.left;
        head.left.left.right = new Node(2);
        head.left.left.right.parent = head.left.left;
        head.left.right = new Node(4);
        head.left.right.parent = head.left;
        head.left.right.right = new Node(5);
        head.left.right.right.parent = head.left.right;
        head.right = new Node(9);
        head.right.parent = head;
        head.right.left = new Node(8);
        head.right.left.parent = head.right;
        head.right.left.left = new Node(7);
        head.right.left.left.parent = head.right.left;
        head.right.right = new Node(10);
        head.right.right.parent = head.right;

        Node test = head.left.left;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.left.left.right;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.left;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.left.right;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.left.right.right;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.right.left.left;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.right.left;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.right;
        System.out.println(test.value + " next: " + findSuccessorNode(test).value);
        test = head.right.right; // 10's next is null
        System.out.println(test.value + " next: " + findSuccessorNode(test));
    }
}

2、找前驱节点

    // 若有左子树,则为左子树上最右的节点;
    // 若没有左子树,则为第一个满足某种要求的父节点,要求是我在该父节点的右子树上
    public static Node findPrecursorNode(Node head) {
        if (head == null) return head;
        if (head.left != null) return findRightMost(head.left);
        else return findFatherWhichIamInHisRight(head);
    }
    public static Node findRightMost(Node node) {
        while (node.right != null) {
            node = node.right;
        }
        return node;
    }

测试函数:

   public static Node findFatherWhichIamInHisRight(Node node) {
        while (node.parent != null && node.parent.right != node)
            node = node.parent;
        return node.parent;
    }
    public static void main(String[] args) {
        Node head = new Node(6);
        head.parent = null;
        head.left = new Node(3);
        head.left.parent = head;
        head.left.left = new Node(1);
        head.left.left.parent = head.left;
        head.left.left.right = new Node(2);
        head.left.left.right.parent = head.left.left;
        head.left.right = new Node(4);
        head.left.right.parent = head.left;
        head.left.right.right = new Node(5);
        head.left.right.right.parent = head.left.right;
        head.right = new Node(9);
        head.right.parent = head;
        head.right.left = new Node(8);
        head.right.left.parent = head.right;
        head.right.left.left = new Node(7);
        head.right.left.left.parent = head.right.left;
        head.right.right = new Node(10);
        head.right.right.parent = head.right;

        Node test = head.right.right; // 10
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.right; // 9
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.right.left; // 8
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.right.left.left; // 7
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head; // 6
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.left.right.right; // 5
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.left.right; // 4
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.left; // 3
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.left.left.right; // 2
        System.out.println(test.value + " previous: " + findPrecursorNode(test).value);
        test = head.left.left; // 1's previous is null
        System.out.println(test.value + " previous: " + findPrecursorNode(test));
    }
查看评论

用Winsock完成了话音的一端—端传输

摘要:在Windows 95环境下,基于TCP/IP协议,用Winsock完成了话音的一端—端传输。采用双套接字技术,阐述了主要函数的使用要点,以及基于异步选择机制的应用方法。同时,给出了相应的实例程...
  • ghj1976
  • ghj1976
  • 2001-08-19 18:57:00
  • 1418

求二叉搜索树任一节点的前驱后继节点

二叉搜索树节点的前驱后继节点之前写过文章介绍了二叉搜索树以及其上的基本操作,但不包括求节点的前驱结点和后继节点。这是一个很老的问题了,首先看下某节点前驱和后继节点的定义。一个节点的 前驱结点:节点v...
  • zhaoyunfullmetal
  • zhaoyunfullmetal
  • 2015-08-27 20:55:58
  • 5692

二叉树遍历的前驱和后继规则说明

二叉树遍历的递归算法和非递归算法我们当然应该很熟悉了,不过还有另外一种遍 历方式,就是增加了树的构造,然后不允许递归或是用到栈进行遍历,如线索树或者是 有父母节点的二叉树等等等等。这样的遍历就需要我们...
  • xiaokang06
  • xiaokang06
  • 2014-03-25 19:51:22
  • 4592

二叉树的前驱和后继

前提二叉树的几何结构和遍历路径,是查找前驱和后继的基础。结点分类几何结构千变万化,但单个结点必然和至多3点邻接:左(或右)父,左子和右子。按照“X”型助记,交叉点为研究对象。为求结点N的前驱结点Pre...
  • liuchaoswu
  • liuchaoswu
  • 2015-05-03 08:56:29
  • 5111

线索二叉树中查找前驱和后继的问题

线索二叉树结点结构定义如下:若结点有左子树,则LChild域仍指向其左孩子; 否则,LChild域指向其某种遍历序列中的直接前驱结点。若结点有右子树,则RChild域仍指向其右孩子; 否则,RChi...
  • ldx19980108
  • ldx19980108
  • 2017-11-12 23:52:20
  • 581

二叉查找树后继节点和前驱节点查找

二叉查找树按照二叉树进行组织。二叉查找树关键字的存储方式总是瞒住二叉查找树性质: 设x为二查查找树种一个节点。如果y是x的左子树中的一个节点,那么key[x] >= key[y]。如果y是x的右子树...
  • zldeng_scir
  • zldeng_scir
  • 2011-08-08 16:39:00
  • 4277

二叉树的线索化及其前驱后继查找

一 实质 遍历二叉树过程中用线索(前驱和后继)取代
  • XSF50717
  • XSF50717
  • 2014-10-15 10:33:18
  • 3070

二叉树前驱后继的查找(这个容易理解)

转自度娘
  • gandge
  • gandge
  • 2014-05-14 23:04:25
  • 2256

求二叉查找树指定节点后继

题目 设计一个算法,找出二叉查找树中指定节点的“下一个节点”(也就是中序后继)。可以假定每个节点都含有指向父节点的链接。 分析 既然是二叉查找树,我们知道中序遍历结果为递增有序,那么可以直...
  • fly_yr
  • fly_yr
  • 2016-08-10 16:56:10
  • 1091

二叉树的后继结点

题目:给定一个二叉树上的结点,输出其后继结点(按中序遍历)
  • woshi750814343
  • woshi750814343
  • 2014-04-16 11:26:01
  • 1350
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 1203
    排名: 4万+